TypeScript
TypeScript copied to clipboard
The output structures of `rootDir` and `rootDirs` are inconsistent
🔎 Search Terms
rootDirs rootDir inconsistent structure
Maybe related to #44321 and #22039.
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about rootDirs (of which there are none). I tried next, 5.8.3 and 2.0.8. All exhibit this behaviour.
⏯ Playground Link
Here's a tiny repo or see below.
💻 Code
tsconfig1.json:
{"compilerOptions": {"outDir": "build", "rootDir": "src"}
tsconfig2.json:
{"compilerOptions": {"outDir": "build", "rootDirs": ["src"]}
src/foo/bar.ts is an empty file.
🙁 Actual behavior
Building with "rootDir": "src" and rootDirs: ["src"] produces different output:
$ tsc -p tsconfig1.json
$ find build/ -type f
build/foo/bar.js
$ rm -r build/
$ tsc -p tsconfig2.json
$ find build/ -type f
build/bar.js
🙂 Expected behavior
Both should do the same thing. Indeed, according to the documentation, using rootDirs ‘does not affect how TypeScript emits JavaScript’.
Additional information about the issue
For single-element arrays, of course, it doesn't really matter. But as it stands, I can't see a way to get both the non-nested structure rootDir provides and multiple root directories. #22039 perhaps suggests that setting both rootDir and rootDirs would work, but doing that makes tsc complain about whichever of rootDirs isn't in rootDir.
Confusingly, these settings are for basically completely unrelated behaviors, and are not expected to do the same thing.
rootDir determines what base directory is used to compute the output path. So if you have a file src/foo/bar/baz.ts and set rootDir to src/foo, the output path will be bar/baz.js
rootDirs is a set of directories to start relative module resolution from.
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.