If not all sources are under rootDir, you only get an error message when combined with outDir, not with outFile
See original GitHub issueI realize this may be the currently intended behavior — somewhere, I think I saw it explained that rootDir only affects the layout of files under the output directory. However, I argue that its current behavior, when combined with outFile, can be improved.
The issue is this: Suppose you specify a rootDir, and at least one file is NOT under that directory. Then:
-
If you are compiling with
--outDir
, you get an error message, as expected:error TS6059: File ‘…’ is not under ‘rootDir’ ‘…’. ‘rootDir’ is expected to contain all source files.
-
But if you compile with
--outFile
, you get no error message, and the compiler completely ignores the specified rootDir. This means, for example, that if you have a bug in your tsconfig.json, generated AMD files will have unexpected module names.
TypeScript Version: 3.4.1
Search Terms: rootdir, amd, outfile
Code
A project with two tsconfig files — one targeting commonjs with --outDir
, and one targeting amd with --outFile
.
tsconfig.commonjs.json:
{
"compilerOptions": {
"rootDir": "src",
"module": "commonjs",
"outDir": "out"
},
"includes": [
"src/**/*.ts",
"other-src/**/*.ts"
]
}
tsconfig.amd.json:
{
"compilerOptions": {
"rootDir": "src",
"module": "amd",
"outFile": "out/bundle.js"
},
"includes": [
"src/**/*.ts",
"other-src/**/*.ts"
]
}
src/foo.ts:
export const name = "foo";
other-src/bar.ts:
export const name = "bar";
Expected behavior:
Compile with tsc -p tsconfig.commonjs.json
. As expected, you get a TS6059 error, since other-src/bar.ts
is not under the rootDir src
.
Compile with tsc -p tsconfig.amd.json
. I would expect that to get the same error, since.
Actual behavior:
When compiling with tsc -p tsconfig.amd.json
, it compiles without complaint. But if you look at the generated out/bundle.js
, you see this — notice the module names! They are "other-src/mylib"
and "src/foo"
, completely ignoring the (incorrect) rootDir that I specified.
define("other-src/mylib", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.name = "mylib";
});
define("src/foo", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.name = "foo";
});
Related Issues:
In https://github.com/Microsoft/TypeScript/issues/9858#issuecomment-234379302, @mhegazy explained:
the rootDir is used to build the output folder structure. all files have to be under rootDir for the compiler to know where to write the output. without this constraint, if there are files that are not under rootDir, the compiler has two options 1. either they will be emitted in a totally unexpected place, or 2. not emitted at all. i would say an error message is much better than a silent failure.
I agree with this reasoning; but I think it should apply to outFile scenarios as well as to outDir ones.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:10 (1 by maintainers)
Top GitHub Comments
My workaround for monorepo:
references
in package1’stsconfig.json
composite
in package2’stsconfig.json
hopes it works for you guys, good luck!
solution
https://stackoverflow.com/questions/40579191/unexpected-behaviour-of-rootdir-and-outdir/40752115#40752115