Skip to content

Commit cd55d81

Browse files
authored
remove duplicate parse (#2431)
1 parent 1f820e7 commit cd55d81

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

pkg/cli/compile_command.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,37 @@ func CompileWorkflowWithValidation(compiler *workflow.Compiler, filePath string,
4949
return nil
5050
}
5151

52+
// CompileWorkflowDataWithValidation compiles from already-parsed WorkflowData with validation
53+
// This avoids re-parsing when the workflow data has already been parsed
54+
func CompileWorkflowDataWithValidation(compiler *workflow.Compiler, workflowData *workflow.WorkflowData, filePath string, verbose bool) error {
55+
// Compile the workflow using already-parsed data
56+
if err := compiler.CompileWorkflowData(workflowData, filePath); err != nil {
57+
return err
58+
}
59+
60+
// Always validate that the generated lock file is valid YAML (CLI requirement)
61+
lockFile := strings.TrimSuffix(filePath, ".md") + ".lock.yml"
62+
if _, err := os.Stat(lockFile); err != nil {
63+
// Lock file doesn't exist (likely due to no-emit), skip YAML validation
64+
return nil
65+
}
66+
67+
compileLog.Print("Validating generated lock file YAML syntax")
68+
69+
lockContent, err := os.ReadFile(lockFile)
70+
if err != nil {
71+
return fmt.Errorf("failed to read generated lock file for validation: %w", err)
72+
}
73+
74+
// Validate the lock file is valid YAML
75+
var yamlValidationTest any
76+
if err := yaml.Unmarshal(lockContent, &yamlValidationTest); err != nil {
77+
return fmt.Errorf("generated lock file is not valid YAML: %w", err)
78+
}
79+
80+
return nil
81+
}
82+
5283
// CompileConfig holds configuration options for compiling workflows
5384
type CompileConfig struct {
5485
MarkdownFiles []string // Files to compile (empty for all files)
@@ -202,7 +233,7 @@ func CompileWorkflows(config CompileConfig) ([]*workflow.WorkflowData, error) {
202233
workflowDataList = append(workflowDataList, workflowData)
203234

204235
compileLog.Printf("Starting compilation of %s", resolvedFile)
205-
if err := CompileWorkflowWithValidation(compiler, resolvedFile, verbose); err != nil {
236+
if err := CompileWorkflowDataWithValidation(compiler, workflowData, resolvedFile, verbose); err != nil {
206237
// Always put error on a new line and don't wrap with "failed to compile workflow"
207238
fmt.Fprintln(os.Stderr, err.Error())
208239
errorMessages = append(errorMessages, err.Error())
@@ -343,7 +374,7 @@ func CompileWorkflows(config CompileConfig) ([]*workflow.WorkflowData, error) {
343374
}
344375
workflowDataList = append(workflowDataList, workflowData)
345376

346-
if err := CompileWorkflowWithValidation(compiler, file, verbose); err != nil {
377+
if err := CompileWorkflowDataWithValidation(compiler, workflowData, file, verbose); err != nil {
347378
// Print the error to stderr (errors from CompileWorkflow are already formatted)
348379
fmt.Fprintln(os.Stderr, err.Error())
349380
errorCount++

pkg/workflow/compiler.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,6 @@ type SafeOutputsConfig struct {
221221

222222
// CompileWorkflow converts a markdown workflow to GitHub Actions YAML
223223
func (c *Compiler) CompileWorkflow(markdownPath string) error {
224-
225-
// Reset the step order tracker for this compilation
226-
c.stepOrderTracker = NewStepOrderTracker()
227-
228-
// replace the .md extension by .lock.yml
229-
lockFile := strings.TrimSuffix(markdownPath, ".md") + ".lock.yml"
230-
231-
log.Printf("Starting compilation: %s -> %s", markdownPath, lockFile)
232-
233224
// Parse the markdown file
234225
log.Printf("Parsing workflow file")
235226
workflowData, err := c.ParseWorkflowFile(markdownPath)
@@ -252,6 +243,21 @@ func (c *Compiler) CompileWorkflow(markdownPath string) error {
252243
return errors.New(formattedErr)
253244
}
254245

246+
return c.CompileWorkflowData(workflowData, markdownPath)
247+
}
248+
249+
// CompileWorkflowData compiles a workflow from already-parsed WorkflowData
250+
// This avoids re-parsing when the data has already been parsed
251+
func (c *Compiler) CompileWorkflowData(workflowData *WorkflowData, markdownPath string) error {
252+
253+
// Reset the step order tracker for this compilation
254+
c.stepOrderTracker = NewStepOrderTracker()
255+
256+
// replace the .md extension by .lock.yml
257+
lockFile := strings.TrimSuffix(markdownPath, ".md") + ".lock.yml"
258+
259+
log.Printf("Starting compilation: %s -> %s", markdownPath, lockFile)
260+
255261
// Validate expression safety - check that all GitHub Actions expressions are in the allowed list
256262
log.Printf("Validating expression safety")
257263
if err := validateExpressionSafety(workflowData.MarkdownContent); err != nil {

0 commit comments

Comments
 (0)