Skip to content

Commit 2a95b68

Browse files
refactor: simplify fastParseTitle state machine and deduplicate formatCompilerError (#21050)
1 parent 13fd240 commit 2a95b68

2 files changed

Lines changed: 8 additions & 24 deletions

File tree

pkg/cli/workflows.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ func getMarkdownWorkflowFiles(workflowDir string) ([]string, error) {
292292
func fastParseTitle(content string) (string, error) {
293293
firstLine := true
294294
inFrontmatter := false
295-
pastFrontmatter := false
296295
for line := range strings.SplitSeq(content, "\n") {
297296
trimmed := strings.TrimSpace(line)
298297
if firstLine {
@@ -301,21 +300,19 @@ func fastParseTitle(content string) (string, error) {
301300
inFrontmatter = true
302301
continue
303302
}
304-
// No frontmatter on first line; treat the entire file as markdown.
305-
pastFrontmatter = true
306-
} else if inFrontmatter && !pastFrontmatter {
303+
} else if inFrontmatter {
307304
if trimmed == "---" {
308-
pastFrontmatter = true
305+
inFrontmatter = false
309306
}
310307
continue
311308
}
312-
if pastFrontmatter && strings.HasPrefix(trimmed, "# ") {
309+
if strings.HasPrefix(trimmed, "# ") {
313310
return strings.TrimSpace(trimmed[2:]), nil
314311
}
315312
}
316313

317314
// Unclosed frontmatter is an error (consistent with ExtractFrontmatterFromContent).
318-
if inFrontmatter && !pastFrontmatter {
315+
if inFrontmatter {
319316
return "", errors.New("frontmatter not properly closed")
320317
}
321318

pkg/workflow/compiler_error_formatter.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,16 @@ type wrappedCompilerError struct {
2020
func (e *wrappedCompilerError) Error() string { return e.formatted }
2121
func (e *wrappedCompilerError) Unwrap() error { return e.cause }
2222

23-
// formatCompilerError creates a formatted compiler error message with optional error wrapping.
24-
// It always uses line:1, column:1 so IDE tooling can navigate to the file even when a
25-
// specific source position is unavailable.
23+
// formatCompilerError creates a formatted compiler error at line 1, column 1.
24+
// Use this when the exact source position is unknown; IDE tooling can still navigate to the file.
25+
// Use formatCompilerErrorWithPosition when a specific line/column is available.
2626
//
2727
// filePath: the file path to include in the error (typically markdownPath or lockFile)
2828
// errType: the error type ("error" or "warning")
2929
// message: the error message text
3030
// cause: optional underlying error to wrap (use nil for validation errors)
3131
func formatCompilerError(filePath string, errType string, message string, cause error) error {
32-
compilerErrorLog.Printf("Formatting compiler error: file=%s, type=%s, message=%s", filePath, errType, message)
33-
formattedErr := console.FormatError(console.CompilerError{
34-
Position: console.ErrorPosition{
35-
File: filePath,
36-
Line: 1,
37-
Column: 1,
38-
},
39-
Type: errType,
40-
Message: message,
41-
})
42-
43-
// Always return a *wrappedCompilerError so isFormattedCompilerError can detect it.
44-
// cause may be nil for validation errors that have no underlying cause.
45-
return &wrappedCompilerError{formatted: formattedErr, cause: cause}
32+
return formatCompilerErrorWithPosition(filePath, 1, 1, errType, message, cause)
4633
}
4734

4835
// isFormattedCompilerError reports whether err is already a console-formatted compiler error

0 commit comments

Comments
 (0)