From 972462dab75b6d7e012c5a8266d4e4153effa0c9 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Mon, 23 Nov 2026 11:39:59 +0530 Subject: [PATCH 1/3] Fix panic when computing emit signature for fileless diagnostics --- tsc/internal/execute/incremental/snapshot.go | 7 ++- .../execute/incremental/snapshot_test.go | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tsc/internal/execute/incremental/snapshot_test.go diff --git a/tsc/internal/execute/incremental/snapshot.go b/tsc/internal/execute/incremental/snapshot.go index cb0f5c21840c1..935bda311837f 100644 --- a/tsc/internal/execute/incremental/snapshot.go +++ b/tsc/internal/execute/incremental/snapshot.go @@ -410,15 +410,16 @@ func diagnosticToStringBuilder(diagnostic *ast.Diagnostic, file *ast.SourceFile, return } builder.WriteString("\n") - if diagnostic.File() != file { - diagnosticFileName := diagnostic.File().FileName() + diagnosticFile := diagnostic.File() + if diagnosticFile != file && diagnosticFile != nil { + diagnosticFileName := diagnosticFile.FileName() if relativePath, ok := tspath.CaseInsensitive.RelativePathFromFile(file.FileName(), diagnosticFileName); ok { builder.WriteString(relativePath.AsModuleSpecifier().AsString()) } else { builder.WriteString(diagnosticFileName.AsString()) } } - if diagnostic.File() != nil { + if diagnosticFile != nil { builder.WriteString(fmt.Sprintf("(%d,%d): ", diagnostic.Pos(), diagnostic.Len())) } builder.WriteString(diagnostic.Category().Name()) diff --git a/tsc/internal/execute/incremental/snapshot_test.go b/tsc/internal/execute/incremental/snapshot_test.go new file mode 100644 index 0000000000000..8ed104594b769 --- /dev/null +++ b/tsc/internal/execute/incremental/snapshot_test.go @@ -0,0 +1,55 @@ +package incremental + +import ( + "errors" + "strings" + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/ast" + "github.com/microsoft/TypeScript/tsc/internal/bundled" + "github.com/microsoft/TypeScript/tsc/internal/compiler" + "github.com/microsoft/TypeScript/tsc/internal/core" + "github.com/microsoft/TypeScript/tsc/internal/diagnostics" + "github.com/microsoft/TypeScript/tsc/internal/tspath" + "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" + "gotest.tools/v3/assert" +) + +func TestDiagnosticToStringBuilderWithFilelessDiagnostic(t *testing.T) { + t.Parallel() + + // A failed output write is reported as a compiler diagnostic that has no + // source file (see #64875). When computing a declaration file's signature, + // that diagnostic is included alongside diagnostics that do have a file, so + // the formatter must not assume every diagnostic has one. + diagnostic := ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, "dist/a.js", "is a directory") + currentFile := &ast.SourceFile{} + + var builder strings.Builder + diagnosticToStringBuilder(diagnostic, currentFile, &builder) + + 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) { + t.Parallel() + if !bundled.Embedded { + t.Skip("bundled files are not embedded") + } + + fs := bundled.WrapFS(vfstest.FromMap(map[string]string{ + "/tsconfig.json": `{"compilerOptions":{"incremental":true,"declaration":true,"outDir":"dist","rootDir":"."}}`, + "/a.ts": `export const a = 1;`, + }, tspath.CaseSensitive)) + program := buildTestProgram(t, fs) + result := program.Emit(t.Context(), compiler.EmitOptions{ + WriteFile: func(fileName tspath.RootedFilePath, text string, data *compiler.WriteFileData) error { + return errors.New("write failed") + }, + }) + + assert.Assert(t, result != nil) + assert.Assert(t, core.Some(result.Diagnostics, func(diagnostic *ast.Diagnostic) bool { + return diagnostic.Code() == diagnostics.Could_not_write_file_0_Colon_1.Code() + }), "expected a Could not write file diagnostic") +} From 763c65d3786c3274e3eb1ab79bee5bf81d023609 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Tue, 24 Nov 2026 12:01:53 +0530 Subject: [PATCH 2/3] Test failed output writes through the tsc test harness --- .../execute/incremental/snapshot_test.go | 55 --------- .../tsc_incremental_failed_write_test.go | 22 ++++ ...-reports-failed-write-without-panicking.js | 116 ++++++++++++++++++ 3 files changed, 138 insertions(+), 55 deletions(-) delete mode 100644 tsc/internal/execute/incremental/snapshot_test.go create mode 100644 tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go create mode 100644 tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js diff --git a/tsc/internal/execute/incremental/snapshot_test.go b/tsc/internal/execute/incremental/snapshot_test.go deleted file mode 100644 index 8ed104594b769..0000000000000 --- a/tsc/internal/execute/incremental/snapshot_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package incremental - -import ( - "errors" - "strings" - "testing" - - "github.com/microsoft/TypeScript/tsc/internal/ast" - "github.com/microsoft/TypeScript/tsc/internal/bundled" - "github.com/microsoft/TypeScript/tsc/internal/compiler" - "github.com/microsoft/TypeScript/tsc/internal/core" - "github.com/microsoft/TypeScript/tsc/internal/diagnostics" - "github.com/microsoft/TypeScript/tsc/internal/tspath" - "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" - "gotest.tools/v3/assert" -) - -func TestDiagnosticToStringBuilderWithFilelessDiagnostic(t *testing.T) { - t.Parallel() - - // A failed output write is reported as a compiler diagnostic that has no - // source file (see #64875). When computing a declaration file's signature, - // that diagnostic is included alongside diagnostics that do have a file, so - // the formatter must not assume every diagnostic has one. - diagnostic := ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, "dist/a.js", "is a directory") - currentFile := &ast.SourceFile{} - - var builder strings.Builder - diagnosticToStringBuilder(diagnostic, currentFile, &builder) - - 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) { - t.Parallel() - if !bundled.Embedded { - t.Skip("bundled files are not embedded") - } - - fs := bundled.WrapFS(vfstest.FromMap(map[string]string{ - "/tsconfig.json": `{"compilerOptions":{"incremental":true,"declaration":true,"outDir":"dist","rootDir":"."}}`, - "/a.ts": `export const a = 1;`, - }, tspath.CaseSensitive)) - program := buildTestProgram(t, fs) - result := program.Emit(t.Context(), compiler.EmitOptions{ - WriteFile: func(fileName tspath.RootedFilePath, text string, data *compiler.WriteFileData) error { - return errors.New("write failed") - }, - }) - - assert.Assert(t, result != nil) - assert.Assert(t, core.Some(result.Diagnostics, func(diagnostic *ast.Diagnostic) bool { - return diagnostic.Code() == diagnostics.Could_not_write_file_0_Colon_1.Code() - }), "expected a Could not write file diagnostic") -} diff --git a/tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go b/tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go new file mode 100644 index 0000000000000..9c0d5cd1b6531 --- /dev/null +++ b/tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go @@ -0,0 +1,22 @@ +package tsctests + +import ( + "io/fs" + "testing" + "testing/fstest" +) + +func TestTscIncrementalFailedWrite(t *testing.T) { + t.Parallel() + test := &tscInput{ + subScenario: "declaration emit reports failed write without panicking", + files: FileMap{ + "/home/src/workspaces/project/tsconfig.json": `{"compilerOptions":{"incremental":true,"declaration":true,"rootDir":"src","outDir":"dist"}}`, + "/home/src/workspaces/project/src/a.ts": `export const a = 1;`, + // A directory where the JS output goes makes the write fail with TS5033. + "/home/src/workspaces/project/dist/a.js": &fstest.MapFile{Mode: fs.ModeDir | 0o777}, + }, + commandLineArgs: []string{"-p", "."}, + } + test.run(t, "incremental") +} diff --git a/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js b/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js new file mode 100644 index 0000000000000..6591753a33b70 --- /dev/null +++ b/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js @@ -0,0 +1,116 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/project/src/a.ts] *new* +export const a = 1; +//// [/home/src/workspaces/project/tsconfig.json] *new* +{"compilerOptions":{"incremental":true,"declaration":true,"rootDir":"src","outDir":"dist"}} + +tsc -p . +ExitStatus:: DiagnosticsPresent_OutputsGenerated +Output:: +error TS5033: Could not write file '/home/src/workspaces/project/dist/a.js': write "home/src/workspaces/project/dist/a.js": path exists but is not a regular file. + +Found 1 error. + +//// [/home/src/tslibs/TS/Lib/lib.es2026.full.d.ts] *Lib* +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +interface SymbolConstructor { + (desc?: string | number): symbol; + for(name: string): symbol; + readonly toStringTag: symbol; +} +declare var Symbol: SymbolConstructor; +interface Symbol { + readonly [Symbol.toStringTag]: string; +} +declare const console: { log(msg: any): void; }; +//// [/home/src/workspaces/project/dist/a.d.ts] *new* +export declare const a = 1; + +//// [/home/src/workspaces/project/tsconfig.tsbuildinfo] *new* +{"version":"FakeTSVersion","root":[2],"fileNames":["lib.es2026.full.d.ts","./src/a.ts"],"fileInfos":[{"version":"8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true,"impliedNodeFormat":1},{"version":"f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;","signature":"3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n","impliedNodeFormat":1}],"options":{"declaration":true,"outDir":"./dist","rootDir":"./src"},"emitDiagnosticsPerFile":[[2,[{"noFile":true,"pos":-1,"end":-1,"code":5033,"category":1,"messageKey":"Could_not_write_file_0_Colon_1_5033","messageArgs":["/home/src/workspaces/project/dist/a.js","write \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file"]}]]]} +//// [/home/src/workspaces/project/tsconfig.tsbuildinfo.readable.baseline.txt] *new* +{ + "version": "FakeTSVersion", + "root": [ + { + "files": [ + "./src/a.ts" + ], + "original": 2 + } + ], + "fileNames": [ + "lib.es2026.full.d.ts", + "./src/a.ts" + ], + "fileInfos": [ + { + "fileName": "lib.es2026.full.d.ts", + "version": "8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };", + "signature": "8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true, + "impliedNodeFormat": "CommonJS", + "original": { + "version": "8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true, + "impliedNodeFormat": 1 + } + }, + { + "fileName": "./src/a.ts", + "version": "f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;", + "signature": "3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n", + "impliedNodeFormat": "CommonJS", + "original": { + "version": "f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;", + "signature": "3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n", + "impliedNodeFormat": 1 + } + } + ], + "options": { + "declaration": true, + "outDir": "./dist", + "rootDir": "./src" + }, + "emitDiagnosticsPerFile": [ + [ + "./src/a.ts", + [ + { + "noFile": true, + "pos": -1, + "end": -1, + "code": 5033, + "category": 1, + "messageKey": "Could_not_write_file_0_Colon_1_5033", + "messageArgs": [ + "/home/src/workspaces/project/dist/a.js", + "write \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file" + ] + } + ] + ] + ], + "size": 1572 +} + +tsconfig.json:: +SemanticDiagnostics:: +*refresh* /home/src/tslibs/TS/Lib/lib.es2026.full.d.ts +*refresh* /home/src/workspaces/project/src/a.ts +Signatures:: +(stored at emit) /home/src/workspaces/project/src/a.ts From 2bd4c2d77d7cee0c8684a1fa2649dfd9ff41e6d6 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Wed, 25 Nov 2026 22:20:09 +0530 Subject: [PATCH 3/3] Exclude output-write diagnostics from declaration signatures --- tsc/internal/execute/incremental/snapshot.go | 6 ++++++ ...aration-emit-reports-failed-write-without-panicking.js | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tsc/internal/execute/incremental/snapshot.go b/tsc/internal/execute/incremental/snapshot.go index 935bda311837f..7252079d1b91f 100644 --- a/tsc/internal/execute/incremental/snapshot.go +++ b/tsc/internal/execute/incremental/snapshot.go @@ -400,6 +400,12 @@ func (s *snapshot) computeSignatureWithDiagnostics(file *ast.SourceFile, text st var builder strings.Builder builder.WriteString(getTextHandlingSourceMapForSignature(text, data)) for _, diag := range data.Diagnostics { + // A failed output write is not a diagnostic of the declaration's contents. + // Including it would change the signature once a later attempt writes + // successfully, marking unchanged declarations as changed. + if diag.Code() == diagnostics.Could_not_write_file_0_Colon_1.Code() { + continue + } diagnosticToStringBuilder(diag, file, &builder) } return s.computeHash(builder.String()) diff --git a/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js b/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js index 6591753a33b70..845ac3901d4bf 100644 --- a/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js +++ b/tsc/testdata/baselines/reference/tsc/incremental/declaration-emit-reports-failed-write-without-panicking.js @@ -40,7 +40,7 @@ declare const console: { log(msg: any): void; }; export declare const a = 1; //// [/home/src/workspaces/project/tsconfig.tsbuildinfo] *new* -{"version":"FakeTSVersion","root":[2],"fileNames":["lib.es2026.full.d.ts","./src/a.ts"],"fileInfos":[{"version":"8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true,"impliedNodeFormat":1},{"version":"f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;","signature":"3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n","impliedNodeFormat":1}],"options":{"declaration":true,"outDir":"./dist","rootDir":"./src"},"emitDiagnosticsPerFile":[[2,[{"noFile":true,"pos":-1,"end":-1,"code":5033,"category":1,"messageKey":"Could_not_write_file_0_Colon_1_5033","messageArgs":["/home/src/workspaces/project/dist/a.js","write \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file"]}]]]} +{"version":"FakeTSVersion","root":[2],"fileNames":["lib.es2026.full.d.ts","./src/a.ts"],"fileInfos":[{"version":"8859c12c614ce56ba9a18e58384a198f-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ninterface SymbolConstructor {\n (desc?: string | number): symbol;\n for(name: string): symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true,"impliedNodeFormat":1},{"version":"f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;","signature":"67cd7ccc14045107336f34154f76a8ca-export declare const a = 1;\n","impliedNodeFormat":1}],"options":{"declaration":true,"outDir":"./dist","rootDir":"./src"},"emitDiagnosticsPerFile":[[2,[{"noFile":true,"pos":-1,"end":-1,"code":5033,"category":1,"messageKey":"Could_not_write_file_0_Colon_1_5033","messageArgs":["/home/src/workspaces/project/dist/a.js","write \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file"]}]]]} //// [/home/src/workspaces/project/tsconfig.tsbuildinfo.readable.baseline.txt] *new* { "version": "FakeTSVersion", @@ -72,11 +72,11 @@ export declare const a = 1; { "fileName": "./src/a.ts", "version": "f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;", - "signature": "3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n", + "signature": "67cd7ccc14045107336f34154f76a8ca-export declare const a = 1;\n", "impliedNodeFormat": "CommonJS", "original": { "version": "f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;", - "signature": "3c0d6bd71647a06c9b7bfd6cbfdfc05b-export declare const a = 1;\n\nerror5033: Could_not_write_file_0_Colon_1_5033\n/home/src/workspaces/project/dist/a.js\nwrite \"home/src/workspaces/project/dist/a.js\": path exists but is not a regular file\n", + "signature": "67cd7ccc14045107336f34154f76a8ca-export declare const a = 1;\n", "impliedNodeFormat": 1 } } @@ -105,7 +105,7 @@ export declare const a = 1; ] ] ], - "size": 1572 + "size": 1394 } tsconfig.json::