Skip to content
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

Fix wrong location on metadata parse errors on first line #6807

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,11 @@ func (b *metadataParser) Parse() (*Annotations, error) {
b.loc = comment.Location
}
}

if match == nil && len(b.comments) > 0 {
anderseknert marked this conversation as resolved.
Show resolved Hide resolved
b.loc = b.comments[0].Location
}

return nil, augmentYamlError(err, b.comments)
}

Expand Down
21 changes: 21 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5931,6 +5931,27 @@ func TestAnnotationsAugmentedError(t *testing.T) {
}
}

// https://github.com/open-policy-agent/opa/issues/6587
func TestAnnotationsParseErrorOnFirstRowGetsCorrectLocation(t *testing.T) {
module := `# METADATA
# description: ` + "`foo` bars" + `
# title: foo
package foo`

_, err := ParseModuleWithOpts("test.rego", module, ParserOptions{ProcessAnnotation: true})
if err == nil {
t.Fatalf("Expected error but got none")
}

if len(err.(Errors)) != 1 {
t.Fatalf("Expected exactly one error but got %v", err)
}

if err.(Errors)[0].Location.Row != 2 {
t.Errorf("Expected error on row 2 but got error on row %d", err.(Errors)[0].Location.Row)
}
}

func TestAuthorAnnotation(t *testing.T) {
tests := []struct {
note string
Expand Down