Failure to deploy canister when `rand` as dependency (Rust)

Hi,

The package rand has no support for wasm32-unknown-unknown, unless the js feature is enabled, i.e. add getrandom = { version = "0.2", features = ["js"] } to the dependencies.

[dependencies]
rand = "0.8"
getrandom = { version = "0.2", features = ["js"] }

When running:

dfx deploy canister_name

The following error is thrown:

The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Wasm module of canister xxxx-xxxx-xxxxx-xxxxxx is not valid: Wasm module has an invalid import section. Module imports function '__wbindgen_object_drop_ref' from '__wbindgen_placeholder__' that is not exported by the runtime.

The error is only thrown on dfx cli, where simply running the Rust cargo build does not throw an error.

cargo build --target wasm32-unknown-unknown --release --package xxxx

The rand package can be quite useful when mocking data.

Versions:
cargo 1.54.0
rustc 1.54.0
rustup 1.24.3

References:
https://docs.rs/getrandom/0.2.3/getrandom/#webassembly-support

3 Likes