Skip to content

Commit feb866d

Browse files
authored
Fix double-wrapped compiler errors emitting spurious file:1:1: prefix (#24316)
1 parent 6a540f0 commit feb866d

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

pkg/parser/import_error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ type FormattedParserError struct {
8484
func (e *FormattedParserError) Error() string { return e.formatted }
8585
func (e *FormattedParserError) Unwrap() error { return e.cause }
8686

87+
// NewFormattedParserError creates a FormattedParserError with the given pre-formatted
88+
// message string. Use this in external packages (e.g. pkg/workflow) to return an error
89+
// that isFormattedCompilerError can detect without double-wrapping.
90+
func NewFormattedParserError(formatted string) *FormattedParserError {
91+
return &FormattedParserError{formatted: formatted}
92+
}
93+
8794
// FormatImportError formats an import error as a compilation error with source location
8895
func FormatImportError(err *ImportError, yamlContent string) error {
8996
importErrorLog.Printf("Formatting import error: path=%s, file=%s, line=%d", err.ImportPath, err.FilePath, err.Line)

pkg/parser/schema_compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func validateWithSchemaAndLocation(frontmatter map[string]any, schemaJSON, conte
354354

355355
// Format and return the error
356356
formattedErr := console.FormatError(compilerErr)
357-
return errors.New(formattedErr)
357+
return &FormattedParserError{formatted: formattedErr}
358358
}
359359
}
360360

@@ -382,7 +382,7 @@ func validateWithSchemaAndLocation(frontmatter map[string]any, schemaJSON, conte
382382

383383
// Format and return the error
384384
formattedErr := console.FormatError(compilerErr)
385-
return errors.New(formattedErr)
385+
return &FormattedParserError{formatted: formattedErr}
386386
}
387387

388388
// Fallback to the original error if we can't format it nicely

pkg/workflow/compiler_error_formatter_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func TestIsFormattedCompilerError(t *testing.T) {
4545
}, "imports:\n - missing.md"),
4646
expected: true,
4747
},
48+
{
49+
name: "error from parser.NewFormattedParserError is detected as formatted",
50+
err: parser.NewFormattedParserError("workflow.md:5:3: error: bad value"),
51+
expected: true,
52+
},
4853
{
4954
name: "plain error is not formatted",
5055
err: errors.New("plain error"),

pkg/workflow/frontmatter_error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ func (c *Compiler) createFrontmatterError(filePath, content string, err error, f
7373
context := errorStr[loc[0]+1:] // +1 to skip the leading newline
7474
// Return VSCode-compatible format on first line, followed by source context only
7575
frontmatterErrorLog.Print("Formatting error for VSCode compatibility")
76-
return fmt.Errorf("%s\n%s", vscodeFormat, context)
76+
return parser.NewFormattedParserError(fmt.Sprintf("%s\n%s", vscodeFormat, context))
7777
}
7878

7979
// If we can't extract source context, return just the VSCode format
80-
return fmt.Errorf("%s", vscodeFormat)
80+
return parser.NewFormattedParserError(vscodeFormat)
8181
}
8282

8383
// Fallback if we can't parse the line/col

0 commit comments

Comments
 (0)