opa icon indicating copy to clipboard operation
opa copied to clipboard

Parser should fail with misspelled if keyword?

Open pippenpaddleopsicopolis opened this issue 3 years ago • 7 comments
trafficstars

Short description

I'm actually not sure if this syntax is valid but I mistyped if (ifv) and the policy parsing didn't fail.

package test

import future.keywords.if

eval(resource) = "pass" ifv {
    # Logic that evaluates to true if the resource is compliant
    true
}

Steps To Reproduce

https://play.openpolicyagent.org/p/ygexaGdhII

Expected behavior

Parser should fail?

Additional context

pippenpaddleopsicopolis avatar Oct 05 '22 22:10 pippenpaddleopsicopolis

eval(resource) = "pass"

ifv {
    # Logic that evaluates to true if the resource is compliant
    true
}

This is what it looks like to the parser: defining a function and a rule.

We could look into requiring a being after a shorthand rule out function definition, maybe?

srenatus avatar Oct 06 '22 05:10 srenatus

This is similar to the oddness I encountered some week ago, where forgetting to import future keywords would still allow the "if" construct — it would just do something else than what was expected. I wonder if there is any point in us allowing more than one rule to be defined on a single line? It seems like an awful style preference, and one that's much more likely to be the result of a mistake.

anderseknert avatar Oct 10 '22 09:10 anderseknert

How about making this a strict mode check? It'll be the gracious way out of this. After all, awkward or not, it's valid syntax right now and we'd break it if we changed it.

OTOH, it have a really hard time imaging anyone depending on this possibility. 🤔

srenatus avatar Oct 10 '22 09:10 srenatus

I like the idea of adding this to the strict mode first.

pippenpaddleopsicopolis avatar Oct 19 '22 09:10 pippenpaddleopsicopolis

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days.

stale[bot] avatar Nov 18 '22 11:11 stale[bot]

I looked into this for a rule in Regal, but I think the recommendation here would be to automatically format the package using opa fmt, which would reformat the code similarly to what @srenatus demonstrated above. While the formatter has a few quirks of its own, there are many odd-looking constructs and probable bugs that is solved by presenting the code in a better way. I'm not sure strict mode should flag issues fixed by the formatter.

anderseknert avatar Sep 08 '23 08:09 anderseknert

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.

stale[bot] avatar Oct 08 '23 11:10 stale[bot]

Replacing the no longer future import with rego.v1 and this is a compile time error:

1 error occurred: policy.rego:5: rego_parse_error: `if` keyword is required before rule body

Since import rego.v1 effectively replaces import future.keywords and makes both if and contains mandatory, I think we can close this now.

anderseknert avatar Mar 29 '24 22:03 anderseknert