open-policy-agent/opa

Incorrect error message when keywords are used on the LHS of comprehensions

Opened this issue · 1 comments

Short description

In some circumstances, use of keywords on the left hand side of comprehensions can cause the error message to be unexpected identifier token: non-terminated object rather than unexpected ___ keyword. Although OPA is correct to throw an error in this situation, it makes the problem much harder to debug.

Steps To Reproduce

$ opa eval -f pretty '{{"b": bt, "a": as} | as := [1,2,3][_]; bt := ["a","b","c"][_]}'
1 error occurred: 1:8: rego_parse_error: unexpected identifier token: non-terminated object
	{{"b": bt, "a": as} | as := [1,2,3][_]; bt := ["a","b","c"][_]}
	      ^
$ opa eval -f pretty '[{"b": bt, "a": as} | as := [1,2,3][_]; bt := ["a","b","c"][_]]'
1 error occurred: 1:8: rego_parse_error: unexpected identifier token: non-terminated object
	[{"b": bt, "a": as} | as := [1,2,3][_]; bt := ["a","b","c"][_]]
	      ^
$ opa eval -f pretty '{{"a": as, "b": bt} | as := [1,2,3][_]; bt := ["a","b","c"][_]}'
1 error occurred: 1:8: rego_parse_error: unexpected as keyword
	{{"a": as, "b": bt} | as := [1,2,3][_]; bt := ["a","b","c"][_]}
	      ^
$ opa eval -f pretty '[{"a": as, "b": bt} | as := [1,2,3][_]; bt := ["a","b","c"][_]]'
1 error occurred: 1:8: rego_parse_error: unexpected as keyword
	[{"a": as, "b": bt} | as := [1,2,3][_]; bt := ["a","b","c"][_]]

Expected behavior

All of the above examples should have emitted rego_parse_error: unexpected as keyword errors, and pointed to the as, not to the bt, which is an allowed variable name.

Additional context

$ uname -a
Linux eos 6.6.10-76060610-generic #202401051437~1709085277~22.04~31d73d8 SMP PREEMPT_DYNAMIC Wed F x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/lsb-release
DISTRIB_ID=Pop
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Pop!_OS 22.04 LTS"
$ opa version 
Version: 0.63.0
Build Commit: bb30b153692aa77738b982e29424c8b24a1ad14a
Build Timestamp: 2024-03-28T15:50:15Z
Build Hostname: 
Go Version: go1.22.2
Platform: linux/amd64
WebAssembly: unavailable
stale commented

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.