TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

The output structures of `rootDir` and `rootDirs` are inconsistent

Open TomFryersMidsummer opened this issue 5 months ago • 1 comments

🔎 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.

TomFryersMidsummer avatar Jun 17 '25 13:06 TomFryersMidsummer

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.

RyanCavanaugh avatar Jun 17 '25 16:06 RyanCavanaugh

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar Jun 20 '25 01:06 typescript-bot