open-policy-agent/opa

Error message is wrong on ref with keyword in rule head assignment

Closed this issue · 3 comments

Continuation of #6432

If the same expression is moved to the rule head, the error message is completely untrue:

package p

r := input.default
opa eval -d p.rego -f pretty data.p.r
1 error occurred: p.rego:3: rego_parse_error: unexpected assign token: expected rule value term (e.g., r := <VALUE> { ... })
	r := input.default

Interestingly, using = instead of := results in a better error message, although for some reason repeated in two variations:

opa eval -d p.rego -f pretty data.p.r
2 errors occurred:
p.rego:3: rego_parse_error: unexpected default keyword: expected ident
	r = input.default
	          ^
p.rego:3: rego_parse_error: unexpected default keyword: expected rule value term (e.g., r[<nil>] = <VALUE> { ... })
	r = input.default

Fixing this along with #6432 would help empower users to identify and fix mistakes they've made.

The "unexpected assign token" seems to be everywhere when := is used for rule assignment, and replacing it for = often reveals the real error. We really ought to fix this, as the idiomatic option currently leads to a degraded developer experience :/

package play

s := "a
1 error occurred: policy.rego:3: rego_parse_error: unexpected assign token: expected rule value term (e.g., s := <VALUE> { ... })
	s := "a

vs.

package play

s = "a
3 errors occurred:
policy.rego:3: rego_parse_error: non-terminated string
	s = "a
	    ^
policy.rego:3: rego_parse_error: illegal token
	s = "a
	    ^
policy.rego:3: rego_parse_error: illegal token
	s = "a
	    ^

(we could probably do without the "illegal token" errors here, but at least the real error is presented)

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days. Although currently inactive, the issue could still be considered and actively worked on in the future. More details about the use-case this issue attempts to address, the value provided by completing it or possible solutions to resolve it would help to prioritize the issue.

Closing in favor of #6563