Skip to content

Conversation

TimJentzsch
Copy link
Collaborator

@TimJentzsch TimJentzsch commented Aug 11, 2025

Objective

Closes #546.

Automatically configure the getrandom web backend when necessary, to avoid build failures.

Solution

  • Detect usage of getrandom (and whether the backend is applied) via cargo metadata
  • Configure the feature flag via --config flags to pass to cargo build
  • Configure the backend by adding to the RUSTFLAGS

Note: Blocked on #540 to add to the RUSTFLAGS instead of overwriting them!

Testing

First, install the CLI from this branch:

cargo install --git https://github.com/TheBevyFlock/bevy_cli --branch 546-getrandom-fix --locked bevy_cli

Then execute bevy run web on a package that doesn't have the getrandom backend configured.
This most often happens on the in-development Bevy 0.17.

One good example is the breakout example in the bevy repository itself.
Try running this:

bevy run --example=breakout web --verbose

You should see an info log "automatically configuring getrandom web backend" and also see that the fix is applied to the cargo build command.

@TimJentzsch
Copy link
Collaborator Author

This is now functional and can be tested already, but it's blocked on #540.
Otherwise, it will overwrite RUSTFLAGS configured in the project.

@TimJentzsch TimJentzsch added C-Usability An improvement that makes the API more pleasant S-Blocked This cannot move forward until something else changes A-Web Building or running Bevy apps targeting the browser D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels Aug 12, 2025
@Bluefinger
Copy link

Just tested this with the new examples I am doing for bevy_rand. Able to build with no issues, and my examples run as expected when using the JS API to pull entropy from (instead of a set seed).

@TimJentzsch TimJentzsch marked this pull request as ready for review August 17, 2025 18:51
@TimJentzsch TimJentzsch requested a review from DaAlbrecht August 17, 2025 18:52
@BD103 BD103 added S-Needs-Review The PR needs to be reviewed before it can be merged and removed S-Blocked This cannot move forward until something else changes labels Aug 18, 2025
@BD103
Copy link
Member

BD103 commented Aug 18, 2025

#540 is no longer blocked, so this can be reviewed :)

- Setting `build.rustflags` in `Cargo.toml`
- Setting the `RUSTFLAGS` env variable when running `cargo`

However, not that the rustflags are not merged, but _overwritten_.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
However, not that the rustflags are not merged, but _overwritten_.
However, note that the rustflags are not merged, but _overwritten_.

Copy link
Collaborator

@DaAlbrecht DaAlbrecht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! I only got it to work for the current main branch of bevy

This does not work for me

[dependencies]
bevy = "0.16.1"
rand_core = "0.9.3"
rand = "0.9.2"

It should not be a problem that we have multiple versions of getrandom right?

❯ cargo tree -i getrandom
error: There are multiple `getrandom` packages in your project, and the specification `getrandom` is ambiguous.
Please re-run this command with one of the following specifications:
  [email protected]
  [email protected]

@@ -33,6 +34,13 @@ pub fn build_web(
profile_args.append(&mut args.cargo_args.common_args.config);
args.cargo_args.common_args.config = profile_args;

// Apply the `getrandom` web backend if necessary
if let Some(target) = args.target()
Copy link
Collaborator

@DaAlbrecht DaAlbrecht Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to check if the target specified is actually wasm32-unkown-unkown (or perhaps we should just overwrite the flag to wasm32-unkown-unkown when using the web subcommand)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also other potential web targets like wasm32v1-none, so I think it's enough to know that the user used a web subcommand

fn add_feature_config(config: &mut Vec<String>, version: &Version, feature: &str) {
// Distinguish entries by version to allow multiple versions to be configured
let table = format!("dependencies.getrandom_{}{}", version.major, version.minor);
// The config arg doesn't support inline tables, so each entries must be configured separately
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

Suggested change
// The config arg doesn't support inline tables, so each entries must be configured separately
// The config arg doesn't support inline tables, so each entry must be configured separately

@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

### Added

- The `bevy build web` and `bevy run web` commands will now automatically apply the web backend for `getrandom` if necessary. It requires both a feature and a rustflag to be enabled by the user, which can quickly lead to compile errors when not set up correctly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's just me as a none native speaker but at first it read for me like

"for bevy build web to automatically configure it, you have to enable a feature and some rustflags" until I then realize that the "It" refers to getrandom.

@TimJentzsch
Copy link
Collaborator Author

This looks good! I only got it to work for the current main branch of bevy

This does not work for me

[dependencies]
bevy = "0.16.1"
rand_core = "0.9.3"
rand = "0.9.2"

It should not be a problem that we have multiple versions of getrandom right?

❯ cargo tree -i getrandom
error: There are multiple `getrandom` packages in your project, and the specification `getrandom` is ambiguous.
Please re-run this command with one of the following specifications:
  [email protected]
  [email protected]

This should work, I will look into it.
Which project did you use to get this?

@TimJentzsch TimJentzsch added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review The PR needs to be reviewed before it can be merged labels Aug 25, 2025
@DaAlbrecht
Copy link
Collaborator

This should work, I will look into it. Which project did you use to get this?

Created a new project with cargo new

@DaAlbrecht DaAlbrecht added this to the `bevy_cli` v0.1.0-alpha.2 milestone Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Web Building or running Bevy apps targeting the browser C-Usability An improvement that makes the API more pleasant D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically set getrandom backend for web builds
4 participants