question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

If not all sources are under rootDir, you only get an error message when combined with outDir, not with outFile

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Reactions:10
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

47reactions
repairearthcommented, Jul 18, 2019

My workaround for monorepo:

  1. set references in package1’s tsconfig.json
{
  "compilerOptions": {
    "declarationDir": "dist",
    "rootDir": "src"
  },
  "include": ["src"],
  "references": [
    {
      "path": "../package2"
    }
  ]
}

  1. set composite in package2’s tsconfig.json
"compilerOptions": {
    "declarationDir": "dist",
    "rootDir": "src",
    "composite": true
}

hopes it works for you guys, good luck!

1reaction
xgqfrmscommented, Jul 27, 2022

solution

exclude

{
  "compilerOptions": {
    "target": "es2015",
    "module": "esnext",
    "moduleResolution": "node",
    // Tells TypeScript to read JS files, as normally they are ignored as source files
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    // go to js file when using IDE functions like "Go to Definition" in VSCode
    "declarationMap": true,
    // This compiler run should only output d.ts files
    "emitDeclarationOnly": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "rootDir": "./src/",
    "outDir": "./dist/",
    "strict": true,
    "experimentalDecorators": true,
    "useDefineForClassFields": false,
    "skipLibCheck": true, 
    "forceConsistentCasingInFileNames": true
  },
  "exclude":[
    "rollup.config.js",
    "test",
    "dist",
    "node_modules",
  ],
}


https://stackoverflow.com/questions/40579191/unexpected-behaviour-of-rootdir-and-outdir/40752115#40752115

Read more comments on GitHub >

github_iconTop Results From Across the Web

error TS6059: File is not under 'rootDir' .. 'rootDir' is expected ...
One fix is to just remove "rootDir": "src" from compiler options, so it gets set automatically. Caution: rootDir will then consider both ...
Read more >
TSConfig Option: rootDir - TypeScript
When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory. For example, let's...
Read more >
Troubleshooting ts_project failures | Aspect Docs
Bazel expects that each output is produced by a single rule. Thus if you have two ts_project rules with overlapping sources (the same...
Read more >
TypeScript rules for Bazel - GitHub Pages
Thus if you have two ts_project rules with overlapping sources (the same .ts file appears in more than one) then you get an...
Read more >
TSConfig Reference - TypeScript
Specifies an array of filenames or patterns to include in the program. These filenames are resolved relative to the directory containing the tsconfig.json ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found