Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass cargo build options through trunk?

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

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.

I have tried the following (my shell is 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"" in Trunk.toml in the build section.
  • setting command="cargo build --cfg getrandom_backend=\"wasm_js\" --target=wasm32-unknown-unknown --manifest-path /Users/user/git/leptos_stuff/Cargo.toml " in Trunk.toml in the build section.

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: https://docs.rs/getrandom/#webassembly-support

So I must be doing something wrong. How can I pass this flag to cargo through trunk serve?

like image 304
mario.b Avatar asked Oct 25 '25 05:10

mario.b


1 Answers

Like @cafce25 commented, the CLI syntax would be:

RUSTFLAGS='--cfg getrandom_backend="wasm_js"' trunk serve

But if you're tired of doing it the whole time and just want to use trunk serve (and also make it easier for other people working on your project), I'd recommend adding a Cargo config.

Create a .cargo directory with config.toml inside with:

[target.wasm32-unknown-unknown]
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]
like image 111
Shader Avatar answered Oct 27 '25 19:10

Shader