Skip to content

Commit e49dacc

Browse files
authored
Remove all uses of getrandom_backend = "wasm_js" (#771)
Enabling the `wasm_js` crate feature is sufficient.
1 parent 5f7e631 commit e49dacc

File tree

6 files changed

+40
-49
lines changed

6 files changed

+40
-49
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ jobs:
165165
web:
166166
name: ${{ matrix.target.description }} ${{ matrix.feature.description }} ${{ matrix.atomic.description }}
167167
runs-on: ubuntu-24.04
168-
env:
169-
RUSTFLAGS: --cfg getrandom_backend="wasm_js" ${{ matrix.atomic.flags }}
170168
strategy:
171169
fail-fast: false
172170
matrix:

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ jobs:
275275
- {
276276
description: Web,
277277
version: stable,
278-
flags: '-Dwarnings --cfg getrandom_backend="wasm_js"',
278+
flags: '-Dwarnings',
279279
args: '--features=std,sys_rng,wasm_js',
280280
}
281281
- {
282282
description: Web with Atomics,
283283
version: nightly,
284284
components: rust-src,
285-
flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory',
285+
flags: '-Dwarnings -Ctarget-feature=+atomics,+bulk-memory',
286286
args: '--features=std,sys_rng,wasm_js -Zbuild-std=panic_abort,std',
287287
}
288288
steps:

Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ features = ["std", "sys_rng"]
2020
std = []
2121

2222
# Optional backend: wasm_js
23+
#
2324
# This flag enables the wasm_js backend and uses it by default on wasm32 where
2425
# the target_os is unknown. The getrandom_backend cfg may override this.
25-
# WARNING: It is highly recommended to enable this feature only for binary crates and tests,
26-
# i.e. avoid unconditionally enabling it in library crates.
26+
#
27+
# WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
28+
# since it is known to break non-Web WASM builds and further since the usage of `wasm-bindgen`
29+
# causes significant bloat to `Cargo.lock` (on all targets).
30+
#
31+
# The only exception to this rule: if your crate already unconditionally depends on `wasm-bindgen`
32+
# or `js-sys` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
2733
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]
2834

2935
# Provide SysRng over rand_core
@@ -84,7 +90,7 @@ wasm-bindgen-test = "0.3"
8490
[lints.rust.unexpected_cfgs]
8591
level = "warn"
8692
check-cfg = [
87-
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "wasm_js", "windows_legacy", "unsupported"))',
93+
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported"))',
8894
'cfg(getrandom_msan)',
8995
'cfg(getrandom_test_linux_fallback)',
9096
'cfg(getrandom_test_linux_without_fallback)',

README.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
7373

7474
Pull Requests that add support for new targets to `getrandom` are always welcome.
7575

76+
### WebAssembly support
77+
78+
This crate fully supports the [WASI] and [Emscripten] targets. However,
79+
the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
80+
is not automatically supported since, from the target name alone, we cannot deduce
81+
which JavaScript interface should be used (or if JavaScript is available at all).
82+
83+
We do not include support for this target in the default configuration because our JS backend
84+
(supporting web browsers, web workers and Node.js v19 or later) requires [`wasm-bindgen`],
85+
**bloating `Cargo.lock`** and **potentially breaking builds** on non-web WASM platforms.
86+
87+
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using
88+
[`Crypto.getRandomValues`] via [`wasm-bindgen`], enable the `wasm_js` crate feature.
89+
90+
WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
91+
since it is known to break non-Web WASM builds and further since the usage of `wasm-bindgen`
92+
causes significant bloat to `Cargo.lock` (on all targets).
93+
94+
The only exception to this rule: if your crate already unconditionally depends on `wasm-bindgen`
95+
or `js-sys` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
96+
7697
### Opt-in backends
7798

