koute / cargo-web

A Cargo subcommand for the client-side Web
Apache License 2.0
1.1k stars 80 forks source link

tests pass on asmjs but fail on wasm32-unknown #208

Open elichai opened 5 years ago

elichai commented 5 years ago

Hi, Running cargo web test --target=asmjs-unknown-emscripten the tests pass smoothly. But If I run cargo web test --target=wasm32-unknown-unknown I get the following:

Error loading Rust wasm module 'mylib_41e23712799ffb49': <LinkError>
error: unhandled exception thrown
error:     LinkError: WebAssembly.instantiate(): Import #0 module="env" function="a_c_function" error: function import requires a callable
    at WebAssembly.instantiate (http://localhost:41879/__cargo-web__/test_runner.js:319:36)
    at http://localhost:41879/js/app.js:37:65
error: source: http://localhost:41879/__cargo-web__/test_runner.js:318:35

The a_c_function is a C function provided that I use in rust. why does it fail? how can I fix this?

koute commented 5 years ago

Based on that error message alone I'm unable to tell you why it happens. Can you perhaps post a minimal reproduction of your problem?

elichai commented 5 years ago

@koute Yes: https://github.com/elichai/cargo-web-test

koute commented 5 years ago

Ah, right. It's natural that it doesn't work as AFAIK wasm32-unknown-unknown in general doesn't support linking with non-Rust code. (In theory it's possible, but you'd have to compile the C code manually, and then manually hook it up in your Rust module imports.)

By the way, this is unrelated to your issue, but it's interesting that your example doesn't compile on nightly (with vanilla cargo):

$ rustup run nightly rustc --version
rustc 1.38.0-nightly (4560cb830 2019-07-28)
$ rustup run nightly cargo rustc --target wasm32-unknown-unknown --profile test
error: linking with `rust-lld` failed: exit code: 1
  |
...snip...
  = note: rust-lld: error: /tmp/cargo-web-test/target/wasm32-unknown-unknown/debug/build/testweb-dfe6cd8f46074fe6/out/liblinkme.a: archive has no index; run ranlib to add one

error: aborting due to previous error

error: Could not compile `testweb`.

while it does on stable:

$ rustup run stable rustc --version
rustc 1.36.0 (a53f9df32 2019-07-03)
$ rustup run stable cargo rustc --target wasm32-unknown-unknown --profile test
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s

It might be worthwhile to create a rustc issue for this. (:

elichai commented 5 years ago

This works with wasm-pack easily. as long as you have clang there's no reason it won't compile the C code to wasm32-unknown-unknown