Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'rootDir' is expected to contain all source files

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;   } } 
like image 510
Sam Herrmann Avatar asked Aug 14 '18 21:08

Sam Herrmann


People also ask

Is not under rootDir Web rootDir is expected to contain all source files?

The "File is not under 'rootDir'" error occurs when we try to import something from a file that is not located under the specified rootDir in tsconfig. json . To solve the error, move the file under the project's root directory or remove the rootDir setting from tsconfig. json .

What is rootDir in tsconfig?

Default: The longest common path of all non-declaration input files. If composite is set, the default is instead the directory containing the tsconfig. json file. When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory.

How does Tsconfig work?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.


1 Answers

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.

like image 159
mvermand Avatar answered Sep 19 '22 21:09

mvermand