diff --git a/crates/braid/build.rs b/crates/braid/build.rs index b3f3078..add2ec8 100644 --- a/crates/braid/build.rs +++ b/crates/braid/build.rs @@ -77,6 +77,13 @@ fn ensure_stub(dist: &Path) { .expect("could not write ui/dist/index.html stub"); } +/// The npm executable to spawn. Rust's `Command` only appends `.exe` on +/// Windows, so the usual `npm.cmd` shim must be named in full; installs that +/// ship a real `npm.exe` instead (nvm) are found by the plain name. fn npm_cmd() -> &'static str { - if cfg!(windows) { "npm.cmd" } else { "npm" } + let on_path = |name: &str| { + std::env::var_os("PATH") + .is_some_and(|path| std::env::split_paths(&path).any(|dir| dir.join(name).is_file())) + }; + if cfg!(windows) && on_path("npm.cmd") { "npm.cmd" } else { "npm" } } diff --git a/crates/xtask/src/housekeeping.rs b/crates/xtask/src/housekeeping.rs index ff88325..0eb2c58 100644 --- a/crates/xtask/src/housekeeping.rs +++ b/crates/xtask/src/housekeeping.rs @@ -799,8 +799,7 @@ fn check_npm(root: &Path, options: &Options, subcommand: &str) -> Outcome { if !ui.join("package.json").is_file() { return Outcome::skipped("no ui/package.json"); } - let npm = if cfg!(windows) { "npm.cmd" } else { "npm" }; - let out = match tool(npm, &[subcommand], &ui) { + let out = match tool(crate::npm_program(), &[subcommand], &ui) { Ok(out) => out, Err(_) => return Outcome::skipped("needs Node.js / npm"), }; diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index 9dcc89a..34a238e 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -124,6 +124,17 @@ fn run_steps(steps: &[&[&str]]) -> i32 { // build-ui / test-ui // --------------------------------------------------------------------------- +/// The npm executable to spawn. Rust's `Command` only appends `.exe` on +/// Windows, so the usual `npm.cmd` shim must be named in full; installs that +/// ship a real `npm.exe` instead (nvm) are found by the plain name. +fn npm_program() -> &'static str { + let on_path = |name: &str| { + std::env::var_os("PATH") + .is_some_and(|path| std::env::split_paths(&path).any(|dir| dir.join(name).is_file())) + }; + if cfg!(windows) && on_path("npm.cmd") { "npm.cmd" } else { "npm" } +} + /// Run `npm ` in `ui/`, returning a process-style exit code. fn ui_npm(args: &[&str]) -> i32 { let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default(); @@ -134,7 +145,7 @@ fn ui_npm(args: &[&str]) -> i32 { return 1; } }; - let npm = if cfg!(windows) { "npm.cmd" } else { "npm" }; + let npm = npm_program(); let pretty = format!("npm {}", args.join(" ")); eprintln!("xtask: {pretty} (in {})", ui_dir.display()); match Command::new(npm).args(args).current_dir(&ui_dir).status() {