Skip to content

Commit

Permalink
Fix wrong location on metadata parse errors on first line
Browse files Browse the repository at this point in the history
Fixes open-policy-agent#6587

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Jun 10, 2024
1 parent fea2647 commit 5194e38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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 {
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

0 comments on commit 5194e38

Please sign in to comment.