binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Empty/Leaked `pkg/*_bg.wasm-opt.wasm` file causing parse error on subsequent `wasm-opt` runs

Open ryan-williams opened this issue 2 years ago • 0 comments

I had a shell continuously building my project's WASM web target, while I was developing:

cargo watch -i .gitignore -i "pkg/*" -s "wasm-pack build --target web"

At some point, a wasm-opt command (called from within wasm-pack) started failing, even on clean commits that it had previously succeeded on:

[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO  wasm_pack::child] Running "$bin/wasm-opt" "$proj/pkg/${name}_bg.wasm-opt.wasm" "-o" "$proj/pkg/${name}_bg.wasm-opt.wasm-opt.wasm" "-O"
[parse exception: expected more elements in list]
Fatal: error parsing wasm (try --debug for more info)
Error: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "$bin/wasm-opt" "$proj/pkg/${name}_bg.wasm-opt.wasm" "-o" "$proj/pkg/${name}_bg.wasm-opt.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
Caused by: failed to execute `wasm-opt`: exited with exit status: 1

Running the command directly, with --debug:

wasm-opt --debug pkg/${name}_bg.wasm-opt.wasm -o pkg/${name}_bg.wasm-opt.wasm-opt.wasm -O
# [parse exception: expected more elements in list]
# Fatal: error parsing wasm. here is what we read up to the error:
# (module
# )
# 

I eventually realized the problem was the existence of an empty file named ${name}_bg.wasm-opt.wasm in my pkg/ directory. Presumably it was left over from a prior run of wasm-{pack,opt} that failed to clean up after itself. The references to pkg/*_bg.wasm-opt.wasm-opt.wasm (note the double .wasm-opt) was a clue, in retrospect.

It seems like there are 2 issues:

  1. wasm-opt makes a temporary pkg/*_bg.wasm-opt.wasm file in pkg/ that should instead live in a proper tempdir somewhere, so it doesn't cause problems if it's leaked.
  2. wasm-opt doesn't always properly clean up after itself. Maybe I SIGINT'd a build at some point? That shouldn't leak an empty/invalid .wasm file in pkg/.

I had tried cargo clean, uninstall/reinstall wasm-{pack,opt}, to no avail, and found no search hits for various salient error messages above, so hopefully this also helps future searchers.

ryan-williams avatar Sep 08 '23 19:09 ryan-williams