Skip to content

Conversation

@martinfrances107
Copy link
Contributor

@martinfrances107 martinfrances107 commented Dec 15, 2025

I see this when building for the browser

The problems is side-effects from changes inside of the getrandom crate.

  1. Here is a dependency graph

MyApp -> MyLib -> geo -> rand -> getrandom

getrandom The change here releates to the sourcing of randomness for "supported targets" Here is the relevant quote from the getrandom README

 "wasm32-unknown-unknown" is no longer supported by default. see
(webassembly-support)[<https://crates.io/crates/getrandom#webassembly-support>]

For this PR the summary is

rand must enable the "wasm-js" and the root crate ( MyApp in this example ) must set this enviromment variable

RUSTFLAGS='--cfg getrandom_backend="wasm_js"'

rand takes its own view on this matter

Here is the relevant quote form the rand README

"The WASI and Emscripten targets are directly supported. The wasm32-unknown-unknown target is not automatically supported. To enable support for this target, refer to the getrandom documentation for WebAssembly. Alternatively, the os_rng feature may be disabled."

The best we can do, in geo, I think, is to optionally remove the rand crate, for builds that target the web.

geo = { path ="0.32", features = [ "earcutr", "spade", "multithreading"], default-features = false }

I should empahsise this comes at the cost of removing the kmeans module and its associated functionality.

  • [ x] I agree to follow the project's code of conduct.
  • I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

… the build".

The problems is side-effects from changes inside of the getrandom crate.

1) Here is a dependency graph

MyApp -> MyLib -> geo -> rand -> getrandom

1) [getrandom](<https://crates.io/crates/getrandom>) The change releates to the sourcing of randomness for "supported targets"
Here is the relevant quote from the getrandoms README

 "wasm32-unknown-unknown" is no longer supported by default. see
(webassembly-support)[<https://crates.io/crates/getrandom#webassembly-support>]

For this PR the summary is

rand must enable the "wasm-js" and the root crate ( MyApp in this example )
must set this enviromment variable

RUSTFLAGS='--cfg getrandom_backend="wasm_js"'

2) [rand](https://crates.io/crates/rand>) takes its own view on this matter

Here is the relevant quote form the rand README

"The WASI and Emscripten targets are directly supported. The wasm32-unknown-unknown target is not automatically supported. To enable support for this target, refer to the getrandom documentation for WebAssembly. Alternatively, the os_rng feature may be disabled."

The best we can do, in geo, I think, is to optionally remove the rand crate, for builds that target the web.

geo = { path ="0.32", features =  [ "earcutr", "spade", "multithreading"], default-features = false }

*I should empahsise* this comes at the cost of removing the kmeans crate and its associated functionality.
@dabreegster
Copy link
Contributor

As a workaround, you can add getrandom = { version = "0.2.16", features = ["js"] } to your crate. This was discussed a bit on Discord.

@martinfrances107
Copy link
Contributor Author

WOW .. thank you, thank you

That works and fixes a problem I have been thinking about for weeks. 😍

Just so I understand you and for other people reading this

in the MyApp Cargo.toml file from above

MyApp -> MyLib -> geo -> rand -> getrandom

When I add this

# Create a shadow dependency on getrandom with the feature "js"
# In effect this will add the "js" feature flag to the real depedency
#  MyApp -> MyLib -> geo -> rand -> genrandom
getrandom = { version = "0.2.16", features = ["js"]}

My response is this is brittle but ok

It needs to be updated with the changing winds from getrandom, which makes me sad.
When I see crazy/sneaky/"Not at all obvious" that is when I document.

I will adjust this PR sometime in this evening... limiting it to just adding a section to to README

@dabreegster
Copy link
Contributor

I think this PR is a nice solution, since it stops downstream users from needing this workaround. Opting into a feature to get kmeans seems much more typical. I'll let other maintainers chime in on the best way forward

@martinfrances107
Copy link
Contributor Author

I have stripped down the PR to just documentation.

I see that the rand crate needs bumping to the latest version ...
one step at a time .. but just to be here clear here I am leaving a maintenance behind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants