Repository navigation
Non-deterministic TS2305 "has no exported member" errors across project references with --emitDeclarationOnly #4262
Description
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-previewversion: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 --emitDeclarationOnlyThe 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
*.tsbuildinfoanddist/, the only outputs). - The
Files:count from--extendedDiagnosticsdiffers 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:
- Include the same set of files in the program.
- Produce the same diagnostics (ideally zero, since the referenced
.d.tsfiles do export the symbols in question).
Additional notes
disableSourceOfProjectReferenceRedirect: trueis set so tsgo should be reading the upstream.d.tsfiles (not their.tssources). We have verified the relevant.d.tsfiles exist, are not empty, and containexport declare const useHookXfor 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'spackage.jsondeclares"main": "dist/index.js","types": "dist/index.d.ts",sideEffects: false. There is noexportsmap. - 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
DeepCloneNodeallocation runaway (44 GB / 308 s) for recursive tagged-tuple type alias TypeScript#63825 (DeepCloneNode allocation runaway) under the sameDomain: Build Modelabel, 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
RyanCavanaugh commentedon Jun 11, 2026
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
ibesuperv commentedon Jun 16, 2026
Potential Cause Identified
We investigated the non-determinism where Files: count and diagnostics (like
TS2305) vary between runs in large monorepos withdisableSourceOfProjectReferenceRedirectset totrue.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.tasksto 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 flakyTS2305errors.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?
jakebailey commentedon Jun 16, 2026
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.
ibesuperv commentedon Jun 16, 2026
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.
eddking commentedon Jul 9, 2026
I hit this on macOS in a large pnpm monorepo and tracked it down. The nondeterminism is in
Realpathon darwin, which resolves viafcntl(F_GETPATH). For a file withnlink > 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 varyingFiles:counts.Fix and details in #4578.
fix: add nativepath realpath darwin patch (typescript-go#4578)
thempatel commentedon Aug 10, 2026
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
jakebailey commentedon Aug 10, 2026
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
thempatel commentedon Aug 11, 2026
thank you so much Jake Bailey (@jakebailey)