确定为什么tsc正在编译一个子目录。

huangapple go评论62阅读模式
英文:

How to determine why tsc is compiling a subdirectory

问题

我有一个项目的结构如下:

project/
client/
src/
client.ts
tsconfig.client.json
src/
index.ts
tsconfig.json


当我在`project`文件夹中运行`tsc -b`时,它尝试编译`client`子目录。我了解到如果子文件夹中有`tsconfig.json`,那么--build将构建该文件夹,但在这种情况下,子目录的tsconfig已被重命名。

结果,tsc输出一个错误:

error TS5083: Cannot read file 'project/client/tsconfig.json'.


我该如何追踪为什么tsc试图加载这个文件夹呢? `client`没有在顶级tsconfig的`references`中列出,而且`include`和`rootDir`选项被设置为仅构建`src`。

我尝试过使用--dry build和--traceResolution,但都没有提供有用的答案。
英文:

I have a project arranged like so:

project/
  client/
    src/
      client.ts
    tsconfig.client.json
  src/
    index.ts
  tsconfig.json

When I run tsc -b in the project folder, it is attempting to compile the client subdirectory as well. Now I understand that if there is a tsconfig.json in a subfolder, then --build will build that folder, but in this case the subdirectory's tsconfig is renamed.

As a result though, tsc outputs an error:

error TS5083: Cannot read file 'project/client/tsconfig.json'.

How can I go about tracing why tsc is trying to load this folder? client is not listed in the references in the top level tsconfig, and the include and rootDir options are set to only build src.

I've tried doing a --dry build and a --traceResolution but neither provide a helpful answer.

Edit to show my tsconfigs:

project/tsconfig.json

{
  "extends": "../../../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "src",
    "target": "es6",
    "lib": ["es2019","dom"],
    "jsx": "react",
    "noUnusedLocals": false
  },
  "include": ["./src/**/*"],
  "references": [
    { "path": "shared/api" },
    { "path": "../../lib/logger" },
  ]
}

../../../tsconfig.base.json

{
    "compilerOptions": {
      "target": "es2020",
      "module": "commonjs",
      "moduleResolution": "node",
      "rootDir": "packages",
      "baseUrl": "./packages",
      "lib": ["es2019"],
  
      "strict": true,
      "noUnusedLocals": true,
      "noFallthroughCasesInSwitch": true,
      "noImplicitReturns": true,
      "esModuleInterop": true,
      "allowJs": true,
  
      "composite": true,
      "inlineSources": true,
      "sourceMap": true,
      "incremental": true,

      "skipLibCheck": true,
      "emitDeclarationOnly": true,
  
      "allowSyntheticDefaultImports": true,
      "experimentalDecorators": true,
  
      "newLine": "lf",
  
      "noEmitOnError": true,
    },
    "exclude": ["**/jest.config.js", "**/dist", "**/__mocks__/**/*", "**/coverage", "**/gulpfile.js"]
}

client/tsconfig.client.json - if it's relevant, since tsc isn't even using this file

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost",
      "webworker"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
  "references": [
    { "path": "../shared/api" },
    { "path": "../" }
  ]
}

答案1

得分: 0

我发现的是,我在使用 npx 运行 tsc 命令。由于我在我的 monorepo 中设置了工作区,子文件夹有一个 package.json,npm 正在检测到并在子文件夹中再次运行 tsc 命令。

将我的 npx 命令更新为 npx --workspaces=false tsc -b 可以解决这个问题。

英文:

What I discovered is that I was running the tsc command using npx. Since I have workspaces set up in my monorepo, the subfolder had a package.json which npm was picking up and running the tsc command a second time in the subfolder.

Updating my npx command to npx --workspaces=false tsc -b fixes the problem.

huangapple
  • 本文由 发表于 2023年2月24日 00:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定