Cannot build library that depends on other library in monorepo
See original GitHub issue[x] Bug Report
[ ] Feature Request
Description
We have a YARN workspace (no PnP) monorepo running angular v13. Structure is as below:
dist node_modules projects
- library 1
- library 2
- app
tsconfig.json package.json
Library 2 depends on library 1 for methods, types etc.
Library 1 builds fine with the command ng build library 1
Library 2 fails to build with ng build library 2
.
App also depends on Library 1 and builds fine.
Possibly related to this issue? https://github.com/ng-packagr/ng-packagr/issues/1210
How To Reproduce
root tsconfig.json looks like this:
{ “compileOnSave”: false, “compilerOptions”: { “baseUrl”: “./”, “outDir”: “./dist/out-tsc”, “forceConsistentCasingInFileNames”: true, “noFallthroughCasesInSwitch”: true, “sourceMap”: true, “declaration”: false, “downlevelIteration”: true, “experimentalDecorators”: true, “moduleResolution”: “node”, “importHelpers”: true, “target”: “es2015”, “module”: “es2020”, “lib”: [ “es2018”, “dom” ], “paths”: { “@myproject/library2/": [ "./projects/library2/” ], “@myproject/library1/": [ "./projects/library1/” ] } }, “angularCompilerOptions”: { “strictInjectionParameters”: true, “strictInputAccessModifiers”: true, “strictTemplates”: true } }
I get this error when I run library 2 build command after library 1 finishes building.
File 'projects/library1/utilities/utilities.ngtypecheck.ts' is not under 'rootDir' 'projects\library2\common'. 'rootDir' is expected to contain all source files.
This is the file where I am importing library1 method into library2.
Expected Behaviour
Both library to compile the same way without issues.
Version Information
ng-packagr: 13.1.2
@angular/compiler: 13.1.1
rollup: 2.56.3
typescript: 4.5.4
yarn: 1.22.15
node: 14.17.5
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
Yes, indeed this is by design and to solve the issue, you’d need to specific the paths to point to the folder.
You can also do something like the below to fallback to the source files in
projects
whendist
is not available. Although I don’t really recommand this approach (Will discuss this further down).Angular CLI does not add path mappings to source files. It only adds paths to the
dist
path (https://github.com/angular/angular-cli/blob/23dac94c78718e3292556482b9f581b8e57e2887/packages/schematics/angular/library/index.ts#L145) and this is for a reason.Pointing to paths of a library to the project source files instead of the disk causes these libraries are no longer treated as such, but rather they become part of the application compilation. So truly, they are no different from a sub-directory in the app itself. This results in a monolith application compilation and in same cases can result in broken behaviour when eventually you try to consume these projects as a true library. This is because different compilation rules and pipelines are used to build applications and libraries.
In general it is a bad practice to build your library as part of your application.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
This action has been performed automatically by a bot.