rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.75k stars 1.07k forks source link

tests fail if start attribute is present #2206

Closed Urhengulas closed 4 years ago

Urhengulas commented 4 years ago

Describe the Bug

Testing of wasm-bindgen lib fails if #[wasm_bindgen(start)] is present (for both browser and node).

Error Message ```bash $ wasm-pack test [INFO]: Checking for the Wasm target... Compiling rav1e_js v0.1.0 (/home/urhengulas/Documents/github.com/xiph/rav1e/rav1e_js) error: linking with `rust-lld` failed: exit code: 1 ### ----- stripped ----- ### = note: rust-lld: error: function signature mismatch: main >>> defined as () -> void in /path/to/rav1e/target/wasm32-unknown-unknown/debug/deps/librav1e_js.rlib(rav1e_js.e16cnisiuzwaayg.rcgu.o) >>> defined as (i32, i32) -> i32 in /path/to//rav1e/target/wasm32-unknown-unknown/debug/deps/web-17ed0dcdfd20c376.2kwhsv3emygdoj04.rcgu.o ### ----- stripped ---- ### error: entry symbol `main` declared multiple times | = help: did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead error: aborting due to previous error error: could not compile `rav1e_js`. ### ----- stripped ----- ### Caused by: failed to execute `cargo build`: exited with exit code: 101 full command: "cargo" "build" "--tests" "--target" "wasm32-unknown-unknown" ```

Steps to Reproduce

# 1. clone repo
git clone https://github.com/Urhengulas/rav1e.git
# 2. checkout commit
git checkout 46283554cfc093fb6721557068b3242794544226
# 3. go into subfolder
cd rav1e_js/
# 4. run tests
wasm-pack test --node

Expected Behavior

I expect wasm-pack test --node to run all wasm-bindgen tests for node.

And since the wasm-bindgen books says here:

The start function will not be executed when testing.

I also expect it to not be executed. (Maybe not even part of the build).

Actual Behavior

The command fails and doesn't run any tests.

Additional Context

wasm-pack build runs without any problems and also the pkg/ works.

alexcrichton commented 4 years ago

This says that main is defined multiple times in two crates, which I think is unrelated to #[wasm_bindgen(start)]? Can you see why the main symbol is defined twice?

Urhengulas commented 4 years ago

Thanks for the fast answer @alexcrichton.

No, I didn't define the #[wasm_bindgen(start)] twice. I now solved the issue by renaming the function I annotated with the attribute from main to main_js.

I got the hint from https://github.com/rust-gamedev/wg/issues/51:

wasm-bindgen doesn't currently support main - you have to use the wasm_bindgen(start) attribute on a public function instead, and that function can't be main

I couldn't find a reason why this solves the problem, but it does.

I will send a PR changing this in the example in the rust-bindgen book.

alexcrichton commented 4 years ago

Ok thanks for the follow-up! It sounds like this isn't a bug in wasm-bindgen so I'm going to close this.

nstoddard commented 3 years ago

I just encountered this issue, and the workaround of renaming main to main_js fails for me with the error <path to output file>.wasm: Syntax error: end of file unexpected. Everything works fine when building normally; the issue only occurs with cargo test.

Is there another workaround? Does anyone know if this is a bug in rustc, cargo, or wasm-bindgen?