Skip to content

webpack fails to parse WASM file generated by Rust 1.82+ #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
denis-sh opened this issue Feb 16, 2025 · 2 comments
Open

webpack fails to parse WASM file generated by Rust 1.82+ #191

denis-sh opened this issue Feb 16, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@denis-sh
Copy link

Describe the Bug

webpack v4 used in this template fails to parse WASM file generated by Rust 1.82+.

Steps to Reproduce

  1. rustup default 1.82.0
  2. npm run start
  3. See error:
ERROR in ./pkg/index_bg.wasm
Module parse failed: Internal failure: parseVec could not cast the value
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

Expected Behavior

Template should work without errors with latest Rust compiler.

Actual Behavior

Template doesn't work with Rust 1.82+compiler.

Additional Context

Starting from Rust 1.82.0 (released 2024-10-17) Reference types are enabled for WebAssembly target by default, see rust-lang/rust#128475. Reason for this change was just upgrade to LLVM 19, see WebAssembly/tool-conventions#233.

This broke webpack, because it uses webassemblyjs to parse WASM files and the latter didn't have Reference types support, see webpack/webpack#15566 (opened 2022-03-20, long before Rust 1.82.0 release).

webassemblyjs v1.14.1 (released 2024-11-06, 3 weeks after Rust 1.82.0 release) added working support for Reference types.

webpack v5.97.0 (released 2024-12-03, 1.5 months after Rust 1.82.0 release) finally added support for WebAssembly Reference types.

Also see related wasm-bindgen issue rustwasm/wasm-bindgen#4211 (opened 2024-10-18).

@denis-sh denis-sh added the bug Something isn't working label Feb 16, 2025
@denis-sh
Copy link
Author

Workaround

To workaround this issue (e.g. in case one has to use webpack v4) one has to change target to initial release of WebAssembly (as this version doesn't have Reference types) and force cargo to recompile Rust Standard Library.

Use these steps to do this:

  1. Get nightly rust to be able to recompile Rust Standard Library:
rustup default nightly
  1. Create this .cargo/config.toml config:
[target.wasm32-unknown-unknown]
rustflags = ["-Ctarget-cpu=mvp"]

[unstable]
build-std = ["std", "panic_abort"]
  1. Add rust-src component for your architecture:
rustup component add rust-src --toolchain nightly-<your-architecture>

To get exact name of the component one can just run wasm-pack build and see it in error message:

$ wasm-pack build
...
error: ".../Cargo.lock" does not exist, unable to build with the standard library, try:
        rustup component add rust-src --toolchain nightly-<your-architecture-is-here>
Error: Compiling your crate to WebAssembly failed
  1. Done. npm run start should work now.

@denis-sh
Copy link
Author

Pull request #192 fixes this issue once merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant