Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...