Hello World doesn't work on M1 mac
What I did (following the instructions https://trunkrs.dev/#install as well as possible):
-
rustup target add wasm32-unknown-unknown -
cargo install --locked trunk -
cargo install --locked wasm-bindgen-cli
Created a new project: cargo new chip8
Went into the project. Copied the sample code here into the top of the repo:
<html>
<head>
<link data-trunk rel="scss" href="path/to/index.scss"/>
</head>
</html>
Then trunk build failed to build due to that path not resolving; I created a blank scss file at the top of the repo, adjusted the path to point to it, then tried again:
$ trunk build
2025-02-22T18:58:01.382159Z INFO 🚀 Starting trunk 0.21.7
2025-02-22T18:58:01.431624Z INFO 📦 starting build
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
error: failed to find intrinsics to enable `clone_ref` function
2025-02-22T18:58:01.562214Z 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-bindgen
3: wasm-bindgen call to executable '[myhome]/.cargo/bin/wasm-bindgen' with args: '["--target=web", "--out-dir=[projectdir]/chip8/target/wasm-bindgen/debug", "--out-name=chip8", "[projectdir]/chip8/target/wasm32-unknown-unknown/debug/chip8.wasm", "--no-typescript"]' returned a bad status: exit status: 1
2025-02-22T18:58:01.562232Z ERROR error from build pipeline
2025-02-22T18:58:01.562235Z INFO 1: HTML build pipeline failed (1 errors), showing first
2025-02-22T18:58:01.562236Z INFO 2: error from asset pipeline
2025-02-22T18:58:01.562238Z INFO 3: running wasm-bindgen
2025-02-22T18:58:01.562239Z INFO 4: wasm-bindgen call to executable '[myhome]/.cargo/bin/wasm-bindgen' with args: '["--target=web", "--out-dir=[projectdir]/chip8/target/wasm-bindgen/debug", "--out-name=chip8", "[projectdir]/chip8/target/wasm32-unknown-unknown/debug/chip8.wasm", "--no-typescript"]' returned a bad status: exit status: 1
FAIL
I'm not sure what's happening here. The trunk build seems to be failing due to a wasm-bindgen failure; I assume this is the error above, whatever that is -- failed to find intrinsics to enable clone_ref function. I have no idea what this means, but considering my rust file is just fn main() {} I'm not sure what to do about it.
Is there more setup to be done here? I tried to follow the hello world guide pretty closely, so I'm not really sure where I could have gone wrong (I did find that target myself, so maybe I did that wrong, I'm not sure).
I'm going to leave this open in case it's of use to the maintainers but I now think it's a documentation bug; I followed the steps in https://trunkrs.dev/guide/getting-started/project.html and it worked flawlessly. I would suggest moving the content from the guide into the website, or making the website redirect to the guide, or something like that; but I see that there are a number of open issues about that, so it's a known issue.
Anyway let me take the time to say thank you all for the work you do 🙏 I'm thrilled to have it working locally with (comparatively) little effort.
It's a strange issue. I removed all crates from Cargo.toml, except console_error_panic_hook, and then used this in the main:
fn main() {
console_error_panic_hook::set_once();
}
That's enough to make the error disappear. But so is having web_sys in the Cargo.toml, and add a use web_sys; to the main.rs. However, removing the unused use web_sys; from the main.rs makes the error re-appear again.
same issue
same issue
But does it work if you add use web_sys in the main, or if you call console_error_panic_hook::set_once()?
But does it work if you add use web_sys in the main, or if you call console_error_panic_hook::set_once()?
Yes, it works!
I'm getting this with vanilla wasm-bindgen, it may not be a trunk issue.
Reproducible
cargo init deleteme && cd deleteme
cargo build --target wasm32-unknown-unknown
wasm-bindgen --out-dir ./target $CARGO_TARGET_DIR/wasm
32-unknown-unknown/debug/deleteme.wasm
# error: failed to find intrinsics to enable `clone_ref` function
Versions
cargo 1.86.0-nightly (fd784878c 2025-01-03)
wasm-bindgen 0.2.100
As mentioned adding use web_sys::*; to the main file fixes the issue.
thanks!
the above needed some correction here to run properly, though (as the env was not set):
cargo init deleteme && cd deleteme
cargo build --target wasm32-unknown-unknown
wasm-bindgen --out-dir target target/wasm32-unknown-unknown/debug/deleteme.wasm
fix:
cargo add web_sys
echo "use web_sys::*;\n`cat src/main.rs`" > src/main.rs
…
maybe this should go into docs aka guide / faq
Components version:
PS C:\Users\paul> rustup --version
rustup 1.28.2 (e4f3ad6f8 2025-04-28)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.86.0 (05f9846f8 2025-03-31)`
PS C:\Users\paul> rustc --version
rustc 1.86.0 (05f9846f8 2025-03-31)
PS C:\Users\paul> cargo --version
cargo 1.86.0 (adf9b6ad1 2025-02-28)
PS C:\Users\paul> trunk --version
trunk 0.21.13
PS C:\Users\paul> wasm-bindgen --version
wasm-bindgen 0.2.100
The complete reproduction steps are as follows:
1、
cargo init deleteme && cd deleteme
cargo build --target wasm32-unknown-unknown
wasm-bindgen --out-dir target target/wasm32-unknown-unknown/debug/deleteme.wasm
error: failed to find intrinsics to enable `clone_ref` function
2、fix:
cargo add web_sys
echo "use web_sys::*;\n`cat src/main.rs`" > src/main.rs
3、rerun:
cargo build --target wasm32-unknown-unknown
wasm-bindgen --out-dir target target/wasm32-unknown-unknown/debug/deleteme.wasm
This really does seem to be an issue with wasm-bindgen. We should record it in the Trunk's doc and examples, and also be reported to the upstream.
For those who found this via Google, here's the solution I found:
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
extern "C" {}
This error occurs if self.clone_ref is not set. self.clone_ref is set only if heap_alloc is set. heap_alloc is set only if __externref_table_alloc export is present.
I'm not sure when __externref_table_alloc export is present, but after looking at some compilation examples *1 *2 *3, __externref_table_alloc was present whenever there was an extern "C" or struct annotated with #[wasm_bindgen].
I chose extern "C" because it produces smaller wasm output.