Make error messages less ambiguous when rules are declared on same line
Rego allows rules to be declared on the same line:
package test
foo(x) {x == 2} bar {true} baz := 1
The policy
package test
p(x) if {
x == 2
}
generates the error
1 error occurred: policy.rego:3: rego_unsafe_var_error: var x is unsafe
Post the introduction of the if keyword, we have the opportunity to suggest importing the keyword.
However, there remains the issue of future unknown keywords. E.g. say we, in some future version of OPA, introduce the keyword foobar, that is allowed in the same position as if. Users that know about the keyword, but are unwittingly running a too old version of OPA could write the policy
package test
import future.keywords
p(x) foobar {
x == 2
}
and receive the error
1 error occurred: policy.rego:4: rego_unsafe_var_error: var x is unsafe
Since the actively used OPA version doesn't know about the foobar keyword, we cannot include such specific language in the error message. We can, however, add some extra context to the error that might reduce time to troubleshoot. Adding the enclosing rule name in the error would lead the user to understand that foobar wasn't interpreted as a keyword, and that x is indeed not known in the scope of the foobar rule:
1 error occurred: policy.rego:4: rego_unsafe_var_error: var x is unsafe in rule foobar
This issue has been automatically marked as inactive because it has not had any activity in the last 30 days.