7899
`getrandom` also provides optional (opt-in) backends, which allow users to customize the source
@@ -84,7 +105,6 @@ of randomness based on their specific needs:
84105
| `linux_raw` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses raw `asm!`-based syscalls instead of `libc`.
85106
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
86107
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
87-
| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Enabled by the `wasm_js` feature ([see below](#webassembly-support)).
88108
| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler)
89109
| `windows_legacy` | Windows | `*-windows-*` | [`RtlGenRandom`]
90110
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
@@ -94,8 +114,8 @@ Opt-in backends can be enabled using the `getrandom_backend` configuration flag.
94114
The flag can be set either by specifying the `rustflags` field in [`.cargo/config.toml`]:
95115
```toml
96116
# It's recommended to set the flag on a per-target basis:
97-
[target.wasm32-unknown-unknown]
98-
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
117+
[target.'cfg(target_os = "linux")']
118+
rustflags = ['--cfg', 'getrandom_backend="linux_getrandom"']
99119
```
100120

101121
Or by using the `RUSTFLAGS` environment variable:
@@ -124,29 +144,6 @@ Note that the raw syscall backend may be slower than backends based on `libc::ge
124144
e.g. it does not implement vDSO optimizations and on `x86` it uses the infamously slow
125145
`int 0x80` instruction to perform syscall.
126146

127-
### WebAssembly support
128-
129-
This crate fully supports the [WASI] and [Emscripten] targets. However,
130-
the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
131-
is not automatically supported since, from the target name alone, we cannot deduce
132-
which JavaScript interface should be used (or if JavaScript is available at all).
133-
134-
We do not include support for this target in the default configuration because
135-
our JS backend (supporting web browsers, web workers and Node.js v19 or later)
136-
requires [`wasm-bindgen`], **bloating `Cargo.lock`** and
137-
**potentially breaking builds** on non-web WASM platforms.
138-
139-
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using the Web
140-
Crypto methods [described above][opt-in] via [`wasm-bindgen`], enable the
141-
`wasm_js` feature flag. Setting `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'`
142-
is allowed but is no longer required and does nothing (it was required in a
143-
prior version of this crate).
144-
145-
WARNING: enabling the `wasm_js` feature will bloat `Cargo.lock` on all platforms
146-
(where [`wasm-bindgen`] is not an existing dependency) and is known to cause
147-
build issues on some non-web WASM platforms, even when a different backend is
148-
selected via `getrandom_backend`.
149-
150147
### Custom backend
151148

152149
If this crate does not support your target out of the box or you have to use

src/backends.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ cfg_if! {
2929
} else if #[cfg(getrandom_backend = "windows_legacy")] {
3030
mod windows_legacy;
3131
pub use windows_legacy::*;
32-
} else if #[cfg(getrandom_backend = "wasm_js")] {
33-
cfg_if! {
34-
if #[cfg(feature = "wasm_js")] {
35-
mod wasm_js;
36-
pub use wasm_js::*;
37-
} else {
38-
compile_error!(concat!(
39-
"The \"wasm_js\" backend requires the `wasm_js` feature \
40-
for `getrandom`. For more information see: \
41-
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
42-
));
43-
}
44-
}
4532
} else if #[cfg(getrandom_backend = "unsupported")] {
4633
mod unsupported;
4734
pub use unsupported::*;
@@ -187,8 +174,7 @@ cfg_if! {
187174
} else {
188175
compile_error!(concat!(
189176
"The wasm32-unknown-unknown targets are not supported by default; \
190-
you may need to enable the \"wasm_js\" configuration flag. Note \
191-
that enabling the `wasm_js` feature flag alone is insufficient. \
177+
you may need to enable the \"wasm_js\" crate feature. \
192178
For more information see: \
193179
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
194180
));

src/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ impl Error {
177177
Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure",
178178
#[cfg(all(windows, target_vendor = "win7"))]
179179
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
180-
#[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))]
180+
#[cfg(all(
181+
feature = "wasm_js",
182+
target_arch = "wasm32",
183+
any(target_os = "unknown", target_os = "none")
184+
))]
181185
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
182186
#[cfg(target_os = "vxworks")]
183187
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",

0 commit comments

Comments
 (0)