Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tsc/internal/execute/incremental/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {

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.

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())
Expand Down
22 changes: 22 additions & 0 deletions tsc/internal/execute/tsctests/tsc_incremental_failed_write_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -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*
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
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-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\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-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\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-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\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-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\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