diff --git a/tsc/internal/execute/incremental/snapshot.go b/tsc/internal/execute/incremental/snapshot.go index cb0f5c21840c1..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()) @@ -410,15 +416,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/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..845ac3901d4bf --- /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":"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", + "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": "67cd7ccc14045107336f34154f76a8ca-export declare const a = 1;\n", + "impliedNodeFormat": "CommonJS", + "original": { + "version": "f5c8fff6e1fca35f4a292d48868d4086-export const a = 1;", + "signature": "67cd7ccc14045107336f34154f76a8ca-export declare const a = 1;\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": 1394 +} + +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