“ rootDir”应包含所有源文件 [英] 'rootDir' is expected to contain all source files

查看:270
本文介绍了“ rootDir”应包含所有源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


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

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上的沙箱仓库中重现了错误此处。我仍然尽可能地简化了代码,同时仍然遇到错误。您可以通过在npm run build 来重现该错误。 all-source-files-error rel = noreferrer> rootDir-expect-all-source-files-error 分支。错误原因是什么?可能是 ng-packagr ngc tsc ?还是仅仅是配置问题?

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?

下面是我可以用来进行构建的代码更改通过,但是我想知道是什么引起了代码的错误。

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.

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


推荐答案

此看起来是由于 TypeScript 2.9 中引入的导入类型而引起的问题。发出时,这些未正确接线,请参见第3行。

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

在上面发出的 dts 列表:import( projects / bar / src / lib / types)。Item []; 应该类似于 import( ./types)。Item []; 代替。

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.

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

list = this.barService.list();

至:

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

这将删除类型import并创建使用库。

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

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

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

这篇关于“ rootDir”应包含所有源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