From 20442eecd3c81c543eae5672955626c87035d874 Mon Sep 17 00:00:00 2001 From: bherbruck Date: Thu, 15 Oct 2026 21:49:46 +0000 Subject: [PATCH] Agent chat on Windows: start npm agents through their .cmd shim npm puts a Unix shell script with the bare name (codex-acp, claude-code-acp, gemini) beside the .cmd shim. The lookup tried the bare name first, so every npm-installed agent failed with "not a valid Win32 application (os error 193)". Windows now only considers .exe/.cmd/.bat, and a command saved with the bare path runs the shim beside it. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 60ea8c69d26e21bea09bd6239313d43f5af112b3) --- crates/ui-egui/src/agent_chat/discover.rs | 42 +++++++++++++++++++++-- crates/ui-egui/src/agent_chat/process.rs | 5 ++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/agent_chat/discover.rs b/crates/ui-egui/src/agent_chat/discover.rs index fcd09660..24fffffa 100644 --- a/crates/ui-egui/src/agent_chat/discover.rs +++ b/crates/ui-egui/src/agent_chat/discover.rs @@ -184,13 +184,30 @@ pub fn search_dirs() -> Vec { dirs } -/// The first `program` in `dirs` (on Windows also with `.exe`/`.cmd`). +/// The first `program` in `dirs` (on Windows with `.exe`/`.cmd`/`.bat`). pub fn find_in(program: &str, dirs: &[PathBuf]) -> Option { - let names: Vec = - if cfg!(windows) { ["", ".exe", ".cmd", ".bat"].iter().map(|e| format!("{program}{e}")).collect() } else { vec![program.to_string()] }; + let names = names_for(program, cfg!(windows)); dirs.iter().flat_map(|d| names.iter().map(move |n| d.join(n))).find(|p| p.is_file()) } +/// The file names `program` can have. On Windows only ones it can run: npm puts a Unix shell +/// script with the bare name next to the `.cmd` shim, and starting that fails with "not a valid +/// Win32 application". +fn names_for(program: &str, windows: bool) -> Vec { + if windows { [".exe", ".cmd", ".bat"].iter().map(|e| format!("{program}{e}")).collect() } else { vec![program.to_string()] } +} + +/// A Windows command saved without an extension (`…\npm\codex-acp`, from an older version's +/// lookup): the `.exe`/`.cmd`/`.bat` beside it, if there is one. +pub fn runnable_sibling(program: &Path) -> Option { + if program.extension().is_some() { + return None; + } + let dir = program.parent()?; + let name = program.file_name()?.to_str()?; + names_for(name, true).into_iter().map(|n| dir.join(n)).find(|p| p.is_file()) +} + /// `program --version`'s first line, or `None` after `limit` (the probe is then stopped). fn version(program: &Path, limit: Duration) -> Option { let mut cmd = if cfg!(windows) && program.extension().is_some_and(|e| e.eq_ignore_ascii_case("cmd") || e.eq_ignore_ascii_case("bat")) { @@ -279,6 +296,25 @@ mod tests { let _ = std::fs::remove_dir_all(root); } + #[test] + fn on_windows_only_runnable_shims_are_candidates() { + // npm's bare `codex-acp` is a shell script; Windows can't start it (os error 193). + assert_eq!(names_for("codex-acp", true), ["codex-acp.exe", "codex-acp.cmd", "codex-acp.bat"]); + assert_eq!(names_for("codex-acp", false), ["codex-acp"]); + } + + #[test] + fn a_saved_bare_npm_path_runs_its_cmd_shim() { + let root = std::env::temp_dir().join(format!("sc-agent-shim-{}", std::process::id())); + std::fs::create_dir_all(&root).unwrap(); + std::fs::write(root.join("codex-acp"), "#!/bin/sh").unwrap(); + std::fs::write(root.join("codex-acp.cmd"), "@echo off").unwrap(); + assert_eq!(runnable_sibling(&root.join("codex-acp")), Some(root.join("codex-acp.cmd"))); + assert_eq!(runnable_sibling(&root.join("codex-acp.cmd")), None); + assert_eq!(runnable_sibling(&root.join("gemini")), None); + let _ = std::fs::remove_dir_all(root); + } + #[test] fn the_version_number_is_picked_out_of_the_line() { assert_eq!(version_of("@agentclientprotocol/codex-acp 2.2.2"), "2.2.2"); diff --git a/crates/ui-egui/src/agent_chat/process.rs b/crates/ui-egui/src/agent_chat/process.rs index 4fc64484..e7049972 100644 --- a/crates/ui-egui/src/agent_chat/process.rs +++ b/crates/ui-egui/src/agent_chat/process.rs @@ -51,8 +51,11 @@ impl ProcessHandle { } } -/// On Windows, npm installs `.cmd` shims, which only `cmd.exe` runs. +/// On Windows, npm installs `.cmd` shims, which only `cmd.exe` runs. A command saved as the bare +/// npm path (a shell script there) runs the shim beside it. fn command_for(program: &str) -> Command { + let shim = if cfg!(windows) { super::discover::runnable_sibling(std::path::Path::new(program)) } else { None }; + let program = shim.as_deref().and_then(|p| p.to_str()).unwrap_or(program); let lower = program.to_ascii_lowercase(); if cfg!(windows) && (lower.ends_with(".cmd") || lower.ends_with(".bat")) { let mut c = Command::new("cmd");