Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Non-deterministic TS2305 "has no exported member" errors across project references with --emitDeclarationOnly #4262

Closed

Description

@benkeen

The following is an AI-generated analysis of a build problem I've been seeing locally.


🔎 Search Terms

non-deterministic, flaky, project references, TS2305, has no exported member, disableSourceOfProjectReferenceRedirect, emitDeclarationOnly, composite, isolatedDeclarations, build mode

🕗 Version & Regression Information

  • @typescript/native-preview version: 7.0.0-dev.20260603.1
  • Platform: macOS (Apple Silicon), Node 22
  • Build tool: pnpm + Rush monorepo
  • Has always reproduced for us with current tsgo preview; we have not yet compared against classic tsc.

⏯ Repro

In a pnpm monorepo (~250 projects), each package builds with:

tsgo -p tsconfig.json --emitDeclarationOnly

The leaf package being compiled (pkg-leaf) has references to ~50 sibling packages via project references. The shared tsconfig extended by every package sets:

{
  "compilerOptions": {
    "composite": true,
    "incremental": true,
    "declaration": true,
    "declarationMap": true,
    "isolatedDeclarations": true,
    "isolatedModules": true,
    "disableReferencedProjectLoad": true,
    "disableSourceOfProjectReferenceRedirect": true,
    "moduleResolution": "bundler",
    "module": "esnext",
    "target": "ES2019",
    "verbatimModuleSyntax": true,
    "skipLibCheck": true
  },
  "tsBuildInfoFile": "./temp/tsconfig.tsbuildinfo"
}

The upstream package (pkg-upstream) has already been fully built — its dist/**/*.d.ts files are present and contain every symbol the consumer imports (verified by grep against the on-disk .d.ts).

Run the same command twice back-to-back, deleting build artifacts between runs so every run is a fresh cold compile:

# Run 1
rm -f *.tsbuildinfo && rm -rf dist
npx tsgo -p tsconfig.json --emitDeclarationOnly --extendedDiagnostics

# Run 2 (identical commands, immediately after)
rm -f *.tsbuildinfo && rm -rf dist
npx tsgo -p tsconfig.json --emitDeclarationOnly --extendedDiagnostics

🙁 Actual behavior

Each run produces a different number of TS2305 errors against valid symbols, and includes a different set of files in the program.

Three observed runs on identical on-disk source:

Run Errors Files Symbols Types Instantiations Memory allocs Total time
A 35 35280 6010187 939869 2795852 49066782 19.9s
B 19 35287 6007386 943871 2765531 52672231 22.0s
C (earlier) 0 — — — — — —

