admin管理员组

文章数量:1399805

TLDR: after discussion with kmdreko, this problem is solvable. The linked github repo has been updated to reflect this.

I am working on a wasm32-unknown-unknown target, and the getrandom crate appears in multiple places and at multiple levels in my cargo tree. It fails to compile with the following:

error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: 
   --> /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:342:9
    |
342 | /         compile_error!("the wasm*-unknown-unknown targets are not supported by \
343 | |                         default, you may need to enable the \"js\" feature. \
344 | |                         For more information see: \
345 | |                         ;);
    | |________________________________________________________________________^

It seems like the cause for this is that I cannot affect the features for a sub-dependency, or a sub-sub-dependency, etc.

Reviewing and attempting to implement solutions in several existing sources gave me unexpected results in my big project, so I created a very simple project using just the age lib, and I am at least hitting the problem consistently now.

This cargo.toml attempts to work like the first source

$ cat Cargo.toml 
[package]
name = "rust-age-testing"
version = "0.1.0"
edition = "2024"

[dependencies]

[dependencies.age]
version = "0.11.1"
features = ["armor"]
getrandom = { features = ["wasm_js"] }

Though getrandom is about 5 levels deep in the dependency graph...

A build comes out like this (note the ignored key):

RUSTFLAGS='--cfg getrandom_backend="wasm_js"' cargo build --target wasm32-unknown-unknown
warning: unused manifest key: dependencies.age.getrandom
   Compiling typenum v1.18.0
   Compiling generic-array v0.14.7
   Compiling cfg-if v1.0.0
   Compiling subtle v2.6.1
   Compiling unic-langid-impl v0.9.5
   Compiling zeroize v1.8.1
   Compiling tinystr v0.7.6
   Compiling unic-langid v0.9.5
   Compiling slab v0.4.9
   Compiling futures-core v0.3.31
   Compiling memchr v2.7.4
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling zerocopy v0.8.23
   Compiling getrandom v0.2.15
   Compiling thiserror v1.0.69
error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: 
   --> /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:342:9
    |
342 | /         compile_error!("the wasm*-unknown-unknown targets are not supported by \
343 | |                         default, you may need to enable the \"js\" feature. \
344 | |                         For more information see: \
345 | |                         ;);
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:398:9
    |
398 |         imp::getrandom_inner(dest)?;
    |         ^^^ use of undeclared crate or module `imp`

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

Do I need to ask all of the dependency maintainers to add feature flags that will control the feature flags that pass to getrandom?

EDIT 1: @kmdreko's suggestion - add direct dependency

$ cargo add getrandom
    Updating crates.io index
      Adding getrandom v0.3.2 to dependencies
             Features:
             - rustc-dep-of-std
             - std
             - wasm_js
    Updating crates.io index
     Locking 4 packages to latest Rust 1.85.0 compatible versions
      Adding getrandom v0.3.2
      Adding r-efi v5.2.0
      Adding wasi v0.14.2+wasi-0.2.4
      Adding wit-bindgen-rt v0.39.0
developer@85ddbd5bb699:~/app$ vim Cargo.toml
developer@85ddbd5bb699:~/app$ cat Cargo.toml 
[package]
name = "rust-age-testing"
version = "0.1.0"
edition = "2024"

[dependencies]
age = { version = "0.11.1", features = ["armor"] }
getrandom = { version="0.3.2",features = ["wasm_js"] 
developer@85ddbd5bb699:~/app$ RUSTFLAGS='--cfg getrandom_backend="wasm_js"' cargo build --target wasm32-unknown-unknown
     Locking 7 packages to latest Rust 1.85.0 compatible versions
      Adding bumpalo v3.17.0
      Adding js-sys v0.3.77
      Adding wasm-bindgen v0.2.100
      Adding wasm-bindgen-backend v0.2.100
      Adding wasm-bindgen-macro v0.2.100
      Adding wasm-bindgen-macro-support v0.2.100
      Adding wasm-bindgen-shared v0.2.100
   Compiling zerocopy v0.8.24
   Compiling getrandom v0.2.15
   Compiling i18n-config v0.4.7
   Compiling find-crate v0.6.3
error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: 
   --> /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:342:9
    |
342 | /         compile_error!("the wasm*-unknown-unknown targets are not supported by \
343 | |                         default, you may need to enable the \"js\" feature. \
344 | |                         For more information see: \
345 | |                         ;);
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:398:9
    |
398 |         imp::getrandom_inner(dest)?;
    |         ^^^ use of undeclared crate or module `imp`

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

As I move through this, I am going to update this github project with the steps taken:

本文标签: