trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Trunk cannot build --release with wasm that uses threads / bulk memory

Open BGR360 opened this issue 1 year ago • 5 comments

It fails at the wasm-opt stage:

[wasm-validator error in module] unexpected false: shared memory requires threads [--enable-threads], on 
memory
[wasm-validator error in module] unexpected false: nonzero segment flags require bulk memory [--enable-bulk-memory], on 
[wasm-validator error in module] unexpected false: nonzero segment flags require bulk memory [--enable-bulk-memory], on 
[wasm-validator error in module] unexpected false: nonzero segment flags require bulk memory [--enable-bulk-memory], on 
Fatal: error validating input
2024-09-07T05:47:45.715687Z ERROR ❌ error
error from build pipeline

Caused by:
    0: HTML build pipeline failed (1 errors), showing first
    1: error from asset pipeline
    2: running wasm-opt
    3: wasm-opt call to executable '/home/runner/.cache/trunk/wasm-opt-version_116/bin/wasm-opt' with args: '["--output=/home/runner/work/dactyl/dactyl/target/wasm-opt/release/dactyl_bg.wasm", "-O", "/home/runner/work/dactyl/dactyl/dist/.stage/dactyl-50e150da88065d87_bg.wasm"]' returned a bad status: exit status: 1

I propose adding another data-... option to rust assets, similar to these ones:

https://github.com/trunk-rs/trunk/blob/14b972f7369cf4e9d1b098e3afb4bc0e0ecd54d9/src/pipelines/rust/mod.rs#L157-L158

BGR360 avatar Sep 07 '24 06:09 BGR360

Similar to https://github.com/trunk-rs/trunk/issues/854#issuecomment-2334715828, it would have been really useful for me if Trunk offered an option to pass arbitrary extra flags to wasm-opt.

BGR360 avatar Sep 07 '24 06:09 BGR360

Should be relatively easy to enable this for wasm-opt versions that require --enable-bulk-memory but I don't see why we shouldn't also have an option to pass aribitrary flags as well.. We can easily do so for rustc with RUSTFLAGS but we have no WASM_OPTFLAGS or even CARGOFLAGS (cargo lacking), it feels a bit odd. It should be possible, even with the current positional parameter expectations trunk imposes

wick3dr0se avatar May 19 '25 16:05 wick3dr0se

I'm stuck with this issue for latest trunk and wasm-opt-version_123

[wasm-validator error in function 2607] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_u
 (f64.neg
  (call $2366
   (call $2963
    (local.get $3)
   )
  )
 )
)
Fatal: error validating input
2025-08-18T16:35:21.331807Z ERROR ❌ error
error from build pipeline

Caused by:
    0: HTML build pipeline failed (1 errors), showing first
    1: error from asset pipeline
    2: running wasm-opt
    3: wasm-opt call to executable '/home/marc/.cache/trunk/wasm-opt-version_123/bin/wasm-opt' with args: '["--output=/home/marc/repos/plotinator3000/target/wasm-opt/release/plotinator3000_bg.wasm", "-O2", "/home/marc/repos/plotinator3000/dist/.stage/plotinator3000_bg.wasm"]' returned a bad status: exit status: 1
2025-08-18T16:35:21.331843Z ERROR error from build pipeline
2025-08-18T16:35:21.331846Z  INFO   1: HTML build pipeline failed (1 errors), showing first
2025-08-18T16:35:21.331849Z  INFO   2: error from asset pipeline
2025-08-18T16:35:21.331851Z  INFO   3: running wasm-opt
2025-08-18T16:35:21.331853Z  INFO   4: wasm-opt call to executable '/home/marc/.cache/trunk/wasm-opt-version_123/bin/wasm-opt' with args: '["--output=/home/marc/repos/plotinator3000/target/wasm-opt/release/plotinator3000_bg.wasm", "-O2", "/home/marc/repos/plotinator3000/dist/.stage/plotinator3000_bg.wasm"]' returned a bad status: exit status: 1

CramBL avatar Aug 18 '25 16:08 CramBL

It was fixed for me by downgrading to Rust v1.86.

1.87+ all error with the same bulk memory related error

CramBL avatar Aug 18 '25 17:08 CramBL

I fixed it by adding data-wasm-opt-params="--enable-bulk-memory --enable-nontrapping-float-to-int" like this

<link
     data-trunk
    rel="rust"
    data-wasm-opt="3"
    data-wasm-opt-params="--enable-bulk-memory --enable-nontrapping-float-to-int"
/>

And adding this to .cargo/config.toml

[target.wasm32-unknown-unknown]
rustflags = ['--cfg=web_sys_unstable_apis', '--cfg=getrandom_backend="wasm_js"']

CramBL avatar Sep 12 '25 20:09 CramBL

It was fixed for me by downgrading to Rust v1.86.

1.87+ all error with the same bulk memory related error

If anyone wants to know why: https://github.com/rust-lang/rust/issues/141048

I did the same (downgrading to Rust 1.86) and also tinkered with the index.html similarly but I just disabled wasm-opt entirely

<link data-trunk rel="rust" data-wasm-opt="0" />

This works too but passing the flags, like you mentioned, probably makes the most sense for now

wick3dr0se avatar Dec 28 '25 23:12 wick3dr0se