Module imports function '__wbindgen_describe' from '__wbindgen_placeholder__' that is not exported by the runtime

I used these dependencies to implement the signature verification of sr25519. The compilation passed, but an error was reported during local deploy. How to solve it

image

error message:
The Replica returned an error: code 5, message: “Wasm module of canister rwlgt-iiaaa-aaaaa-aaaaa-cai is not valid: Wasm module has an invalid import section. Module imports function ‘__wbindgen_describe’ from ‘wbindgen_placeholder’ that is not exported by the runtime.”

Just ran into this, adding what I found if someone wants to dig deeper.

This errors out

getrandom = { version = "0.2.6", features = ["js"]}
rand = "0.8.5"

Error: The Replica returned an error: code 5, message: “Wasm module of canister rrkah-fqaaa-aaaaa-aaaaq-cai is not valid: Wasm module has an invalid import section. Module imports function ‘__wbindgen_describe’ from ‘wbindgen_placeholder’ that is not exported by the runtime.”

This works

getrandom = { version = "0.2.3", features = ["js"]}
rand = "0.7.3"

$ dfx --version
dfx 0.9.3

3 Likes

very interesting.

getrandom = {version="0.2.8", features=[“js”]}
tiny-bip39 = {version = “0.7.3”}

works for me.

getrandom = {version="0.2.8", features=[“js”]}
tiny-bip39 = {version = “1.0.0”}

returns

The Replica returned an error: code 5, message: “Wasm module of canister r7inp-6aaaa-aaaaa-aaabq-cai is not valid: Wasm module has an invalid import section. Module imports function ‘__wbindgen_describe’ from ‘wbindgen_placeholder’ that is not exported by the runtime.”

I am getting this error as well, and the fixes in this thread so far are not working in my project.

@Severin , any chance you know the fix to this?

I am also trying to use the uuid crate in my canister. When I use it I am getting a different kind of error but still similar to this:

      Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, 
reject message Wasm module of canister bkyz2-fmaaa-aaaaa-qaaaq-cai is not valid: Wasm module has 
an invalid import section. Module imports function '__wbg_crypto_58f13aa23ffcb166' from 
'__wbindgen_placeholder__' that is not exported by the runtime., error code None

It’s trying to use functions that are not available in wasm32 I suppose. Is it because of somethin gin the base crate or does it only happen if you turn on certain features?

There’s probably some inspiration in this thread: Generating custom principals/uuids in Rust

1 Like

The error I above is only happening when I use the Uuid package, So I guess I need to checkout those custom principals/uuids.

But even after removing the Uuid. I am getting this similar error:

Failed to install wasm module to canister 'backend'.
Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, 
reject message Wasm module of canister bkyz2-fmaaa-aaaaa-qaaaq-cai is not valid: Wasm module has
 an invalid import section. Module imports function '__wbindgen_describe' from '__wbindgen_placeholder__' that is not exported by the runtime., error code None

These are mt dependencies:


[lib]
crate-type = ["cdylib"]

[dependencies]
candid = "0.9.9"
ic-cdk = "0.11.1"
serde = { version = "1", features = ["derive"] }
getrandom = { version = "0.2.11", features = ["js"] }
rand = "0.8.5"
tiny-bip39 = {version = "1.0.0"}
serde_json = "1.0"
ic-stable-structures = "0.6.0"

In my lib I am only using this:

#[macro_use]
extern crate serde;
use candid::{Decode, Encode};
use ic_cdk::api::time;
use ic_stable_structures::memory_manager::{MemoryId, MemoryManager, VirtualMemory};
use ic_stable_structures::storable::Bound;
use ic_stable_structures::{ DefaultMemoryImpl, StableBTreeMap, Storable};
use std::{borrow::Cow, cell::RefCell};

You need to make sure that none of your dependencies depend on wasm bindgen. For example, getrandom you have the js feature set, that dooms you from the start. Start by turning off that feature. Then in your Cargo.lock file you can search for wasm-bindgen or bindgen etc and try to find out which dependencies are assuming a wasm-bindgen environment. Hopefully you can turn features on or off to deal with that, in the worst case you’d need to fork the project and remove the wasm-bindgen code yourself.

2 Likes

The error comes from the js feature of getrandom crate.

The js feature assumes that the target platform is a web browser which containing a JavaScript engine.

This is not true for the IC Wasm Runtime. To get randomness within a Canister, you should use the official API: raw_rand in ic_cdk::api::management_canister::main - Rust

If you have some dependencies rely on getrandom, you can turn on the custom feature of it and implement dummy getrandom hook to make it compile.

Here is an example of how to do it.

6 Likes