WebAssembly / WebAssembly/binaryen

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

Open
#5,926 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
8.6k
Forks
885
Avg merge
1d 19h
Merged PRs (30d)
69

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the wasm-opt command and the pkg/*_bg.wasm-opt.wasm path described in the report; inspect how that temporary output is produced and how the wasm-opt setting in Cargo.toml affects it. Reproduce a failed or interrupted build, then verify that no empty or invalid WASM artifact remains in pkg/ and subsequent runs do not parse the leaked file.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.