78

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.

4 Answers4

70

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!

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
ford04
  • 66,267
  • 20
  • 199
  • 171
  • 11
    Another option is to use Project References. See [this answer](https://stackoverflow.com/a/61467483/8910547) for dealing with `rootDir` issues and using Project Reference to manage interal dependencies. Things will start to make a lot more sense. – Inigo Apr 29 '20 at 01:29
5

I got this error by duplicating paths in tsconfigs. Removing the latter solved the issue:

// ./tsConfig.json
"paths": {
  "@myPackage": ["./myPackage/index.ts"],
},

// ./MY_OTHER_PACKAGE/tsConfig.json
"paths": {
 "@myPackage": ["../myPackage/index.ts"]
},
DamianoPantani
  • 1,168
  • 2
  • 13
  • 23
0
 -src
     index.ts
 -tsconfig.json

If in tsconfig.json its "rootDir": "./src" & index.ts is in root it doesn't work, make sure index.ts is inside src

Bucket
  • 7,415
  • 9
  • 35
  • 45
0

My solution is for angular libraries. For external libraries, I added the paths definition to the tsconfig.lib.json file.

tsconfig.lib.json

{
  "extends": "../../../tsconfig.json",
  "compilerOptions": {
    "resolveJsonModule": true,
    "outDir": "../../../out-tsc/lib",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": [],
    "paths": {
      "@tbtk/popover": ["dist/tbtk/popover"]
    }
  },
  "exclude": ["**/*.spec.ts"]
}
Serkan KONAKCI
  • 1,100
  • 11
  • 16