Skip to content

Commit

Permalink
Fix: attach annotation to static part of rule ref
Browse files Browse the repository at this point in the history
Previously, annotations were missing on rules that had variable
components in the ref head. Now we annotate the rule using the
path up to the first var.

Fixes open-policy-agent#7050

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Sep 23, 2024
1 parent e980731 commit ef2c9f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ast/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func attachRuleAnnotations(mod *Module) {
var j int
var found bool
for i, a := range cpy {
if rule.Ref().Equal(a.GetTargetPath()) {
if rule.Ref().GroundPrefix().Equal(a.GetTargetPath()) {
if a.Scope == annotationScopeDocument {
rule.Annotations = append(rule.Annotations, a)
} else if a.Scope == annotationScopeRule && rule.Loc().Row > a.Location.Row {
Expand Down
17 changes: 17 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5785,6 +5785,23 @@ p contains 2
},
},
},
{
note: "rule with variable in ref head",
module: `package test
# METADATA
# title: foo
rule[x] := true if x := 1
`,
expAnnotations: map[int][]*Annotations{
5: {
{
Scope: "rule",
Title: "foo",
},
},
},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit ef2c9f1

Please sign in to comment.