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

  1. 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
  1. Add the following to tsconfig.base.json:
    "paths": {
      "lib1/*": ["packages/lib1/*"]
    }
  1. Add the following to packages\lib2\src\lib\lib2.ts:
import { lib1 } from 'lib1/src/lib/lib1';
  1. 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

Most upvoted comments

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.js After const tscOptions = { add rootDir, so the code looks like this:

        const tscOptions = {
			rootDir: normalizedOptions.root,

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.json name 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 @acme was missing from the name