'error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files

I am getting this fairly non-sensical tsc transpilation error:

error TS6059: File '/Users/alex/codes/interos/teros-cli/src/logging.ts' is not under 'rootDir' '/Users/alex/codes/teros/notifier-server/src'. 'rootDir' is expected to contain all source files.

my PWD is /Users/alex/codes/teros/notifier-server and the tsconfig.json file for /Users/alex/codes/teros/notifier-server/tsconfig.json is:

{
  "compilerOptions": {
    "outDir": "dist",
    "allowJs": false,
    "pretty": true,
    "resolveJsonModule": true,
    "sourceMap": false,
    "skipLibCheck": true,
    "rootDir": "src",
    "declaration": false,
    "baseUrl": ".",
    "target": "es2018",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "allowUnreachableCode": true,
    "lib": [
      "es2017",
      "es2018"
    ]
  },
  "compileOnSave": false,
  "include": [
    "src"
  ]
}

this seems like a bug..since teros-cli dir is outside the PWD, and is governed by a separate tsconfig.json file.

I even changed this field to:

  "include": [
    "/Users/alex/codes/teros/notifier-server/src"
  ],
  "exclude": [
    "/Users/alex/codes/teros/teros-cli"
  ]

still get the same error.



Solution 1:[1]

What is rootDir?

rootDir is set to a root folder, that contains all your source files. If not specified, TS will automatically choose a suitable parent folder of all inputs. rootDir also determines the output directory.

What does the error mean?

My guess is you have an import statement for logging.ts somewhere in notifier-server:

import {logger} from "@teros-cli/logging" // or similar

Then logging.ts module will be automatically included by the compiler, regardless of include and exclude options in tsconfig.json. One way to check all included files is tsc --listFiles.

A tsconfig.json file outside notifier-server doesn't help here. The compiler picks up exactly one config per tsc compilation and optionally pulls inherited configs. If it cannot find one in notifier-server project root (where you started tsc), only then the compiler searches upwards the parent directory chain, until a config is found.

Possible solutions

One fix is to just remove "rootDir": "src" from compiler options, so it gets set automatically. Caution: rootDir will then consider both projects as inputs!

Alternative: You can add a separate logging.ts module contained in notifier-server/src project and drop the external import.

Hope, that helps!

Solution 2:[2]

In my case the reason was a circular dependency. So e.g.:

// Page.ts
import { Grid } from "./components/Grid";
export const test = "";

// Grid.ts
import { test } from "../Page"; // circular dep!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergei Basharov
Solution 2 Damiano