'rootDir' 预计包含所有源文件

'rootDir' is expected to contain all source files

我有一个 Angular CLI 工作区,其中包含两个库项目,foobar。当我构建两个库中的第二个 foo 时,构建失败并出现以下错误:

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>)

我已经在 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 分支的沙箱回购中重现了错误。错误的原因是什么?这可能是 ng-packagrngctsc 的错误?还是仅仅是配置问题?

观察

下面是我可以使构建通过的代码更改,但我想知道是什么导致了代码的错误。

bar.component.ts

构建失败

export class BarComponent {

  list = this.barService.list();

  constructor(private barService: BarService) {}
}

构建通过

在构造函数中而不是内联初始化列表属性

export class BarComponent {

  list;

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

bar.service.ts

构建失败

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;
  }
}

构建通过

删除数据类型

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;
  }
}

这看起来像是由于 TypeScript 2.9 中引入的 import types 而出现的问题。发出时,这些未正确重新布线,请参见第 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;
}

在上面发出的 dts 中,list: import("projects/bar/src/lib/types").Item[]; 应该是类似 import("./types").Item[]; 的东西。

解决此问题的方法是从您的代码中显式设置它,而不是推断类型。

bar.component.ts中更改以下内容;

list = this.barService.list();

至:

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

这将删除类型导入,并将构建使用库。

我还检查了一些未来版本的 TypeScript,它仍然是 TypeScript 3.0.1 中的一个问题,但看起来它已在 dev 版本的 TypeScript 3.1.0,即3.1.0-dev.20180813

我遇到了同样的问题,但是@Agius 的解决方案没有帮助。

我有:

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

事实上,我通过拖动 WebStorm 中的文件夹将一个组件从 lib2 移动到 lib1。通过这样做,lib2 中对该组件的引用不会被删除,而是会更新并指向 lib1 的源文件夹。我忘记删除 lib2 中不再需要的这些引用。在删除对 lib2 中组件的所有引用后,该库已编译。

我不得不删除

中的引用
  • public_api.ts
  • ./lib/lib2.module.ts

也许你的项目中有更多的引用。

此问题与未正确提供导入路径的导入语句有关。

例如:-

 import * from '../.././../tools1/tools2/tools/demo';

应该是

import * from '@tools/tools/demo';

这里@tools被认为是根目录。 注意:- 上面提到的路径只是一个例子。

如果您正在使用 https://nx.dev 并且收到此错误...

我也遇到了这个问题...

我快要死了...我不明白为什么 nx 没有正确构建...它给了我很多关于我引用的项目@scope1/lib2 和所有项目的错误它的.ts文件在引用它的项目之外......我还注意到dep-graph有@scope1/lib2但是引用库没有依赖箭头......

我找到了这个修复:

rm -rf node_modules/.cache

我猜 nx 在缓存中有错误...删除后,dep-graph 最终正确显示 link 并且构建开始工作

我有一个打字稿项目,遇到一个问题,抱怨导入时出现同样的错误。我已经通过更正 tsconfig.json 文件中 include 的值来解决它,如下所示

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2017",
    "noImplicitAny": true,
    "preserveConstEnums": true,
    "outDir": "./dist",
  },
  "exclude": ["node_modules", "**/*.test.ts"],
  "include": ["src"]
}

希望这可能有所帮助。