TypeScript: 4.8 hasSameBuildInfo can randomly crash tsc.js with "Cannot read properties of undefined (reading 'path')"

Bug Report

Multiple packages in the monorepo are being built simultaneously when this happens:

// This is where we are building each package to be published.
const results = await pMap(packagePaths, async (packagePath) => buildForNpm({ packagePath, groupName }), {
  // Change to 1 and the problem seems to go away, 
  // but it will take significantly longer to build.
  concurrency: 20 
});

buildForNpm runs tsc --build tsconfig.json --pretty

        if (refBuildInfo.path === buildInfoCacheEntry.path)
                         ^

TypeError: Cannot read properties of undefined (reading 'path')
    at hasSameBuildInfo (/project/node_modules/typescript/lib/tsc.js:105509:26)
    at hasSameBuildInfo (/project/node_modules/typescript/lib/tsc.js:105517:21)
    at getUpToDateStatusWorker (/project/node_modules/typescript/lib/tsc.js:105453:44)
    at getUpToDateStatus (/project/node_modules/typescript/lib/tsc.js:105531:22)
    at getNextInvalidatedProjectCreateInfo (/project/node_modules/typescript/lib/tsc.js:105082:26)
    at getNextInvalidatedProject (/project/node_modules/typescript/lib/tsc.js:105142:20)
    at build (/project/node_modules/typescript/lib/tsc.js:105665:38)
    at Object.build (/project/node_modules/typescript/lib/tsc.js:105863:101)
    at performBuild (/project/node_modules/typescript/lib/tsc.js:106569:73)
    at Object.executeCommandLine (/project/node_modules/typescript/lib/tsc.js:106514:24)

Once the problem happens, the problem will keep happening until the output in cache/outDir is deleted.

The error is thrown here: https://github.com/microsoft/TypeScript/blob/main/src/compiler/tsbuildPublic.ts#L1794

Possibly related to the changes in this PR: https://github.com/microsoft/TypeScript/pull/48784.

This fixes the problem for me. Happy to make a PR if I can figure out how to test it.

-if (refBuildInfo.path === buildInfoCacheEntry.path) return true;
+if (refBuildInfo?.path === buildInfoCacheEntry.path) return true;

🔎 Search Terms

  • Parallel builds.
  • Concurrent builds.
  • race condition.
  • randomly failing to build.

🕗 Version & Regression Information

  • TypeScript 4.8.2
  • This is a crash.
  • This changed between versions 4.7 and 4.8.2

⏯ Playground Link

Not able to reproduce with small projects.

💻 Code

This is happening in a large closed-source project.

🙁 Actual behavior

Crashing during build.

🙂 Expected behavior

No crash.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@dylang thanks for all the info. i am working on the fix. But just so you know /projects/dylang-prs/config/browserslist-config-dylang/tsconfig.json isnt building anything… you mentioned something about javascript file being there but since you dont have allowJs or checkJs it isnt included in the project and because you have “references: []” it is treated as solution config and you arent getting any error about no files found per configuration.

Here is /projects/dylang-prs/config/browserslist-config-dylang/tsconfig.json:

It does not have any dependencies and therefor no references.

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "// Note //": "This file is automatically maintained by dylang.",
  "extends": "@m/dylang-typescript",
  "include": [
    "./src/**/*"
  ],
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "../../.cache/ts/config/browserslist-config-magic",
    "composite": true,
    "tsBuildInfoFile": "../../.cache/ts/config/browserslist-config-magic/tsconfig.tsbuildinfo"
  },
  "references": []
}