In my Angular CLI workspace, I have two library projects named foo
and bar
. The issue arises when I try to build the second library, foo
, as it fails with the following error:
Error TS6059: File '/code/projects/bar/src/lib/types.ts' is not located under 'rootDir' '/code/projects/foo/src'. The 'rootDir' should contain all source files.
Error: Error TS6059: File '/code/projects/bar/src/lib/types.ts' is not located under 'rootDir' '/code/projects/foo/src'. The 'rootDir' should 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>)
To recreate this error, I have set up a sandbox repository on GitHub available here. While attempting to simplify the code, I encountered this issue. By running npm run build
on the rootDir-expect-all-source-files-error branch, you can experience the error firsthand. What could be the root cause of this problem? Is it related to a bug within ng-packagr
, ngc
, or tsc
? Or is it just a configuration mishap?
Observations
Below are some modifications that allow the build to succeed. However, I am interested in understanding what is triggering the error in its current state.
bar.component.ts
Build Failure
export class BarComponent {
list = this.barService.list();
constructor(private barService: BarService) {}
}
Successful Build
Initiate the list property inside the constructor instead of inline
export class BarComponent {
list;
constructor(private barService: BarService) {
this.list = this.barService.list();
}
}
bar.service.ts
Build Failure
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;
}
}
Successful Build
Remove the specific 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;
}
}