Skip to content

Ergonomics improvements for alternative backends #671

@josephlr

Description

@josephlr

As noted in many issues (#658, bevyengine/bevy#18047, https://users.rust-lang.org/t/getrandom-0-3-2-wasm-js-feature-issue/127584, n0-computer/iroh#3200), the use of a Rust --cfg flag can be unergonomic. See the current docs about this for context.

This is mostly due to how tooling (rustc, cargo, etc...) interact with RUSTFLAGS and the rustc command line. This is exacerbated by the fact that the wasm32-unknown-unknown target cannot have a default supported backend, as it is used in many environments (some of which do not have JavaScript). Leading to an unfortunate situation where, if someone wants to build any code which depends on getrandom targeting wasm32-unknown-unknown, they must:

  • Modify Cargo.toml to include:
    [dependencies]
    getrandom = { version = "0.3", features = ["wasm_js"] }
  • Modify .cargo/config.toml to include:
    [target.wasm32-unknown-unknown]
    rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

This issue is to explore and evaluate approaches like #670 which seek to improve this story, either in 0.3 or in a future major release. It may turn out we end up keeping the current approach for a future major release, but we should explore all reasonable alternatives.

Requirements

A bunch of the ergonomics issues stem from the requirements for this crate. Some of these requirements might be debatable or less important, but ideally any solution would have the following properties (note that the current approach only has most of these):

  • Without using any alternative backend, a user shouldn't have to do anything. All the supported backends should work, and the user should get good error messages if building on an unsupported platform.
  • Overriding the default backend should be possible.
  • You cannot declare/enable multiple alternative backends (for any given target).
  • For security, it should be difficult/impossible for a crate deep in the dependency tree to override the default source of randomness.
  • The approach should encourage "leaf" crates (i.e. binary crates) to select the backend, rather than core crates.
  • wasm32-unknown-unknown should not be special-cased and should not call JavaScript by default (as the target makes it very clear that cannot be assumed).
  • Any dependencies used by an alternative backend should not be present in the user's Cargo.lock if that backend is not being used.
  • The approach should be similar or analogous to #[global_allocator], as we are defining a global source of randomness.
  • We should not conflict with Tracking Issue for secure random data generation in std rust-lang/rust#130703, and ideally have a solution which works well even if that feature gets stabilized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions