egoist / egoist/tsup

"error TS6059: File is not under 'rootDir' '.'. 'rootDir' is expected to contain all source files." when generating .d.ts files in monorepo

Open
#929 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
275
PR merge metrics
No merged PRs in 30d

Description

Hi!

I encountered the following strange error when trying to set up a build step with `tsup` for a library in a monorepo:

```
$ pnpm nx build my-library # <-- original command run in terminal

✔ 1/1 dependent project tasks succeeded [1 read from cache]

Hint: you can run the command with --verbose to see the full dependent project outputs

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> nx run my-library:build

> @my-monorepo/my-library@0.0.1 build /my-monorepo/libs/my-library
> tsup
▲ [WARNING] "tsconfig.json" does not affect esbuild's own target setting [tsconfig.json]
../../tsconfig.base.json:11:8:
11 │ "target": "es2015",
╵ ~~~~~~~~
This is because esbuild supports reading from multiple "tsconfig.json" files within a single build, and using different language targets for different files in the same build wouldn't be correct. If you want to set esbuild's language target, you should use esbuild's own global "target" setting such as with "target: 'es2015'".
▲ [WARNING] "tsconfig.json" does not affect esbuild's own target setting [tsconfig.json]
../../tsconfig.base.json:11:8:
11 │ "target": "es2015",
╵ ~~~~~~~~
This is because esbuild supports reading from multiple "tsconfig.json" files within a single build, and using different language targets for different files in the same build wouldn't be correct. If you want to set esbuild's language target, you should use esbuild's own global "target" setting such as with "target: 'es2015'".
CLI Building entry: src/index.ts
CLI Using tsconfig: tsconfig.json
CLI tsup v7.0.0
CLI Using tsup config: /my-monorepo/libs/my-library/tsup.config.ts
CLI Target: es2015
CLI Cleaning output folder
ESM Build start
ESM ../../dist/libs/my-library/index.js 13.21 KB
ESM ⚡️ Build success in 111ms
onSuccess:
- Copied README.md and ../../LICENSE to out directory.
- Merged package.json and ../../package.json and wrote to out directory.
- Done.
DTS Build start
src/index.ts(1,15): error TS6059: File '/my-monorepo/libs/my-other-library/src/index.ts' is not under 'rootDir' '.'. 'rootDir' is expected to contain all source files.
The file is in the program because:
Imported via "@my-monorepo/my-other-library" from file '/my-monorepo/libs/my-library/src/index.ts'
Error: error occured in dts build
at Worker. (/my-monorepo/node_modules/.pnpm/tsup@7.0.0_@swc+core@1.3.54_postcss@8.4.21_ts-node@10.9.1_typescript@4.8.4/node_modules/tsup/dist/index.js:2297:26)
at Worker.emit (node:events:513:28)
at MessagePort. (node:internal/worker:243:53)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)
DTS Build error
RollupError: Failed to compile. Check the logs above.
at error (/my-monorepo/node_modules/.pnpm/rollup@3.21.0/node_modules/rollup/dist/shared/rollup.js:279:30)
at Object.error (/my-monorepo/node_modules/.pnpm/rollup@3.21.0/node_modules/rollup/dist/shared/rollup.js:24825:20)
at Object.error (/my-monorepo/node_modules/.pnpm/rollup@3.21.0/node_modules/rollup/dist/shared/rollup.js:24019:42)
at generateDtsFromTs (/my-monorepo/node_modules/.pnpm/tsup@7.0.0_@swc+core@1.3.54_postcss@8.4.21_ts-node@10.9.1_typescript@4.8.4/node_modules/tsup/dist/rollup.js:7498:22)
at /my-monorepo/node_modules/.pnpm/tsup@7.0.0_@swc+core@1.3.54_postcss@8.4.21_ts-node@10.9.1_typescript@4.8.4/node_modules/tsup/dist/rollup.js:7505:59
at _nullishCoalesce (/my-monorepo/node_modules/.pnpm/tsup@7.0.0_@swc+core@1.3.54_postcss@8.4.21_ts-node@10.9.1_typescript@4.8.4/node_modules/tsup/dist/rollup.js:1:198)
at Object.transform (/my-monorepo/node_modules/.pnpm/tsup@7.0.0_@swc+core@1.3.54_postcss@8.4.21_ts-node@10.9.1_typescript@4.8.4/node_modules/tsup/dist/rollup.js:7505:18)
at /my-monorepo/node_modules/.pnpm/rollup@3.21.0/node_modules/rollup/dist/shared/rollup.js:25024:40
 ELIFECYCLE  Command failed with exit code 1.

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> NX Ran target build for project my-library and 1 task(s) they depend on (4s)

✖ 1/2 failed
✔ 1/2 succeeded [1 read from cache]
```

It's a [Nx](https://github.com/nrwl/nx) monorepo and `my-library` depends on `my-other-library`.

Directory structure looks like following:

```
my-monorepo/
├─ libs/
│ ├─ my-library/
│ │ ├─ tsconfig.json
│ │ ├─ tsup.config.ts
│ ├─ my-other-library/
│ │ ├─ tsconfig.json
├─ tsconfig.base.json
```

In `tsconfig.base.json`:

```json
{
"compilerOptions": {
"rootDir": ".",
"paths": {
"@my-monorepo/my-library": [
"libs/my-library/src/index.ts"
],
"@my-monorepo/my-other-library": [
"libs/my-other-library/src/index.ts"
],
}
}
}
```

In `libs/my-library/tsconfig.json`:

```json
{
"extends": "../../tsconfig.base.json"
}
```

My theory is that the issue has to do with `tsup` (or `rollup-plugin-dts`, which it uses internally for generating the declaration files) viewing `"rootDir": "."` as pointing to `/my-monorepo/libs/my-library/` (the root directory of the library), when in fact `"rootDir": "."` is meant to be viewed as pointing to `/my-monorepo/` (the root directory of the monorepo) as it's defined in `tsconfig.base.json` which resides in the root directory of the monorepo, so relative paths within that file should be resolved relative to the location of the file.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with `pnpm nx build my-library`, then inspect `libs/my-library/tsconfig.json`, `libs/my-library/tsup.config.ts`, and the root `tsconfig.base.json`, focusing on the declaration build. Done means the monorepo dependency can generate `.d.ts` files without TS6059 while preserving the reported library build.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.