feat: add --panic-unwind flag to build and test commands#1572
Merged
Conversation
Adds a new --panic-unwind option to `wasm-pack build` and `wasm-pack test` that builds with `-Cpanic=unwind` instead of the default `panic=abort`. When the flag is set, cargo is invoked via `+nightly` with `-Z build-std=std,panic_unwind` and `RUSTFLAGS` is augmented with `-Cpanic=unwind` (preserving any user-provided `RUSTFLAGS`). The nightly toolchain, `rust-src` component, and nightly `wasm32-unknown-unknown` target are auto-installed via `rustup` if not already present. The stable rustc and wasm-target preflight checks are skipped when --panic-unwind is set, since they are irrelevant for the nightly invocation. This is a build-tool-level enabler: it produces a wasm artifact compiled with unwinding semantics. The actual conversion of caught panics into JavaScript exceptions is the responsibility of the consuming bindings layer (e.g. wasm-bindgen's catch-unwind support).
8034af1 to
e8ad139
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a new
--panic-unwindflag towasm-pack buildandwasm-pack testso projects can opt in to building with-Cpanic=unwindinstead of the defaultpanic=abort.Rust's default panic strategy on
wasm32-unknown-unknownisabort, which terminates the WebAssembly instance on any panic. With nightly +-Zbuild-std, projects can now rebuildstdwith unwinding enabled, allowing bindings layers (e.g. wasm-bindgen's catch-unwind support) to convert panics into catchable JavaScript exceptions. Doing this manually requires plumbing several flags and ensuring the nightly toolchain has the right components —--panic-unwindcollapses that into a single flag.Worth noting up front: this is a build-tool-level enabler. It produces a
.wasmcompiled with unwinding semantics. The actual conversion of caught panics into JS exceptions is the responsibility of the consuming bindings layer; wasm-pack does not generate runtime shims for this.When
--panic-unwindis passed, cargo is invoked with:Any pre-existing
RUSTFLAGSvalue is preserved.--panic-unwindtoBuildOptionsandTestOptionscargo_build_wasm/cargo_build_wasm_testscheck_nightly_prerequisites()insrc/build/wasm_target.rsthat auto-installs the nightly toolchain,rust-srccomponent, and nightlywasm32-unknown-unknowntarget viarustupwhen missingstep_check_rustc_versionandstep_check_for_wasm_targetwhen--panic-unwindis set (the stable toolchain isn't invoked in that mode)docs/src/commands/build.mdanddocs/src/commands/test.mdBefore:
After:
cargo fmt --checkis clean andcargo clippyintroduces no new warnings. Thebuild::*integration tests all pass locally (cargo test --test all -- --test-threads=1 build::— 16/16). The default code path (without the flag) is unchanged.Related: #737 (panic_immediate_abort, distinct from this — that's a size flag).
Make sure these boxes are checked! 📦✅
rustfmtinstalledcargo fmton the code base before submitting