Wasm-bindgen: Error building with function that returns a String

Created on 11 Aug 2020  路  10Comments  路  Source: rustwasm/wasm-bindgen

Describe the Bug

After adding a function which returns a String, running wasm-pack build outputs a series of complex error messages.

Steps to Reproduce

  1. Create a new project with wasm-pack new hello-wasm
  2. Add a public function which returns a string (for example the one below which was taken from the wasm-bindgen guide) to the bottom of lib.rs
  3. Run wasm-pack build

Function code:

#[wasm_bindgen]
pub fn return_string() -> String {
    "hello".into()
}

Expected Behavior

Successful build.

Actual Behavior

Wasm-pack outputs 16462 lines of debugging to STDOUT, and the following to STDERR:

[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
warning: function is never used: `set_panic_hook`
 --> src/utils.rs:1:8
  |
1 | pub fn set_panic_hook() {
  |        ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

    Finished release [optimized] target(s) in 0.01s
[INFO]: Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[wasm-validator error in module] unexpected true: Exported global cannot be mutable, on 
global$0
Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
  full command: "/home/tobico/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt" "/home/tobico/Code/hello-wasm/pkg/hello_wasm_bg.wasm" "-o" "/home/tobico/Code/hello-wasm/pkg/hello_wasm_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

Additional Context

  • Running on Windows with Ubuntu 20.04 under WSL
  • Rust 1.45.2 installed with rustup install script
  • wasm-pack 0.9.1 installed with the wasm-pack install script
  • wasm-bindgen version 0.2.67
bug

Most helpful comment

I got this same error as I was going through the game of life chapter in the Rust and WebAssemly tutorial.

Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
  full command: "/home/dg/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt" "/home/dg/Documents/projects/rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm" "-o" "/home/dg/Documents/projects/rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

I added these lines to my Cargo.toml file and got it to compile.

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false

[package.metadata.wasm-pack.profile.release]
wasm-opt = false


Ubuntu 19
rustc 1.45.2 (d3fb005a3 2020-07-31)
wasm-bindgen 0.2.67
wasm-pack 0.9.1

All 10 comments

One more update, I just tried installing Rust and wasm-pack natively in Windows (not under WSL) and could reproduce this same issue under that environment also.

Look like it's related to wasm-pack#793
Or the other way around, I guess.

I got this same error as I was going through the game of life chapter in the Rust and WebAssemly tutorial.

Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
  full command: "/home/dg/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt" "/home/dg/Documents/projects/rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm" "-o" "/home/dg/Documents/projects/rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

I added these lines to my Cargo.toml file and got it to compile.

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false

[package.metadata.wasm-pack.profile.release]
wasm-opt = false


Ubuntu 19
rustc 1.45.2 (d3fb005a3 2020-07-31)
wasm-bindgen 0.2.67
wasm-pack 0.9.1

Having the same while trying out the game of life tutorial.

MacOs 11 Beta
rustc 1.45.0 (5c1f21c3b 2020-07-13)
wasm-pack 0.9.1

+1 for having the exact same issue as @dgendill.

Also experiencing the error with other very simple code.

MacOs 10.14.6 (18G5033)
rustc 1.45.2 (d3fb005a3 2020-07-31)
wasm-pack 0.9.1

FWIW: ran into similar issue, after upgrading to 0.2.67 (0.2.66 gives same error too).

Fixed it by downgrading to 0.2.65 in Cargo.tom:

wasm-bindgen = "=0.2.65"

Sorry for the slow response on this, but this is due to wasm-opt which wasm-bindgen does not run, so the issue here is with wasm-pack not wasm-bindgen, and I'd recommend disabling wasm-opt for now.

Ok, somehow related to version of wasm-bindgen though.

All else being the same, =0.2.65 works for me while >=0.2.66 does not :S

Yes recent version of wasm-bindgen started using exported mutable globals which have been stable for quite some time now.

The related issue is https://github.com/rustwasm/wasm-pack/issues/886

And the current workaround is to add the following section in the Cargo.toml.

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Os", "--enable-mutable-globals"]
Was this page helpful?
0 / 5 - 0 ratings