'VSCode Rego Plugin opa evaluate not working as expected
I'm learning OPA and rego. I'm trying to write a simple policy and have the same evaluated through VSCode Plugin.
Folder Structure:
learning.rego
package learning
test {
a := "test"
a == "test"
}
input.json
{}
And when I choose "OPA: Evaluate Selection" option from the command pallete, I get the below error.
{
"errors": [
{
"message": "expected body but got *ast.Package"
}
],
"metrics": {
"timer_rego_load_bundles_ns": 919639,
"timer_rego_module_compile_ns": 481890,
"timer_rego_module_parse_ns": 55722,
"timer_rego_query_parse_ns": 22773
}
}
But the same policy is working as expected in rego playground and evaluates to "true" as expected. What am I missing here?
Solution 1:[1]
Try running "Evaluate Package" instead of "Evaluate Selection", and it should work.
The Open Policy Agent extension for VSCode offers three ways of evaluating Rego code:
- Evaluate Package
This seems to be the one you want. This parses the selected Rego code as an entire package. It expects the selection to begin with a Package declaration (e.g. package learning
). If you select the entire Rego package and run "Evaluate Packages", it should work.
- Evaluate Selection
This option evaluates part of a Rego package, so it expects the selection to begin with a Body (e.g. your test
definition). If you just select test
and its definition, it should work.
- Partially Evaluate Selection
This final option is a bit more complex. In essence, rather than evaluating the policy it returns the conditions under which the policy is satisfied. This is useful for building queries for external datastores, or for interacting with complex data.
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Will Beason |
Solution 2 | Mansi |