question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alan-agius4commented, Jan 25, 2022

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 when dist is not available. Although I don’t really recommand this approach (Will discuss this further down).

"@myproject/library1/": [
  "./dist/library1/",
  "./projects/library1/"
]

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.

0reactions
github-actions[bot]commented, Feb 25, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my Angular library not included in prod build?
Running locally with ng serve is fine but ng build app doesn't include the models library dependency. I've tried building both ( ng...
Read more >
How to Easily Manage Dependencies in a JS Monorepo
The problem is that you might have a dependency (like Lodash or prettier) installed in the root, and a different versions in the...
Read more >
Enforcing Dependency Constraints in Nx Monorepo Projects
Within the Libs folder you can end up creating lots of projects and it is very confusing when every project can depend on...
Read more >
Phantom dependencies - Rush.js
Everyone knows that software packages can depend on other packages, and the resulting dependency graph is a kind of directed acyclic graph in...
Read more >
Getting Started with Monorepo with Nx Nrwl - Rupesh Tiwari
Create Monorepo using Nx with angular projects and applications. ... Who depends on whom within a library ? Before Push to Git Guidelines ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found