Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: running with --rego-v1 produced unparseable file #6833

Closed
anderseknert opened this issue Jun 26, 2024 · 0 comments · Fixed by #6867
Closed

fmt: running with --rego-v1 produced unparseable file #6833

anderseknert opened this issue Jun 26, 2024 · 0 comments · Fixed by #6867

Comments

@anderseknert
Copy link
Member

I'm helping a project modernizing their policies, and to prepare them for OPA 1.0. Running opa fmt --rego-v1 on one of their policies resulted in output that opa check later rejected as unparseable. Minimal example to reproduce:

original.rego

package policy

is_array(arr, _) = path {
    not contains(arr, "_")
    path = arr
}

contains(arr, elem) {
    arr[_] = elem
}

formatted.rego

package policy

import rego.v1

is_array(arr, _) := path if {
    not contains(arr, "_")
    path = arr
}

contains(arr, elem) if {
    arr[_] = elem
}

This looks valid, or at least it does to me... but opa check disagrees. If I had to guess, perhaps using contains as a custom function name would be the culprit (and maybe it is), but the error message leaves more questions than it answers:

$ opa check  .
1 error occurred during loading: policy.rego:10: rego_parse_error: unexpected if keyword
	contains(arr, elem) if {
	                    ^

That's... not right. Removing the is_array function above resolves the issue, so presumably there's some weird state kept from parsing that? Both function parses fine in isolation though.

Some thoughts:

  1. Why is this a parser error?
  2. Why is the if reported as unexpected?
  3. Should it be possible to name a custom function contains in Rego v1?
  4. Should opa fmt --rego-v1 perhaps do a parse validation of its output to ensure it's valid? I would prefer it if that tool said "can't convert this because of ..." than having it say nothing and discover the issue later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
2 participants