Repository navigation
fix(linter): keep node-level lint fixes when the fixed document is written #281
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
Open
RaphaelFakhri
wants to merge
1
commit into
speakeasy-api:main
Choose a base branch
from
RaphaelFakhri:fix/lint-node-fixes-lost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ package fix | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| "github.com/speakeasy-api/openapi/marshaller" | ||
| "github.com/speakeasy-api/openapi/openapi" | ||
| "github.com/speakeasy-api/openapi/validation" | ||
| "go.yaml.in/yaml/v3" | ||
|
|
@@ -158,7 +160,9 @@ func (e *Engine) ProcessErrors(ctx context.Context, doc *openapi.OpenAPI, errs [ | |
| result := &Result{} | ||
| modified := make(map[conflictKey]bool) | ||
| skippedRules := make(map[string]bool) | ||
| var nodeFixes []pendingNodeFix | ||
|
|
||
| fixLoop: | ||
| for _, fe := range fixable { | ||
| fix := fe.fix | ||
| vErr := fe.vErr | ||
|
|
@@ -225,7 +229,7 @@ func (e *Engine) ProcessErrors(ctx context.Context, doc *openapi.OpenAPI, errs [ | |
| continue | ||
| } | ||
| if errors.Is(err, validation.ErrExitInteractive) { | ||
| return result, nil | ||
| break fixLoop | ||
| } | ||
| result.Failed = append(result.Failed, FailedFix{ | ||
| Error: vErr, Fix: fix, FixError: err, | ||
|
|
@@ -241,19 +245,20 @@ func (e *Engine) ProcessErrors(ctx context.Context, doc *openapi.OpenAPI, errs [ | |
| } | ||
| } | ||
|
|
||
| // Apply the fix | ||
| var applyErr error | ||
| if nodeFix, ok := fix.(validation.NodeFix); ok { | ||
| rootNode := doc.GetRootNode() | ||
| if rootNode != nil { | ||
| applyErr = nodeFix.ApplyNode(rootNode) | ||
| } else { | ||
| applyErr = fix.Apply(doc) | ||
| // Node fixes are deferred until all model fixes have been applied, as | ||
| // marshalling syncs the model back over the YAML nodes. | ||
| if nodeFix, ok := fix.(validation.NodeFix); ok && doc.GetRootNode() != nil { | ||
| if key.Line >= 0 { | ||
| modified[key] = true | ||
| } | ||
| } else { | ||
| applyErr = fix.Apply(doc) | ||
| nodeFixes = append(nodeFixes, pendingNodeFix{vErr: vErr, fix: nodeFix, index: len(result.Applied)}) | ||
| result.Applied = append(result.Applied, makeAppliedFix(vErr, fix)) | ||
| continue | ||
| } | ||
|
|
||
| // Apply the fix | ||
| applyErr := fix.Apply(doc) | ||
|
|
||
| if applyErr != nil { | ||
| result.Failed = append(result.Failed, FailedFix{ | ||
| Error: vErr, Fix: fix, FixError: applyErr, | ||
|
|
@@ -269,9 +274,67 @@ func (e *Engine) ProcessErrors(ctx context.Context, doc *openapi.OpenAPI, errs [ | |
| result.Applied = append(result.Applied, makeAppliedFix(vErr, fix)) | ||
| } | ||
|
|
||
| if err := applyNodeFixes(ctx, doc, result, nodeFixes); err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| type pendingNodeFix struct { | ||
| vErr *validation.Error | ||
| fix validation.NodeFix | ||
| index int // position of the fix in Result.Applied | ||
| } | ||
|
|
||
| // applyNodeFixes applies fixes that edit the YAML node tree directly. The model | ||
| // is synced to the nodes first so earlier model fixes are kept, and the model is | ||
| // rebuilt from the edited nodes afterwards so the edits survive marshalling. | ||
| func applyNodeFixes(ctx context.Context, doc *openapi.OpenAPI, result *Result, nodeFixes []pendingNodeFix) error { | ||
| if len(nodeFixes) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| rootNode, err := marshaller.Sync(ctx, doc) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to sync document before applying node fixes: %w", err) | ||
| } | ||
|
|
||
| failed := make(map[int]bool) | ||
| for _, nf := range nodeFixes { | ||
| if err := nf.fix.ApplyNode(rootNode); err != nil { | ||
| failed[nf.index] = true | ||
| result.Failed = append(result.Failed, FailedFix{ | ||
| Error: nf.vErr, Fix: nf.fix, FixError: err, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if len(failed) > 0 { | ||
| applied := make([]AppliedFix, 0, len(result.Applied)-len(failed)) | ||
| for i, af := range result.Applied { | ||
| if !failed[i] { | ||
| applied = append(applied, af) | ||
| } | ||
| } | ||
| result.Applied = applied | ||
| } | ||
|
|
||
| if len(failed) == len(nodeFixes) { | ||
| return nil | ||
| } | ||
|
|
||
| config := doc.GetCore().GetConfig() | ||
| *doc = openapi.OpenAPI{} | ||
|
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. P2: Rebuilding the model discards the original Prompt for AI agents |
||
| doc.InitCache() | ||
| doc.GetCore().SetConfig(config) | ||
| if _, err := marshaller.UnmarshalNode(ctx, "", rootNode, doc); err != nil { | ||
| return fmt.Errorf("failed to rebuild document after applying node fixes: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func makeAppliedFix(vErr *validation.Error, fix validation.Fix) AppliedFix { | ||
| af := AppliedFix{Error: vErr, Fix: fix} | ||
| if cd, ok := fix.(validation.ChangeDescriber); ok { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: A deferred node fix reserves its conflict key before it runs, so a failed node fix permanently blocks later same-rule fixes at that location. Reserve the key only after successful application, while preserving conflict handling among pending fixes.
Prompt for AI agents