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

北城以北 提交于 2019-12-11 16:38:25

问题


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.


回答1:


My guess is that somewhere in your project /Users/alex/codes/teros/notifier-server you have an import statement for logging.ts, like

import {xxx} from "../my/path/logging"

Then logging.ts module will be automatically included by the compiler, regardless of your include and exclude options in the tsconfig.json. See the TypeScript FAQ here. It makes no difference, that you have another tsconfig.json outside your project path. When you compile with tsc from your project directory, the compiler will only consider this one tsconfig.json. If there would be no config file, then only it would search upwards the parent directory, until it finds one (see here).

To lead over to your rootDir error:

rootDir must be set to a folder that is the root directory of all your input files (compiler options). In your case, you have "rootDir": "src". And '/Users/alex/codes/interos/teros-cli/src/logging.ts' is outside /Users/alex/codes/teros/notifier-server/src. See this post for more infos how rootDir works.

Your solution depends on how you want to integrate logging.ts with your project. A simple fix would be to just remove "rootDir": "src" from compiler options. If not set, rootDir is computed from the list of all input files automatically (compiler options). You can find out all included files by tsc --listFiles.

Hope, that helps.



来源:https://stackoverflow.com/questions/57422458/error-ts6059-file-is-not-under-rootdir-rootdir-is-expected-to-contain-al

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!