{"record":{"id":"e633513e0fad29ce","repo":"clockworklabs/SpacetimeDB","slug":"getrandom-usage-detected-it-seems-like-either-yo","errorCode":null,"errorMessage":"getrandom usage detected.\n\nIt seems like either you or a crate in your dependency tree is depending on\nthe `getrandom` crate for random number generation. getrandom is the default\nrandomness source for the `rand` crate, and is used when you call\n`rand::random()` or `rand::thread_rng()`. If this is you, you should instead\nuse `ctx.rng()` on a `ReducerContext`. If this is a crate in your\ntree, you should try to see if the crate provides a way to pass in a custom\n`Rng` type, and pass it the rng returned from `ctx.rng()`.","messagePattern":"getrandom usage detected\\.\n\nIt seems like either you or a crate in your dependency tree is depending on\nthe `getrandom` crate for random number generation\\. getrandom is the default\nrandomness source for the `rand` crate, and is used when you call\n`rand::random\\(\\)` or `rand::thread_rng\\(\\)`\\. If this is you, you should instead\nuse `ctx\\.rng\\(\\)` on a `ReducerContext`\\. If this is a crate in your\ntree, you should try to see if the crate provides a way to pass in a custom\n`Rng` type, and pass it the rng returned from `ctx\\.rng\\(\\)`\\.","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/cli/src/tasks/rust.rs","lineNumber":133,"sourceCode":"    };\n    let Ok(module) = wasmbin::Module::decode_from(&mut io::BufReader::new(file)) else {\n        return Ok(());\n    };\n    if has_wasm_bindgen(&module) {\n        anyhow::bail!(\n            \"wasm-bindgen detected.\\n\\\n             \\n\\\n             It seems like either you or a crate in your dependency tree is depending on\\n\\\n             wasm-bindgen. wasm-bindgen is only for webassembly modules that target the web\\n\\\n             platform, and will not work in the context of SpacetimeDB.\\n\\\n             \\n\\\n             To find the offending dependency, run `cargo tree -i wasm-bindgen`. Try checking\\n\\\n             its cargo features for 'js' or 'web' or 'wasm-bindgen' to see if there's a way\\n\\\n             to disable it.\"\n        )\n    }\n    if has_getrandom(&module) {\n        anyhow::bail!(\n            \"getrandom usage detected.\\n\\\n             \\n\\\n             It seems like either you or a crate in your dependency tree is depending on\\n\\\n             the `getrandom` crate for random number generation. getrandom is the default\\n\\\n             randomness source for the `rand` crate, and is used when you call\\n\\\n             `rand::random()` or `rand::thread_rng()`. If this is you, you should instead\\n\\\n             use `ctx.rng()` on a `ReducerContext`. If this is a crate in your\\n\\\n             tree, you should try to see if the crate provides a way to pass in a custom\\n\\\n             `Rng` type, and pass it the rng returned from `ctx.rng()`.\"\n        )\n    }\n    Ok(())\n}\n\nconst WBINDGEN_PREFIX: &str = \"__wbindgen\";\nfn has_wasm_bindgen(module: &wasmbin::Module) -> bool {\n    let check_import = |import: &wasmbin::sections::Import| {\n        import.path.module.starts_with(WBINDGEN_PREFIX) || import.path.name.starts_with(WBINDGEN_PREFIX)","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/cli/src/tasks/rust.rs#L115-L151","documentation":"SpacetimeDB modules are deterministic and sandboxed: OS entropy is not available, so the `getrandom` backend that the `rand` crate uses by default cannot be satisfied by the host. check_for_issues detects the `__getrandom_custom` import in the compiled wasm and rejects the module, directing you to the host-provided RNG on ReducerContext. Reducers that need randomness must use ctx.rng().","triggerScenarios":"Module code calling rand::random() or rand::thread_rng(); or a dependency that internally sources entropy itself (uuid::Uuid::new_v4 with default `v4` features, nanoid-style crates, anything constructing OsRng) rather than accepting an injected Rng.","commonSituations":"Porting game code that rolls dice with rand::random(); generating UUIDs server-side in a reducer; a helper crate that defaults to ThreadRng/OsRng internally.","solutions":["Replace rand::random()/thread_rng() with the reducer's ReducerContext RNG: `use rand::Rng; let n = ctx.rng().gen();`","For dependencies, pass randomness in: construct them with an API that accepts a custom Rng and give it `ctx.rng()` (or StdRng::from_rng(ctx.rng()))","Disable random-sourcing features (e.g. uuid's `v4`) and generate the IDs from ctx.rng() yourself","Rebuild and publish"],"exampleFix":"// before\nlet roll: u32 = rand::random();\n\n// after\nuse rand::Rng;\nlet roll: u32 = ctx.rng().gen(); // ctx: ReducerContext","handlingStrategy":"validation","validationCode":"#!/usr/bin/env bash\n# module code must not source OS entropy directly\nif grep -RnE 'rand::random|thread_rng|OsRng' --include='*.rs' src/; then\n  echo 'ERROR: use ctx.rng() instead of OS randomness' >&2\n  exit 1\nfi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always take ReducerContext in reducers that need randomness and use ctx.rng()","Disable default random features (uuid's v4, etc.) on module dependencies","Prefer APIs that accept an injected Rng when choosing helper crates","Grep for rand usage in code review of ported code"],"tags":["rust","wasm","randomness","dependencies","publish"],"backgroundTag":"getrandom-unavailable-in-wasm","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}