'rootDir' is expected to contain all source files

随声附和 提交于 2019-11-28 07:10:39

问题


I have an Angular CLI workspace containing two library projects, foo and bar. When I build the second of the two libraries, foo, the build fails with the following error:

error TS6059: File '/code/projects/bar/src/lib/types.ts' is not under 'rootDir' '/code/projects/foo/src'. 'rootDir' is expected tocontain all source files.

Error: error TS6059: File '/code/projects/bar/src/lib/types.ts' is not under 'rootDir' '/code/projects/foo/src'. 'rootDir' is expected to contain all source files.

    at Object.<anonymous> (/code/node_modules/ng-packagr/lib/ngc/compile-source-files.js:53:68)
    at Generator.next (<anonymous>)
    at /code/node_modules/ng-packagr/lib/ngc/compile-source-files.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/code/node_modules/ng-packagr/lib/ngc/compile-source-files.js:3:12)
    at Object.compileSourceFiles (/code/node_modules/ng-packagr/lib/ngc/compile-source-files.js:19:12)
    at Object.<anonymous> (/code/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:26:32)
    at Generator.next (<anonymous>)
    at /code/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:7:71
    at new Promise (<anonymous>)

I have reproduced the error in a sandbox repo on GitHub here. I have simplified the code to as much as I can while still experiencing the error. You can reproduce the error by executing npm run build on the rootDir-expect-all-source-files-error branch. What is the cause of the error? May this be a bug with ng-packagr or ngc or tsc? Or is it simply a configuration issue?

Observations

Below are code changes with which I can make the build pass, but I would like to know what is causing the error with the code as is.

bar.component.ts

Build fails

export class BarComponent {

  list = this.barService.list();

  constructor(private barService: BarService) {}
}

Build passes

Initialize list property in constructor instead of inline

export class BarComponent {

  list;

  constructor(private barService: BarService) {
    this.list = this.barService.list();
  }
}

bar.service.ts

Build fails

import { Injectable } from '@angular/core';
import { List, Item } from './types';

@Injectable({
  providedIn: 'root'
})
export class BarService {

  private _list: List = [];

  constructor() { }

  add(item: Item): void {
    this._list.push(item);
  }

  list(): List {
    return this._list;
  }
}

Build passes

Remove the data types

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class BarService {

  private _list: any[] = [];

  constructor() { }

  add(item: any): void {
    this._list.push(item);
  }

  list(): any {
    return this._list;
  }
}

回答1:


This looks like the problem that is occurring due to the import types which was introduced in TypeScript 2.9. When emitted these are not being rewired properly see line 3.

dist/bar/lib/bar.component.d.ts(5,11):

export declare class BarComponent implements OnInit {
    private barService;
    list: import("projects/bar/src/lib/types").Item[]; 
    constructor(barService: BarService);
    ngOnInit(): void;
}

In the above emitted dts, list: import("projects/bar/src/lib/types").Item[]; should be something like import("./types").Item[]; instead.

A workaround for this can be that from your code instead infering the type, you explicitly set it.

in bar.component.ts change the below;

list = this.barService.list();

to:

list: Item[] = this.barService.list();

This will remove the type import and the consuming library will build.

I also checked a bit with future versions of TypeScript, it is still an issue in TypeScript 3.0.1, but it looks like it has been addressed in dev version of TypeScript 3.1.0, ie 3.1.0-dev.20180813




回答2:


I had the same issue, but the solution of @Agius did not help.

I had:

Angular Workspace
  - projects
      - lib1
      - lib2
  - src
      - test application

In fact I had moved a component from lib2 to lib1, by dragging the folder in WebStorm. By doing this, the references in lib2 to that component were not removed but updated and pointed to the source-folder of lib1. I forgot to remove these references which were no longer needed in lib2. After removing all references to the component in lib2, that library compiled.

I had to remove the references in

  • public_api.ts
  • ./lib/lib2.module.ts

Maybe there are more references in your project.



来源:https://stackoverflow.com/questions/51850063/rootdir-is-expected-to-contain-all-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!