错误 TS6059:文件不在“rootDir"下..“rootDir"应包含所有源文件 [英] error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files

查看:175
本文介绍了错误 TS6059:文件不在“rootDir"下..“rootDir"应包含所有源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这个相当无意义的 tsc 转译错误:

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

错误 TS6059:文件'/Users/alex/codes/interos/teros-cli/src/logging.ts' 不在'rootDir' '/Users/alex/codes/teros/notifier-server/src'.'根目录'预计包含所有源文件.

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.

我的 PWD 是 /Users/alex/codes/teros/notifier-server/Users/alex/codes/teros/notifier-server/tsconfig 的 tsconfig.json 文件.json 是:

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"
  ]
}

这似乎是一个错误..因为 teros-cli 目录在 PWD 之外,并且由单独的 tsconfig.json 文件管理.

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"
  ]

仍然出现同样的错误.

推荐答案

什么是 rootDir?

rootDir 设置为根文件夹,其中包含所有您的源文件.如果未指定,TS 将自动为所有输入选择一个合适的父文件夹.rootDir确定输出目录.

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.

我猜你在 notifier-server 的某处有一个 import 语句用于 logging.ts:

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

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

然后 logging.ts 模块将是 自动被编译器包含,不管tsconfig.json 中的 include 和 exclude 选项.检查所有包含文件的一种方法是 tsc --listFiles.

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.

notifier-server 之外的 tsconfig.json 文件在这里没有帮助.编译器在每次 tsc 编译时只选取一个配置,并可选择拉取 继承配置.如果在 notifier-server 项目根目录(您启动 tsc 的地方)中找不到,则只有编译器 向上搜索父目录链,直到找到一个配置.

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.

一种解决方法是从编译器选项中删除 "rootDir":"src",这样它就会自动设置.注意:rootDir 会将两个项目都视为输入!

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!

替代方案:您可以添加包含在 notifier-server/src 项目中的单独 logging.ts 模块并删除外部 import.

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

希望有帮助!

这篇关于错误 TS6059:文件不在“rootDir"下..“rootDir"应包含所有源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