opa icon indicating copy to clipboard operation
opa copied to clipboard

opa build: combine -Ox with -t wasm

Open srenatus opened this issue 2 years ago • 5 comments

Optimizations, i.e. running partial eval, before compiling into Wasm is something you could want to do. However, at the moment, the only way to get that done is to do a series of calls:

$ opa build -O2 -o o2.tar.gz -e x/y x.rego
$ opa build  -t wasm -e x/y -b o2.tar.gz    
$ opa eval -fpretty -b bundle.tar.gz data.x.y
true

and the resulting bundle looks a bit funny:

$ tar tvf bundle.tar.gz                      
-rw-------  0 0      0           3 Jan  1  1970 /data.json
-rw-------  0 0      0          27 Jan  1  1970 /o2.tar.gz/optimized/x.rego
-rw-------  0 0      0      131444 Jan  1  1970 /policy.wasm
-rw-------  0 0      0          83 Jan  1  1970 /.manifest

It would be nice if you could just call opa build -O2 -t wasm -e x/y x.rego to get the steps combined.

If you run this command now, it'll panic:

$ opa build -O2 -t wasm -e x/y x.rego     
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).fileAddr(...)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:1662
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).runtimeErrorAbort(0x1016d0800, {0x0, 0x0, 0x0, {0x0, 0x0}, {0x0, 0x0}}, 0xc00063f028?)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:1754 +0x2b3
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).compileBlock(0xc0003dd200, 0xc0002454d0?)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:1047 +0x805
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).compileFunc(0xc0003dd200, 0xc0000b4cc0)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:855 +0x85e
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).compileFuncs(0xc0003dd200)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:717 +0x85
github.com/open-policy-agent/opa/internal/compiler/wasm.(*Compiler).Compile(0xc0003dd200)
        /Users/stephan/Sources/open-policy-agent/opa/internal/compiler/wasm/wasm.go:311 +0x58
github.com/open-policy-agent/opa/compile.(*Compiler).compileWasm(0xc00063fb30, {0x101c2ecb0, 0xc000124008})
        /Users/stephan/Sources/open-policy-agent/opa/compile/compile.go:627 +0x2b0
github.com/open-policy-agent/opa/compile.(*Compiler).Build(0xc00063fb30, {0x101c2ecb0, 0xc000124008})
        /Users/stephan/Sources/open-policy-agent/opa/compile/compile.go:307 +0x1cb
github.com/open-policy-agent/opa/cmd.dobuild({0xc0001eb4d0, 0xc0000d6cc0, 0x0, 0x0, 0x2, {{0xc00043a100, 0x1, 0x1}, 0x1}, {0x10184bb09, ...}, ...}, ...)
        /Users/stephan/Sources/open-policy-agent/opa/cmd/build.go:313 +0x888
github.com/open-policy-agent/opa/cmd.init.1.func2(0xc0001fc900?, {0xc0000b41e0?, 0x6?, 0x6?})
        /Users/stephan/Sources/open-policy-agent/opa/cmd/build.go:220 +0x6c
github.com/spf13/cobra.(*Command).execute(0xc0001fc900, {0xc0000b4180, 0x6, 0x6})
        /Users/stephan/Sources/open-policy-agent/opa/vendor/github.com/spf13/cobra/command.go:920 +0x847
github.com/spf13/cobra.(*Command).ExecuteC(0x1025d8040)
        /Users/stephan/Sources/open-policy-agent/opa/vendor/github.com/spf13/cobra/command.go:1044 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/stephan/Sources/open-policy-agent/opa/vendor/github.com/spf13/cobra/command.go:968
main.main()
        /Users/stephan/Sources/open-policy-agent/opa/main.go:14 +0x25

srenatus avatar Jan 06 '23 09:01 srenatus