nx: library importing other library using wildcard path mapping fails with "is not under 'rootDir'"
Current Behavior
When one library imports from another, I get .../packages/lib1/src/lib/lib1.ts' is not under 'rootDir' 'packages/lib2'
Note that this doesn’t happen if an app imports the library in the same way - presumably because an app uses executor @nrwl/node:webpack rather the a library’s @nrwl/js:tsc.
Expected Behavior
library should import. This is a new project, no migration, therefore it’s not the same like https://github.com/nrwl/nx/issues/10785#issuecomment-1159017136.
Steps to Reproduce
- Start from scratch:
npx create-nx-workspace badlibs --preset=ts --nxCloud==false
cd badlibs
nx generate @nrwl/js:library --name=lib1 --buildable
nx generate @nrwl/js:library --name=lib2 --buildable
- Add the following to
tsconfig.base.json:
"paths": {
"lib1/*": ["packages/lib1/*"]
}
- Add the following to
packages\lib2\src\lib\lib2.ts:
import { lib1 } from 'lib1/src/lib/lib1';
- Build all by running:
nx affected:build -all --skip-nx-cache
Result:
× nx run lib2:build
Compiling TypeScript files for project "lib2"...
packages/lib2/src/lib/lib2.ts:1:22 - error TS6059: File 'C:/mono/badlibs/packages/lib1/src/lib/lib1.ts' is not under 'rootDir' 'packages/lib2'. 'rootDir' is expected to contain all source files.
1 import { lib1 } from 'lib1/src/lib/lib1';
Failure Logs
Environment
Node : 14.20.0
OS : win32 x64
npm : 6.14.17
nx : 14.5.6
@nrwl/angular : Not Found
@nrwl/cypress : Not Found
@nrwl/detox : Not Found
@nrwl/devkit : 14.5.6
@nrwl/eslint-plugin-nx : 14.5.6
@nrwl/express : Not Found
@nrwl/jest : 14.5.6
@nrwl/js : 14.5.6
@nrwl/linter : 14.5.6
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : Not Found
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : Not Found
@nrwl/web : Not Found
@nrwl/workspace : 14.5.6
typescript : 4.7.4
---------------------------------------
Local workspace plugins:
---------------------------------------
Community plugins:
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 16 (3 by maintainers)
Commits related to this issue
- fix(js): use workspace rootDir while compiling TS files fix: #11583 — committed to kopach/nx by kopach 8 months ago
- fix(js): use workspace rootDir while compiling TS files fix: #11583 — committed to kopach/nx by kopach 8 months ago
- fix(js): use workspace rootDir while compiling TS files fix: #11583 — committed to kopach/nx by kopach 8 months ago
Looking into nx code, I found a workaround that unblocks this for me. I don’t know enough the nx codebase to say whether this is the actual fix…
edit
.\node_modules\@nrwl\js\src\utils\typescript\compile-typescript-files.jsAfterconst tscOptions = {addrootDir, so the code looks like this:https://medium.com/@thearunmalik/angular-monorepo-multiple-libs-and-rootdir-issue-7b8270cbc167
this solution worked for me
same problem
I’ve been fighting with this issue on and off for the last few days - I finally managed to figure the problem for me at least; thought it worth posting here if it helps anyone.
Check that your library
package.jsonname hasn’t changed; I think my editor was trying to autocorrect the created library name for new libraries (to be fair they do break the required pattern as @kevinpastor points out@acme/foo/bar- in my case the@acmewas missing from the name