Empty/Leaked `pkg/*_bg.wasm-opt.wasm` file causing parse error on subsequent `wasm-opt` runs
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:
-
wasm-optmakes a temporarypkg/*_bg.wasm-opt.wasmfile inpkg/that should instead live in a proper tempdir somewhere, so it doesn't cause problems if it's leaked. -
wasm-optdoesn't always properly clean up after itself. Maybe ISIGINT'd a build at some point? That shouldn't leak an empty/invalid.wasmfile inpkg/.
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.