Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions tsc/internal/execute/incremental/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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
55 changes: 55 additions & 0 deletions tsc/internal/execute/incremental/snapshot_test.go
Original file line number Diff line number Diff line change
@@ -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) {

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.

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")
}