Repository navigation
Fix panic when computing emit signature for fileless diagnostics #65635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point — replaced the Go-level tests with a |
||
| 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") | ||
| } | ||
There was a problem hiding this comment.
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 validatepasses.