Skip to content

Fix panic when computing emit signature for fileless diagnostics - #65635

Open
Shubh (Shubham02022002) wants to merge 3 commits into
microsoft:mainfrom
Shubham02022002:fix/64875-fileless-diagnostic-panic
Open

Shubh (Shubham02022002) wants to merge 3 commits into
microsoft:mainfrom
Shubham02022002:fix/64875-fileless-diagnostic-panic

Conversation

@Shubham02022002

@Shubham02022002 Shubh (Shubham02022002) commented Nov 23, 2026 •

Copy link
Copy Markdown

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.

diagnosticToStringBuilder assumed every diagnostic had a file:

if diagnostic.File() != file {
    diagnosticFileName := diagnostic.File().FileName()
    // ...
}

For a fileless diagnostic, diagnostic.File() is nil and differs from the emitting file, so this dereferenced nil and crashed the compiler. With declaration: true, the JS write failure is included in the declaration emit's diagnostics, so the panic is reachable from a normal tsc -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 when diagnostic.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 tsc test: tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go, with baseline tsc/incremental/declaration-emit-reports-failed-write-without-panicking. The test pre-creates dist/a.js as a directory so the JS output write fails, then runs tsc -p . through execute.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:

  • npx hereby validate
  • npx hereby validate --api (for TypeScript API changes)

Copilot AI balanced review requested due to automatic review settings November 23, 2026 06:10
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 23, 2026
@typescript-automation

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 23, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@Shubham02022002

Copy link
Copy Markdown
Author

@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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is the right kind of test for this issue; a tsctest or similar would be better.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Including output-write diagnostics in declaration signatures can make unchanged declarations appear changed after later edits, causing unnecessary importer rechecks and re-emits.

1 open issue

Review effort: Max

if diagnostic.File() != file {
diagnosticFileName := diagnostic.File().FileName()
diagnosticFile := diagnostic.File()
if diagnosticFile != file && diagnosticFile != nil {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

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

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Incremental compile panics on a failed output write, or never retries it

3 participants