Getrandom version conflict error - building a WASM

Hello rusty fellow,

im trying to build libsignalś protocol crate .WASM for R&D and project analysis but im failing again and again because of the getrandom version conflict.

What i tried
i build using following command

aqua@aqua-server:~/Desktop/libsignal-0.76.7/rust/protocol$ wasm-pack build --target web --out-dir ../pkg_wasm

and get following error

aqua@aqua-server:~/Desktop/libsignal-0.76.7/rust/protocol$ wasm-pack build --target web --out-dir ../pkg_wasm
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
   Compiling proc-macro2 v1.0.94
   Compiling unicode-ident v1.0.18
   Compiling cfg-if v1.0.0
   Compiling log v0.4.27
   Compiling wasm-bindgen-shared v0.2.100
   Compiling bumpalo v3.17.0
   Compiling quote v1.0.40
   Compiling syn v2.0.100
   Compiling typenum v1.18.0
   Compiling version_check v0.9.5
   Compiling generic-array v0.14.7
   Compiling wasm-bindgen v0.2.100
   Compiling once_cell v1.21.3
   Compiling getrandom v0.3.3
   Compiling subtle v2.6.1
error: The wasm32-unknown-unknown targets are not supported by default; you may need to enable the "wasm_js" configuration flag. Note that enabling the `wasm_js` feature flag alone is insufficient. For more information see: https://docs.rs/getrandom/0.3.3/#webassembly-support
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/backends.rs:168:9
    |
168 | /         compile_error!(concat!(
169 | |             "The wasm32-unknown-unknown targets are not supported by default; \
170 | |             you may need to enable the \"wasm_js\" configuration flag. Note \
171 | |             that enabling the `wasm_js` feature flag alone is insufficient. \
172 | |             For more information see: \
173 | |             https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
174 | |         ));
    | |__________^

error[E0425]: cannot find function `fill_inner` in module `backends`
  --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:99:19
   |
99 |         backends::fill_inner(dest)?;
   |                   ^^^^^^^^^^ not found in `backends`

error[E0425]: cannot find function `inner_u32` in module `backends`
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:128:15
    |
128 |     backends::inner_u32()
    |               ^^^^^^^^^ not found in `backends`
    |
help: consider importing this function
    |
33  + use crate::util::inner_u32;
    |
help: if you import `inner_u32`, refer to it directly
    |
128 -     backends::inner_u32()
128 +     inner_u32()
    |

error[E0425]: cannot find function `inner_u64` in module `backends`
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:142:15
    |
142 |     backends::inner_u64()
    |               ^^^^^^^^^ not found in `backends`
    |
help: consider importing this function
    |
33  + use crate::util::inner_u64;
    |
help: if you import `inner_u64`, refer to it directly
    |
142 -     backends::inner_u64()
142 +     inner_u64()
    |

For more information about this error, try `rustc --explain E0425`.
error: could not compile `getrandom` (lib) due to 4 previous errors
warning: build failed, waiting for other jobs to finish...
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/home/aqua/Desktop/libsignal-0.76.7/rust/protocol" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

After roaming on the internet i tried this by patching a /rust/protocol crate Cargo.toml

[patch.crates-io]
getrandom = {version = "0.2", features = ["js"]}

and get very same error. so i update the getrandom version from 0.2 to 0.3

[patch.crates-io]
getrandom = {version = "0.3", features = ["wasm_js"]}

i also tried by not patching just declaring as dependency.

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

and get very same error. so i update the getrandom version from 0.2 to 0.3

[dependencies]
getrandom = {version = "0.3", features = ["wasm_js"]}

i also tried patching root folder Cargo.toml file and get following error.

[patch.crates-io]
getrandom = {version = "0.2", features = ["js"]}
# tried both of this version
# getrandom = {version = "0.3", features = ["wasm_js"]}

And get following error.

aqua@aqua-server:~/Desktop/libsignal-0.76.7/rust/protocol$ wasm-pack build --target web --out-dir ../pkg_wasm
Error: `cargo metadata` exited with an error: warning: patch for `getrandom` uses the features mechanism. default-features and features will not take effect because the patch dependency does not support this mechanism
error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`

Caused by:
  patch for `getrandom` in `https://github.com/rust-lang/crates.io-index` points to the same source, but patches must point to different sources

Caused by: `cargo metadata` exited with an error: warning: patch for `getrandom` uses the features mechanism. default-features and features will not take effect because the patch dependency does not support this mechanism
error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`

Caused by:
  patch for `getrandom` in `https://github.com/rust-lang/crates.io-index` points to the same source, but patches must point to different sources

Instead patching i tried to declare as dependency on root Cargo.toml, and that leads very same error.

i also tried as describe in getrandom documentation, there were no config.toml so i created a new one and paste this on .cargo/config.toml

