Context
I am building a rust web application using leptos. It is a client side rendered application, therefore I am using trunk serve
to start my application.
I want to add the rand crate but this gives me an error on startup, which links to this part of the getrandom docs. (getrandom is a dependency of rand). Which states:
To enable getrandom’s functionality on wasm32-unknown-unknown using the Web Crypto methods described above via wasm-bindgen, do both of the following:
- Use the wasm_js feature flag, i.e. getrandom = { version = "0.3", features = ["wasm_js"] }. On its own, this only makes the backend available. (As a side effect this will make your Cargo.lock significantly larger if you are not already using wasm-bindgen, but otherwise enabling this feature is harmless.)
- Set RUSTFLAGS='--cfg getrandom_backend="wasm_js"' (see above).
The first one was easy to do, but the second one is tricky, since I am using trunk. But it does not seem possible to pass a flag to cargo build
using trunk
. It even says so explicitly when I encounter the build error:
error from build pipeline
Caused by:
0: HTML build pipeline failed (1 errors), showing first
1: error from asset pipeline
2: running cargo build
3: error during cargo build execution
4: cargo call to executable 'cargo' with args: '["build", "--target=wasm32-unknown-unknown", "--manifest-path", "/Users/user/git/leptos_stuff/Cargo.toml"]' returned a bad status: exit status: 101
The problem
I need trunk serve
to pass RUSTFLAGS='--cfg getrandom_backend="wasm_js"'
to cargo build
. I have not found anything about that in the trunk docs
Approaches I tried
I am using zsh
- running
RUSTFLAGS='--cfg getrandom_backend="wasm_js"' && trunk serve
- running
trunk serve --cfg getrandom_backend="wasm_js"
- setting
rustflags="--cfg getrandom_backend="wasm_js""
inTrunk.toml
in thebuild
section. - setting
command="cargo build --cfg getrandom_backend=\"wasm_js\" --target=wasm32-unknown-unknown --manifest-path /Users/user/git/leptos_stuff/Cargo.toml "
inTrunk.toml
in thebuild
section.
Result
I still get this error:
error: The wasm32-unknown-unknown targets are not supported by default; you may need to enable the "wasm_js" configuration flag. Note that enabling the
wasm_js feature flag alone is insufficient. For more information see:
So I must be doing something wrong. Any help is appreciated.