From 30d0f137b172f1e74829ca58910010b5b6bfe9d0 Mon Sep 17 00:00:00 2001 From: Kris Jenkins Date: Wed, 18 Nov 2026 10:56:31 +0000 Subject: [PATCH] Stop 'spacetime init' copying a broken generate script into TypeScript projects. Say you scaffold a project with 'spacetime init --template react-ts' (or any of the twelve TypeScript client templates), change your module, and want fresh bindings. package.json has an obvious-looking 'generate' script, so you run 'npm run generate'. It fails: "generate": "pnpm --dir spacetimedb install && cargo run -p gen-bindings -- --out-dir src/module_bindings --module-path spacetimedb && prettier --write src/module_bindings" gen-bindings is a tool in the SpacetimeDB repository's Cargo workspace. The templates use it to regenerate their checked-in bindings, and the repo's own CI calls it through this script ('pnpm generate' in chat-react-ts, and in react-ts via bindings-typescript). Outside the repo there is no such crate, and usually no Cargo at all. The script that actually works for users, 'spacetime:generate', sits a line below it, but the broken one has the name people reach for. Nobody caught it because it works for everyone who maintains the templates: inside the monorepo the script runs fine. 'spacetime init' copies each template's package.json unchanged apart from the name and the spacetimedb version, and the template smoketests only install and publish. Renaming the script in every template would mean changing the repo's CI and SDK scripts that call it. Instead, update_package_json, which already rewrites each template's package.json during init, now drops any script that invokes gen-bindings. The monorepo keeps its workflow, and scaffolded projects get only scripts that can run. A new test runs update_package_json over every embedded template's package.json. It checks that each gen-bindings script is removed and every other script is kept unchanged. It also fails if no template uses gen-bindings any more, so the marker can't go stale silently. Removing the call makes it fail, naming money-exchange-react-ts. Scaffolding svelte-ts with the rebuilt CLI produces a package.json without 'generate' and with its other scripts intact. --- crates/cli/src/subcommands/init.rs | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/crates/cli/src/subcommands/init.rs b/crates/cli/src/subcommands/init.rs index 0411393788f..0d9643a409d 100644 --- a/crates/cli/src/subcommands/init.rs +++ b/crates/cli/src/subcommands/init.rs @@ -1093,6 +1093,23 @@ fn to_major_minor_patch_wildcard(version: &str) -> String { } } +/// Templates keep a `generate` script that regenerates their checked-in +/// bindings with `cargo run -p gen-bindings`. That tool only exists inside the +/// SpacetimeDB repository, so in a user's project the script can only fail. +/// Users regenerate bindings with `spacetime generate` (or the template's +/// `spacetime:generate` script) instead. +const MONOREPO_ONLY_SCRIPT_MARKER: &str = "gen-bindings"; + +fn remove_monorepo_only_scripts(package: &mut serde_json::Value) { + if let Some(scripts) = package.get_mut("scripts").and_then(|s| s.as_object_mut()) { + scripts.retain(|_, command| { + !command + .as_str() + .is_some_and(|command| command.contains(MONOREPO_ONLY_SCRIPT_MARKER)) + }); + } +} + fn update_package_json(dir: &Path, package_name: &str) -> anyhow::Result<()> { let package_path = dir.join("package.json"); if !package_path.exists() { @@ -1111,6 +1128,8 @@ fn update_package_json(dir: &Path, package_name: &str) -> anyhow::Result<()> { deps["spacetimedb"] = json!(to_major_minor_patch_wildcard(get_spacetimedb_typescript_version())); } + remove_monorepo_only_scripts(&mut package); + let updated_content = serde_json::to_string_pretty(&package)?; fs::write(package_path, updated_content)?; @@ -2471,6 +2490,66 @@ mod tests { ); } + #[test] + fn test_update_package_json_removes_monorepo_only_scripts_from_every_template() { + let mut templates_with_monorepo_scripts = 0; + for (source, files) in embedded::get_template_files() { + let Some(original) = files.get("package.json") else { + continue; + }; + let original: serde_json::Value = serde_json::from_str(original).unwrap(); + let original_scripts = original["scripts"].as_object().cloned().unwrap_or_default(); + if original_scripts + .values() + .any(|command| command.as_str().unwrap_or("").contains("gen-bindings")) + { + templates_with_monorepo_scripts += 1; + } + + let temp = tempfile::TempDir::new().unwrap(); + let package_json = temp.path().join("package.json"); + std::fs::write(&package_json, serde_json::to_string(&original).unwrap()).unwrap(); + update_package_json(temp.path(), "my-app").unwrap(); + let updated: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string(&package_json).unwrap()).unwrap(); + let updated_scripts = updated["scripts"].as_object().cloned().unwrap_or_default(); + + for (name, command) in &original_scripts { + let command = command.as_str().unwrap_or(""); + if command.contains("gen-bindings") { + assert!( + !updated_scripts.contains_key(name), + "{source}: script `{name}` needs the SpacetimeDB repo but was kept" + ); + } else { + assert_eq!( + updated_scripts.get(name).and_then(|c| c.as_str()), + Some(command), + "{source}: script `{name}` should be kept unchanged" + ); + } + } + } + // Guard against the marker going stale: if no template uses it any + // more, this test no longer checks anything. + assert!( + templates_with_monorepo_scripts > 0, + "no embedded template has a gen-bindings script; update MONOREPO_ONLY_SCRIPT_MARKER" + ); + } + + #[test] + fn test_update_package_json_keeps_package_without_scripts() { + let temp = tempfile::TempDir::new().unwrap(); + let package_json = temp.path().join("package.json"); + std::fs::write(&package_json, r#"{ "name": "old-name" }"#).unwrap(); + + update_package_json(temp.path(), "new-name").unwrap(); + + let parsed: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(package_json).unwrap()).unwrap(); + assert_eq!(parsed, serde_json::json!({ "name": "new-name" })); + } + #[test] fn test_update_cargo_toml_rewrites_spacetimedb_deps_to_patch_wildcards() { let temp = tempfile::TempDir::new().unwrap();