Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Find the Windows shim, not npm's POSIX script, for a bare program name (#51)

muraa-p793e47ce0f83
diff --git a/crates/platform/src/which.rs b/crates/platform/src/which.rs
index 164cde91..cc9aa8b6 100644
--- a/crates/platform/src/which.rs
+++ b/crates/platform/src/which.rs
@@ -169,7 +169,10 @@ pub fn self_cli_path() -> Option<PathBuf> {
#[cfg(windows)]
fn candidates(base: &Path) -> Vec<PathBuf> {
- let mut v = vec![base.to_path_buf()];
+ let mut v = Vec::new();
+ // A file with no extension can't be started by name (CreateProcess only adds `.exe`), and npm
+ // puts an extensionless POSIX shell script beside every `.cmd` shim it installs. Try the kinds
+ // Windows runs first, then the bare name, so `which` returns what the system would start (#41).
if base.extension().is_none() {
let exts = std::env::var("PATHEXT").unwrap_or_else(|_| ".COM;.EXE;.BAT;.CMD".into());
for e in exts.split(';').filter(|e| !e.is_empty()) {
@@ -178,6 +181,7 @@ fn candidates(base: &Path) -> Vec<PathBuf> {
v.push(PathBuf::from(s));
}
}
+ v.push(base.to_path_buf());
v
}
@@ -283,6 +287,24 @@ mod tests {
assert_eq!(serde_json::from_str::<Vec<String>>(&out.stdout_text()).unwrap(), ["-p", prompt]);
}
+ /// #41: npm writes an extensionless POSIX script beside its `.cmd` shim, and Windows can't start
+ /// that script by name, so a bare name must find the shim first.
+ #[cfg(windows)]
+ #[test]
+ fn a_bare_name_finds_what_windows_can_start() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("tool"), "#!/bin/sh\n").unwrap();
+ std::fs::write(dir.path().join("tool.cmd"), "@echo off\n").unwrap();
+ let path = || Some(dir.path().as_os_str().to_owned());
+ // The extension comes from PATHEXT, so its case need not match the file's.
+ let found = which_in("tool", path());
+ assert_eq!(found.len(), 1);
+ assert!(found[0].file_name().is_some_and(|n| n.to_string_lossy().eq_ignore_ascii_case("tool.cmd")), "the shim, not the POSIX script: {found:?}");
+ // With only the script there, it's still reported (and starting it fails with the program's name).
+ std::fs::remove_file(dir.path().join("tool.cmd")).unwrap();
+ assert_eq!(which_in("tool", path()), vec![dir.path().join("tool")]);
+ }
+
#[cfg(unix)]
#[test]
fn explicit_search_path() {
diff --git a/docs/pitfalls.md b/docs/pitfalls.md
index 97d94bcd..9bbdcf55 100644
--- a/docs/pitfalls.md
+++ b/docs/pitfalls.md
@@ -140,6 +140,9 @@ Claude Code as `gemini.cmd`-style shims; `which` finds them through `PATHEXT`, b
only `.exe` files by bare name, so every build failed (#18). Through `cmd.exe` a prompt with line
breaks or over 8191 characters wouldn't arrive anyway. `Process` runs an npm shim's script with
Node directly (`which::launch_target`); test a new program on Windows with a multi-line argument.
+npm also writes an extensionless POSIX script beside each shim, and `which` returned that first,
+so OpenCode was offered and then every run said it wasn't installed (#41): look up the kinds
+Windows can start (`PATHEXT`) before the bare name.
**A first analysis that waited for a lock it could never take.** Ghidra's analysis lock is created
with `create_new` in `ghidra-cache/`, which only existed on machines that had used it before; on a