Repository navigation
Fix panic when computing emit signature for fileless diagnostics - #65635
Shubh (Shubham02022002) wants to merge 3 commits into
Conversation
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change addressing the incremental declaration-emit panic.
No open issues 🎉
What changed in this PR
Fixes the incremental declaration-emit panic from #64875 when a failed output write produces a diagnostic without a source file. Retrying failed writes remains outside this PR’s scope.
Changes:
- Guards filename formatting against fileless diagnostics while preserving their diagnostic details.
- Adds regression tests for direct formatting and incremental emit with a failing write callback.
| File | Description |
|---|---|
tsc/internal/execute/incremental/snapshot.go |
Adds a nil-file guard when formatting diagnostics for emit signatures. |
tsc/internal/execute/incremental/snapshot_test.go |
Tests fileless diagnostic formatting and failed-write emit behavior. |
Review effort: Balanced
|
@microsoft-github-policy-service agree |
| assert.Equal(t, builder.String(), "\nerror5033: Could_not_write_file_0_Colon_1_5033\ndist/a.js\nis a directory\n") | ||
| } | ||
|
|
||
| func TestEmitWithFailingWriteFileDoesNotPanic(t *testing.T) { |
There was a problem hiding this comment.
I do not think this is the right kind of test for this issue; a tsctest or similar would be better.
There was a problem hiding this comment.
Good point — replaced the Go-level tests with a tsc test: tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go (baseline tsc/incremental/declaration-emit-reports-failed-write-without-panicking). It pre-creates dist/a.js as a directory so the output write fails, then runs tsc -p . through execute.CommandLine, so it covers the real incremental emit path. I confirmed it panics without the guard and passes with it; npx hereby validate is green. snapshot_test.go is gone.
| if diagnostic.File() != file { | ||
| diagnosticFileName := diagnostic.File().FileName() | ||
| diagnosticFile := diagnostic.File() | ||
| if diagnosticFile != file && diagnosticFile != nil { |
There was a problem hiding this comment.
Good catch — kept the nil guard and excluded TS5033 from computeSignatureWithDiagnostics, so a failed write no longer affects the stored declaration signature. Regenerated the baseline; the signature is now just the d.ts hash. npx hereby validate passes.

Fixes #64875
Note
This PR fixes the panic reported in the issue. The separate "failed write is never retried" behavior described there is not changed here; maintainers may want to reopen or split that part out.
Analysis
When incremental emit computes a declaration file's signature, it hashes the emitted text together with the diagnostics collected for that emit (
computeSignatureWithDiagnostics). If an earlier output write fails, the emitter records TS5033 ("Could not write file ...") in its diagnostic collection, and that diagnostic has no source file.diagnosticToStringBuilderassumed every diagnostic had a file:For a fileless diagnostic,
diagnostic.File()is nil and differs from the emitting file, so this dereferenced nil and crashed the compiler. Withdeclaration: true, the JS write failure is included in the declaration emit's diagnostics, so the panic is reachable from a normaltsc -p .run.Repro (from the issue):
{ "compilerOptions": { "incremental": true, "declaration": true, "rootDir": "src", "outDir": "dist" } }mkdir -p dist/a.js tsc -p .Before this change this panics at
tsc/internal/execute/incremental/snapshot.go:414; after this change it reports TS5033 and exits with an error.Fix
Handle fileless diagnostics in
diagnosticToStringBuilder: skip the file-name prefix whendiagnostic.File()is nil, while still writing the category, code, message key, and message arguments. Diagnostics that do have a file keep their existing filename/position output; the position suffix was already guarded on a nil file, and this extends the same guard to the filename prefix.Output-write diagnostics (TS5033) are also excluded from
computeSignatureWithDiagnostics, so a failed write does not become part of the declaration's stored signature. Without that, a later successful write would change the signature and make unchanged declarations look changed to downstream projects. The nil guard remains as defense in depth for any other fileless diagnostic that reaches the formatter.Regression coverage is a
tsctest:tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go, with baselinetsc/incremental/declaration-emit-reports-failed-write-without-panicking. The test pre-createsdist/a.jsas a directory so the JS output write fails, then runstsc -p .throughexecute.CommandLine, exercising the real incremental emit path. It panics without the guard and produces the expected TS5033 baseline with it.The remaining behavior in #64875 — a failed write is never retried on the next incremental run — is separate and is not changed here.
Copilot Checklist
I successfully ran the applicable command at the end of my session, and it completed without error: