Open jat9292 opened 1 year ago
Are you using a nightly build? According to the folks at GoogleChromeLabs/wasm-bindgen-rayon
:
the Rust standard library for the WebAssembly target is built without threads support to ensure maximum portability.
Since we do want standard APIs like Mutex, Arc and so on to work, you'll need to use the nightly compiler toolchain and pass some flags to rebuild the standard library in addition to your own code.
In order to reduce risk of breakages, it's strongly recommended to use a fixed nightly version. For example, the latest stable Rust at the moment of writing is version 1.66, which corresponds to
nightly-2022-12-12
, which was tested and works with this crate.
Their advice:
Put a string
nightly-2022-12-12
in arust-toolchain
file in your project directory. This tells Rustup to use nightly toolchain by default for your project.
Source: https://github.com/GoogleChromeLabs/wasm-bindgen-rayon#using-config-files
Very interestingly, compiling the raytracer example with RUSTFLAGS set produces this error, but creating a .cargo/config.toml
with a similar configuration:
[unstable]
build-std = ['std', 'panic_abort']
[build]
target = "wasm32-unknown-unknown"
rustflags = '-Ctarget-feature=+atomics,+bulk-memory,+mutable-globals'
works about fine. Really strange, I may look into this later and open up issue.
In the case that this still doesn't work for whatever reason, you can manually specify the cargo run
flags:
$ wasm-pack build . --target no-modules -- -Z build-std=panic_abort,std
Very interestingly, compiling the raytracer example with RUSTFLAGS set produces this error, but creating a
.cargo/config.toml
with a similar configuration:[unstable] build-std = ['std', 'panic_abort'] [build] target = "wasm32-unknown-unknown" rustflags = '-Ctarget-feature=+atomics,+bulk-memory,+mutable-globals'
works about fine. Really strange, I may look into this later and open up issue.
In the case that this still doesn't work for whatever reason, you can manually specify the
cargo run
flags:
$ wasm-pack build . --target no-modules -- -Z build-std=panic_abort,std
This made the trick for me! Thanks :)
🐛 Bug description
Hello, I keep getting an error when trying to compile a multithreaded Rust library to WASM using
wasm-pack
. I used the following recommended flags for compilation :export RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals'
beforecargo build --target wasm32-unknown-unknown -Z build-std=panic_abort,std
. But then, when tryingwasm-pack build --target web
(as well aswasm-pack build --target no-modules
) I keep getting this error :which is related to the flags that I setup (I need them to be compatible with multithreading btw). I also tried to replace
std::thread
withwasm_thread
crate but got exactly the same issue when using the flags for multithreading. Any idea on how to solve my issue, please? Any help would be greatly appreciated :pray_tone2:Steps to reproduce
You can clone my repo and do previous commands in this directory.
🤔 Expected Behavior
wasm-pack
should build my WASM package.🌍 Your environment
Include the relevant details of your environment. wasm-pack version: 0.12.1 rustc version: rustc 1.74.0-nightly (84a9f4c6e 2023-08-29)