typescript 'rootDir' 应该包含所有源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51850063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:35:04  来源:igfitidea点击:

'rootDir' is expected to contain all source files

angulartypescriptangular-cling-packagrangular-library

提问by Sam Herrmann

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

我有一个 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>)

错误 TS6059:文件“/code/projects/bar/src/lib/types.ts”不在“rootDir”“/code/projects/foo/src”下。'rootDir' 应包含所有源文件。

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 buildon the rootDir-expect-all-source-files-errorbranch. What is the cause of the error? May this be a bug with ng-packagror ngcor tsc? Or is it simply a configuration issue?

我已复制了错误的沙箱回购GitHub上这里。我已经尽可能地简化了代码,同时仍然遇到错误。您可以通过npm run buildrootDir-expect-all-source-files-error分支上执行来重现错误。错误的原因是什么?这可能与一个bugng-packagrngctsc?还是仅仅是配置问题?

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

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

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

采纳答案by A. Agius

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

这看起来是由于发生的问题import types这是在引入TypeScript 2.9。当发出时,这些未正确重新接线,请参见第 3 行。

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

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.

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

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

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

in bar.component.tschange the below;

bar.component.ts改变以下;

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 devversion of TypeScript 3.1.0, ie 3.1.0-dev.20180813

我还检查了未来版本的 TypeScript,它仍然是 TypeScript 的问题3.0.1,但看起来它已经在devTypeScript 的版本中得到解决3.1.0,即3.1.0-dev.20180813

回答by mvermand

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

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

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.

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

I had to remove the references in

我不得不删除中的引用

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

Maybe there are more references in your project.

也许你的项目中有更多的参考。