Sample errors (all symbols verified present in upstream's dist/<subpath>.d.ts):

src/featureA/ComponentA.tsx(3,10): error TS2305:
  Module '"@scope/pkg-upstream/dist/featureA"' has no exported member 'useHookA'.

src/featureB/hooks/useHookB.ts(3,10): error TS2305:
  Module '"@scope/pkg-upstream/dist/featureB"' has no exported member 'useHookB'.

We also occasionally see TS2724 with a hallucinated "Did you mean ...?" suggestion that names another symbol also exported from the same .d.ts:

src/featureC/ComponentC.spec.tsx(4,10): error TS2724:
  '"@scope/pkg-upstream/dist/featureC"' has no exported member named 'useHookC'.
  Did you mean 'useHookCAlt'?

— but useHookC and useHookCAlt are both export declare const ... in pkg-upstream/dist/featureC.d.ts. Run again and either symbol may flip to "missing" or both may resolve fine.

Crucially:

  • The errors are different files / different symbols / different upstream subpaths every run.
  • Nothing on disk changed between runs (every run nukes *.tsbuildinfo and dist/, the only outputs).
  • The Files: count from --extendedDiagnostics differs across runs (35280 vs 35287), indicating the set of files included in the program is itself non-deterministic.

🙂 Expected behavior

Two identical invocations of tsgo on identical on-disk source should:

  1. Include the same set of files in the program.
  2. Produce the same diagnostics (ideally zero, since the referenced .d.ts files do export the symbols in question).

Additional notes

  • disableSourceOfProjectReferenceRedirect: true is set so tsgo should be reading the upstream .d.ts files (not their .ts sources). We have verified the relevant .d.ts files exist, are not empty, and contain export declare const useHookX for every "missing" symbol.
  • The errors cluster around imports from @scope/pkg-upstream/dist/<subpath> form (i.e. deep subpath imports, not the package root). The upstream package's package.json declares "main": "dist/index.js", "types": "dist/index.d.ts", sideEffects: false. There is no exports map.
  • We have not been able to repro outside our monorepo yet — happy to put effort into a minimal standalone repro if that would help. Wanted to file this first since the symptom (non-deterministic program construction across project references) seems severe enough to be worth surfacing on its own.
  • Closest existing open issue we could find is DeepCloneNode allocation runaway (44 GB / 308 s) for recursive tagged-tuple type alias TypeScript#63825 (DeepCloneNode allocation runaway) under the same Domain: Build Mode label, but the symptom is different.

Let me know what additional diagnostic flags (e.g. --traceResolution, --listFiles, build-info dumps) would be most useful and we can capture and attach them.

Activity

  1. RyanCavanaugh commented on Jun 11, 2026

    @RyanCavanaugh
    Member

    This is definitely a bug, but we'll almost certainly need a repro to diagnose. AI can generally reduce these things down to a minimal shareable repro if you ask; see instructions or re-use this skill: https://github.com/microsoft/TypeScript-Maintainer-Skills/blob/main/.github/skills/ts-maintain-reduce-repro/SKILL.md

  2. ibesuperv commented on Jun 16, 2026

    @ibesuperv
    Contributor

    This is definitely a bug, but we'll almost certainly need a repro to diagnose. AI can generally reduce these things down to a minimal shareable repro if you ask; see instructions or re-use this skill: https://github.com/microsoft/TypeScript-Maintainer-Skills/blob/main/.github/skills/ts-maintain-reduce-repro/SKILL.md

    Potential Cause Identified

    We investigated the non-determinism where Files: count and diagnostics (like TS2305) vary between runs in large monorepos with disableSourceOfProjectReferenceRedirect set to true.

    The issue appears to stem from how Go parallelizes file parsing using tasks. In internal/compiler/filesparser.go, the parsed tasks are stored in a standard Go map (data.tasks).

    Since Go map iteration is randomized and non-deterministic, iterating over data.tasks to start file parsing/collection introduces randomness in the order in which files and their dependencies (especially subpath imports and project reference tasks) are queued. When multiple files have dependency hierarchies or case-variant paths, this non-deterministic queueing order causes the build graph to resolve differently across runs, resulting in varying file counts and flaky TS2305 errors.


    Proposed Fix

    We can resolve this by sorting the task keys (e.g., file paths) before launching the worker routines in filesParser.start, ensuring that tasks are queued and processed in a stable, deterministic order.


    Would you like us to submit a PR with this deterministic sorting fix?

  3. jakebailey commented on Jun 16, 2026

    @jakebailey
    Member

    The order of task launches should not matter. Starting the sorted will just cause them to be randomized once they get going on another goroutine.

    If that causes a problem, then that's a bug, yes, but we need a test.

  4. ibesuperv commented on Jun 16, 2026

    @ibesuperv
    Contributor

    Thanks Jake Bailey (@jakebailey)! You're right that goroutine scheduling adds its own layer of non-determinism. The sorting specifically targets the iteration over casing variants within the same data.tasks map (inside the locked section), where different iteration orders cause different casing to propagate down the import graph.

    I'll work on creating a minimal test case that demonstrates the non-determinism — likely a multi-file setup with mixed-casing imports where running the compilation multiple times produces different diagnostics or file counts. Will follow up here once I have something reproducible.

  5. eddking commented on Jul 9, 2026

    @eddking

    I hit this on macOS in a large pnpm monorepo and tracked it down. The nondeterminism is in Realpath on darwin, which resolves via fcntl(F_GETPATH). For a file with nlink > 1, a concurrent open of a hardlink sibling can make the kernel return the sibling's path instead of the queried one. pnpm and other content addressed stores hardlink-dedupe identical files, so under parallel file loading module resolution intermittently returns a different file's path, hence the phantom TS2305s naming the wrong module and the varying Files: counts.

    Fix and details in #4578.

  6. thempatel commented on Aug 10, 2026

    @thempatel

    Hey team! Any updates on this one? It's blocking us from being able to upgrade to the new compiler, specifically the non determinism that Edmund King (@eddking) brought up for mac osx. Our team is regularly working on multiple work trees and being able to config the pnpm store to use hardlinks is a huge space saver

  7. jakebailey commented on Aug 10, 2026

    @jakebailey
    Member

    The discussion was on #4578 but I think that is a dead end; I'll just send another change that kills the fast path I added but that turned out to not work

  8. thempatel commented on Aug 11, 2026

    @thempatel

    thank you so much Jake Bailey (@jakebailey)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More Infobug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions