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.”
The previous solution to this problem encountered in the forums applied to version 0.2 of getrandom and no longer applies to version 0.3.x of getrandom
.
I’m going to use 0.3.2 version as an example to illustrate how to solve this problem.
getrandom docs: getrandom - Rust
Step 1: (This step is equivalent to getrandom = { version = “0.2.15”, features = [“custom”] })
Recommended optional method 1.
Create a .cargo folder in the project root directory and create the config.toml file in it
[build]
rustflags = [“--cfg”, “getrandom_backend=\”custom\“”]
target = “wasm32-unknown-unknown”
Optional method 2.
Modify dfx.json
“type": ‘custom’,
“build": [
“RUSTFLAGS='--cfg getrandom_backend=\“custom\”'
cargo build --target wasm32-unknown-unknown --release -p your_project_name”
],
Optional method 3.
Enter the command each time it is used:
RUSTFLAGS='--cfg getrandom_backend=“wasm_js”'
Step 2:
Add a custom random number method to lib.rs:
use getrandom::Error;
#[no_mangle]
unsafe extern “Rust” fn __getrandom_v03_custom(
dest: *mut u8,
len: usize.
) -> Result<(), Error> {
todo!()
}
Finish:
If after the above 2 steps, you still have related problems, you need to check if the rust library you are using contains multiple versions of getrandom.
Check the dependency chain for using getrandom and submit a bug request to the developer of the corresponding library for not being able to customize getrandom.