[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

and try to build using following rustflags

RUSTFLAGS='--cfg getrandom_backend="linux_getrandom"' cargo build --release --target wasm32-unknown-unknown

Here if i build native lib it would be success but if i try to generate wasm file i get very same error i stuck with.

aqua@aqua-server:~/Desktop/libsignal-0.76.7/rust/protocol$ RUSTFLAGS="--cfg getrandom_wasm_js" cargo build --release --target wasm32-unknown-unknown
warning: patch for the non root package will be ignored, specify patch at the workspace root:
package:   /home/aqua/Desktop/libsignal-0.76.7/rust/protocol/Cargo.toml
workspace: /home/aqua/Desktop/libsignal-0.76.7/Cargo.toml
   Compiling proc-macro2 v1.0.94
   Compiling unicode-ident v1.0.18
   Compiling cfg-if v1.0.0
   Compiling log v0.4.27
   Compiling wasm-bindgen-shared v0.2.100
   Compiling bumpalo v3.17.0
   Compiling quote v1.0.40
   Compiling syn v2.0.100
   Compiling version_check v0.9.5
   Compiling typenum v1.18.0
   Compiling generic-array v0.14.7
   Compiling wasm-bindgen v0.2.100
   Compiling once_cell v1.21.3
   Compiling getrandom v0.3.3
   Compiling subtle v2.6.1
error: The wasm32-unknown-unknown targets are not supported by default; you may need to enable the "wasm_js" configuration flag. Note that enabling the `wasm_js` feature flag alone is insufficient. For more information see: https://docs.rs/getrandom/0.3.3/#webassembly-support
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/backends.rs:168:9
    |
168 | /         compile_error!(concat!(
169 | |             "The wasm32-unknown-unknown targets are not supported by default; \
170 | |             you may need to enable the \"wasm_js\" configuration flag. Note \
171 | |             that enabling the `wasm_js` feature flag alone is insufficient. \
172 | |             For more information see: \
173 | |             https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
174 | |         ));
    | |__________^

error[E0425]: cannot find function `fill_inner` in module `backends`
  --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:99:19
   |
99 |         backends::fill_inner(dest)?;
   |                   ^^^^^^^^^^ not found in `backends`

error[E0425]: cannot find function `inner_u32` in module `backends`
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:128:15
    |
128 |     backends::inner_u32()
    |               ^^^^^^^^^ not found in `backends`
    |
help: consider importing this function
    |
33  + use crate::util::inner_u32;
    |
help: if you import `inner_u32`, refer to it directly
    |
128 -     backends::inner_u32()
128 +     inner_u32()
    |

error[E0425]: cannot find function `inner_u64` in module `backends`
   --> /home/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.3/src/lib.rs:142:15
    |
142 |     backends::inner_u64()
    |               ^^^^^^^^^ not found in `backends`
    |
help: consider importing this function
    |
33  + use crate::util::inner_u64;
    |
help: if you import `inner_u64`, refer to it directly
    |
142 -     backends::inner_u64()
142 +     inner_u64()
    |

For more information about this error, try `rustc --explain E0425`.
error: could not compile `getrandom` (lib) due to 4 previous errors
warning: build failed, waiting for other jobs to finish...

so i tired to identify which package causing an error using cargo tree

$ cargo tree | grep getrandom

aqua@aqua-server:~/Desktop/libsignal-0.76.7/rust/protocol$ cargo tree |  grep getrandom
warning: patch for the non root package will be ignored, specify patch at the workspace root:
package:   /home/aqua/Desktop/libsignal-0.76.7/rust/protocol/Cargo.toml
workspace: /home/aqua/Desktop/libsignal-0.76.7/Cargo.toml
│   │   │   │   └── getrandom v0.2.16
├── getrandom v0.2.16 (*)
│       │       └── getrandom v0.3.3
│           ├── getrandom v0.3.3 (*)

and i explore more on it using cargo features and get the several crate that use getrandom without enabling js features that include rand_core, aes-gcm-siv that causing an error.

I also tried clearing cache by following command.

rm -rf ~/.cargo/registry ~/.cargo/git

let me knw i can share more insight/logs on it, any help would be appreciate.

You can set the features for both 0.2 and 0.3 getrandom using the package key in your Cargo.toml (update: in the crate, not in the workspace). This is what I used when I had this problem:

[target.wasm32-unknown-unknown.dependencies]
getrandom_v2 = { version = "0.2", features = ["js"], package = "getrandom" }
getrandom    = { version = "0.3", features = ["wasm_js"] }

And keep your .cargo/config.toml setting:

[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

I don't think you need to set RUSTFLAGS on the command line (and it may be interfering with the config.toml setting -- I don't actually know about that, this is just a guess).

[patch] can't be used to set Cargo features. That's a completely wrong direction.

Usually you can set features by adding to your [dependencies]. The features are global (per major version, and 0.x are major in Cargo), so setting them in one package sets them for everyone.

getrandom is just weird with their custom cfg requirement.

1 Like

yeah i see but have to goes through it. i tried everything and failed again and again.

there is no [package] but there is a [workspace.package] and when i declare this,

[target.wasm32-unknown-unknown.dependencies]
getrandom_v2 = { version = "0.2", features = ["js"], package = "getrandom" }
getrandom    = { version = "0.3", features = ["wasm_js"] }

its give me an error

cargo build --target wasm32-unknown-unknown --features wasm

error: failed to parse manifest at `/Users/careywoods/Desktop/libsignal-vv/Cargo.toml`

Caused by:
  this virtual manifest specifies a `target` section, which is not allowed

Sorry, put that in your crate Cargo.toml, not in the workspace.

The root problem lies in the weirdness of the wasm32-unknown-unknown target with its pseudo-std and in the lack of global/exclusive feature support in Cargo. Introduction of a proper Web WASM target would also help, e.g. getrandom supports WASI out of box without any issues.

thanks, i put it as suggested.

libsignal-vv % cargo build --target wasm32-unknown-unknown --features wasm

error: none of the selected packages contains this feature: wasm
selected packages: signal-crypto, device-transfer, signal-media, libsignal-message-backup, libsignal-account-keys, libsignal-protocol, usernames, poksho, zkgroup, zkcredential
help: there is a similarly named feature: rand

Then don't include that.

first of all after week i made some progress and get new error.

   Compiling libsignal-message-backup v0.1.0 (/Users/aqua/Desktop/libsignal-vv/rust/message-backup)
   Compiling signal-media v0.1.0 (/Users/aqua/Desktop/libsignal-vv/rust/media)
error: failed to run custom build command for `boring-sys v4.15.0 (https://github.com/signalapp/boring?tag=signal-v4.15.0#bb42da53)`

Caused by:
  process didn't exit successfully: `/Users/aqua/Desktop/libsignal-vv/target/debug/build/boring-sys-4ebcb004f6a98ad6/build-script-main` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=BORING_BSSL_PATH_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_PATH_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_INCLUDE_PATH_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_INCLUDE_PATH_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_INCLUDE_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_INCLUDE_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_SOURCE_PATH_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_SOURCE_PATH_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_SOURCE_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_SOURCE_PATH
  cargo:rerun-if-env-changed=BORING_BSSL_PRECOMPILED_BCM_O_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_PRECOMPILED_BCM_O_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_PRECOMPILED_BCM_O
  cargo:rerun-if-env-changed=BORING_BSSL_PRECOMPILED_BCM_O
  cargo:rerun-if-env-changed=BORING_BSSL_ASSUME_PATCHED_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_ASSUME_PATCHED_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_ASSUME_PATCHED
  cargo:rerun-if-env-changed=BORING_BSSL_ASSUME_PATCHED
  cargo:rerun-if-env-changed=BORING_BSSL_SYSROOT_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_SYSROOT_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_SYSROOT
  cargo:rerun-if-env-changed=BORING_BSSL_SYSROOT
  cargo:rerun-if-env-changed=BORING_BSSL_COMPILER_EXTERNAL_TOOLCHAIN_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_COMPILER_EXTERNAL_TOOLCHAIN_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_COMPILER_EXTERNAL_TOOLCHAIN
  cargo:rerun-if-env-changed=BORING_BSSL_COMPILER_EXTERNAL_TOOLCHAIN
  cargo:rerun-if-env-changed=DEBUG_x86_64-apple-darwin
  cargo:rerun-if-env-changed=DEBUG_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_DEBUG
  cargo:rerun-if-env-changed=DEBUG
  cargo:rerun-if-env-changed=OPT_LEVEL_x86_64-apple-darwin
  cargo:rerun-if-env-changed=OPT_LEVEL_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_OPT_LEVEL
  cargo:rerun-if-env-changed=OPT_LEVEL
  cargo:rerun-if-env-changed=ANDROID_NDK_HOME_x86_64-apple-darwin
  cargo:rerun-if-env-changed=ANDROID_NDK_HOME_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_ANDROID_NDK_HOME
  cargo:rerun-if-env-changed=ANDROID_NDK_HOME
  cargo:rerun-if-env-changed=CMAKE_TOOLCHAIN_FILE_x86_64-apple-darwin
  cargo:rerun-if-env-changed=CMAKE_TOOLCHAIN_FILE_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_CMAKE_TOOLCHAIN_FILE
  cargo:rerun-if-env-changed=CMAKE_TOOLCHAIN_FILE
  cargo:rerun-if-env-changed=BORING_BSSL_RUST_CPPLIB_x86_64-apple-darwin
  cargo:rerun-if-env-changed=BORING_BSSL_RUST_CPPLIB_x86_64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_BORING_BSSL_RUST_CPPLIB
  cargo:rerun-if-env-changed=BORING_BSSL_RUST_CPPLIB
  Initialized empty Git repository in /Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out/boringssl/.git/

  CMAKE_TOOLCHAIN_FILE_wasm32-unknown-unknown = None
  CMAKE_TOOLCHAIN_FILE_wasm32_unknown_unknown = None
  TARGET_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_wasm32-unknown-unknown = None
  CMAKE_GENERATOR_wasm32_unknown_unknown = None
  TARGET_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  CMAKE_PREFIX_PATH_wasm32-unknown-unknown = None
  CMAKE_PREFIX_PATH_wasm32_unknown_unknown = None
  TARGET_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = None
  CMAKE_wasm32-unknown-unknown = None
  CMAKE_wasm32_unknown_unknown = None
  TARGET_CMAKE = None
  CMAKE = None
  running: cd "/Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out/build" && CMAKE_BUILD_PARALLEL_LEVEL="12" CMAKE_PREFIX_PATH="" LC_ALL="C" "cmake" "/Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out/boringssl" "-DCMAKE_CROSSCOMPILING=true" "-DCMAKE_C_COMPILER_TARGET=wasm32-unknown-unknown" "-DCMAKE_CXX_COMPILER_TARGET=wasm32-unknown-unknown" "-DCMAKE_ASM_COMPILER_TARGET=wasm32-unknown-unknown" "-DCMAKE_INSTALL_PREFIX=/Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fno-exceptions --target=wasm32-unknown-unknown" "-DCMAKE_C_COMPILER=/usr/bin/clang" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fno-exceptions --target=wasm32-unknown-unknown" "-DCMAKE_CXX_COMPILER=/usr/bin/clang++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fno-exceptions --target=wasm32-unknown-unknown" "-DCMAKE_ASM_COMPILER=/usr/bin/clang" "-DCMAKE_BUILD_TYPE=Debug"
  -- The C compiler identification is AppleClang 17.0.0.17000013
  -- The CXX compiler identification is AppleClang 17.0.0.17000013
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - failed
  -- Check for working C compiler: /usr/bin/clang
  -- Check for working C compiler: /usr/bin/clang - broken
  -- Configuring incomplete, errors occurred!

  --- stderr

  CMake Warning (dev) at /usr/local/share/cmake/Modules/CMakeDetermineSystem.cmake:175 (message):
    CMAKE_CROSSCOMPILING has been set by the project, toolchain file, or user.
    CMake is resetting it to false because CMAKE_SYSTEM_NAME was not set.  To
    indicate cross compilation, only CMAKE_SYSTEM_NAME needs to be set.
  Call Stack (most recent call first):
    CMakeLists.txt:19 (project)
  This warning is for project developers.  Use -Wno-dev to suppress it.

  CMake Error at /usr/local/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
    The C compiler

      "/usr/bin/clang"

    is not able to compile a simple test program.

    It fails with the following output:

      Change Dir: '/Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out/build/CMakeFiles/CMakeScratch/TryCompile-ZTcCgg'
      
      Run Build Command(s): /usr/local/bin/cmake -E env VERBOSE=1 /usr/bin/make -f Makefile cmTC_76d60/fast
      /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_76d60.dir/build.make CMakeFiles/cmTC_76d60.dir/build
      Building C object CMakeFiles/cmTC_76d60.dir/testCCompiler.c.o
      /usr/bin/clang --target=wasm32-unknown-unknown   -ffunction-sections -fdata-sections -fno-exceptions --target=wasm32-unknown-unknown  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -MD -MT CMakeFiles/cmTC_76d60.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_76d60.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_76d60.dir/testCCompiler.c.o -c /Users/aqua/Desktop/libsignal-vv/target/wasm32-unknown-unknown/debug/build/boring-sys-ff4b1e04739f91d8/out/build/CMakeFiles/CMakeScratch/TryCompile-ZTcCgg/testCCompiler.c
      error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
      1 error generated.
      make[1]: *** [CMakeFiles/cmTC_76d60.dir/testCCompiler.c.o] Error 1
      make: *** [cmTC_76d60/fast] Error 2
      
      



    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt:19 (project)



  thread 'main' panicked at /Users/aqua/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/cmake-0.1.48/src/lib.rs:975:5:

  command did not execute successfully, got: exit status: 1

  build script failed, must exit now
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

Build success, now i can export single crate wasm, for multiple crate i can maybe manage that thanks a lot @duelafn for your quick response and guidance. now i experiment.

to export a wasm i ran following command after build success.

wasm-pack build --target web

this will quickly export a .wasm file.

What did you do to get boring ssl to compile?. I'm stuck there. Any chance you could share your full build process? Are you planning on publishing this wasm build somewhere? Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.