'rootDir' is expected to contain all source files. Bundle failed: test-icons
See original GitHub issueCurrent Behavior
Now when I run build
command in our repo with multiple publishable libs, I get error like this:
test-icons:build-lib
Bundling test-icons...
Error during bundle: Error: /Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-icons/src/index.ts(2,15): semantic error TS6059: File '/Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-core/src/index.ts' is not under 'rootDir' '/Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-icons/src'. 'rootDir' is expected to contain all source files.
Bundle failed: test-icons
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> NX Ran target build-lib for project test-icons (3s)
β 1/1 failed
β 0/1 succeeded [0 read from cache]
Expected Behavior
Expect to build successfully or with a better error message like: test-core
has no build-lib
target.
Steps to Reproduce
- clone the repro repo:
git clone git@github.com:SunStupic/build-lib-bug-repro.git
pnpm i
nx run test-icons:build-lib
Failure Logs
test-icons:build-lib
Bundling test-icons...
Error during bundle: Error: /Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-icons/src/index.ts(2,15): semantic error TS6059: File '/Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-core/src/index.ts' is not under 'rootDir' '/Users/stupic/Documents/demos/build-lib-bug-repro/libs/test-icons/src'. 'rootDir' is expected to contain all source files.
Bundle failed: test-icons
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> NX Ran target build-lib for project test-icons (3s)
β 1/1 failed
β 0/1 succeeded [0 read from cache]
Environment
nx report
> NX Running global Nx CLI with PNPM may have issues.
Prefer to use "pnpm" (https://pnpm.io/cli/exec) to execute commands in this workspace.
TIP create a shortcut such as: alias pnx="pnpm nx --"
> NX Report complete - copy this into the issue template
Node : 16.15.1
OS : darwin x64
pnpm : 7.5.0
nx : 14.4.3
@nrwl/angular : Not Found
@nrwl/cypress : 14.4.3
@nrwl/detox : Not Found
@nrwl/devkit : Not Found
@nrwl/eslint-plugin-nx : 14.4.3
@nrwl/express : Not Found
@nrwl/jest : 14.4.3
@nrwl/js : Not Found
@nrwl/linter : 14.4.3
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : 14.4.3
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : Not Found
@nrwl/web : 14.4.3
@nrwl/workspace : 14.4.3
typescript : 4.7.4
---------------------------------------
Community plugins:
Workaround
Now the workaround is simple, we need to add build-lib
for all the publishable packages or we change back to use build
.
Issue Analytics
- State:
- Created a year ago
- Comments:8
Top Results From Across the Web
'rootDir' is expected to contain all source files - Stack Overflow
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/Β ...
Read more >nrwl-nx/community - Gitter
So, anyone know why the bundle analyzer is no longer in the Angular Console? Witold KupΕ. @Azbesciak ... 'rootDir' is expected to contain...
Read more >TS6059: File '/mui-rff/test/TestUtils.tsx' is not under 'rootDir ...
'rootDir' is expected to contain all source files. ... Unfortunately, now IDEA is giving me an error that rootDir should contain all source...
Read more >Angular,monorepo,multiple libs and rootDir issue | by Arun Malik
On running βng build lib1β All we got is thisβ¦ error TS6059: File ... 'rootDir' is expected to contain all source files. Folder...
Read more >Diff - platform/frameworks/base - android Git repositories
The default value is {@code false} if this was * never set. ... <p>All shortcuts must have an intent, but this method will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To help everyone understand whatβs going on, here is the process of how I found the root cause.
Whatβs the problem
Say we have a monorepo, with several libraries inside, letβs name them
lib-a
,lib-b
andlib-c
.lib-a
depends onlib-b
andlib-c
, in/path-to-monorepo/libs/lib-a/src/foo.tsx
, there will be:Path aliases are defined in root
tsconfig.base.json
(which is extended by roottsconfig.json
):@project-org/lib-b
points to/path-to-monorepo/libs/lib-b/src/bar.ts
@project-org/lib-c
points to/path-to-monorepo/libs/lib-c/src/tor.ts
lib-a
has a build targetbuild-lib
, which invokes executor@nrwl/web:rollup
, another build targetbuild
which invokesbuild-lib
and some other targets.lib-b
has only a build targetbuild
which also invokes executor@nrwl/web:rollup
,lib-c
has 2 build targetsbuild
andbuild-lib
just likelib-a
.Here is the essential part of
project.json
oflib-a
:And here is the essential part of
project.json
oflib-b
:And here is the essential part of
project.json
oflib-c
:All
tsconfig.lib.json
extends the roottsconfig.base.json
.Now when we issue
nx run lib-a:build
, weβll see an error like:Whatβs strange is, thereβs NO such error for
lib-b
.Dig into the source code of nx to find the root cause
We can add
--verbose
option when buildinglib-a
to see where exactly the error is thrown, and it takes us here, hmm, doesnβt help too much.Letβs dive directly into rollup executor then.
NOTE the original error says
TS6059
- which means it is thrown bytsc
. Although the library is transpiled by babel by default,tsc
must be invoked somewhere.Ah, here it is. But, hmm, still doesnβt know what could go wrong.
Letβs take a look what
createCompilerOptions(options, dependencies)
produces.But hold on,
options
is rollup options, what aboutdependencies
? Itβs the calculated project dependencies, which looks like:However weβll find
@project-org/lib-b
missing in the list. Stay calm, weβll come back to this later.With this list of dependencies,
createCompilerOptions(options, dependencies)
produces:Huh?
@project/lib-c
points todist/libs/lib-c
instead oflibs/lib-c/src/tor.ts
, why?In
packages/workspace/src/utilities/buildable-libs-utils.ts
,computeCompilerOptionsPaths()
reads path aliases defined in projectβstsconfig.json
and update them, inupdatePaths()
,paths[dep.name] = dep.outputs
. Ah ha, this is where the outputs folderdist/libs/lib-c
is used to replaced the original path. And because@project-org/lib-b
is missing in the dependency list, its path alias stays intact.Whatβs inside
dist/libs/lib-c
? Generated.d.ts
files, transpiled.js
files, etc., anyway NO.ts
source files, thus wonβt break TS check performed byrollup-plugin-typescript2
.However,
@project/lib-b
is not so lucky,libs/lib-b/src/foo.ts
is a TS source file itself outside ofrootDir
, thustsc
will complain.Now, the question becomes, why is
@project-org/lib-b
missing in the dependency list?Letβs get back to
const { target, dependencies } = calculateProjectDependencies(...)
, one of the arguments of this function call,projectGraph
, is read fromnode_modules/.cache/nx/nxdeps.json
(default path).And next is the essential part.
const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
, whereprojGraph.nodes
looks like:if (isBuildable(targetName, depNode))
,targetName
isbuild-lib
since rollup executor is invoked bybuild-lib
oflib-a
.Letβs look at the definition of
isBuildable()
, it determines whether a dependency is buildable by:As for
lib-c
, it returnstrue
. But as forlib-b
, since it doesnβt have abuild-lib
target, that condition returnsfalse
! This is whylib-b
was missing in the dependency list.Solution
To make nx thinks
lib-b
as buildable, we need to define abuild-lib
target for it. Here is the updated version ofproject.json
oflib-b
:To summarize it, all internal dependencies of a library must have a build target of the same name, in order to make nx treat them as buildable.
Other workarounds
rollup-plugin-typescript2
.@project-org/lib-b
intsconfig.base.json
to"@project-org/lib-b": ["dist/libs/lib-b", "libs/lib-b/src/bar.ts"]
@project-org/lib-b
will point todist
directory which contains.d.ts
files but not real source.ts
files, not a good development experience."@project-org/lib-b": ["dist/libs/lib-b"]
inlibs/lib-b/tsconfig.lib.json
@project-org/lib-b
still points tolibs/lib-b/src/bar.ts
in our editor (because of the path alias"@project-org/lib-b": ["libs/lib-b/src/bar.ts"]
in roottsconfig.json
), while"@project-org/lib-b": ["dist/libs/lib-b"]
inlibs/lib-b/tsconfig.lib.json
overrides it - whenparsedTSConfig.compilerOptions.paths
exists, path aliases inparsedTSConfig.extends
are bypassed.Thats for sure works, but is hard to develop this way. You have to manually build libraries in correct order. You are also importing now from DIST, means changes you make are not seamless applied to the rest of the project.