Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: find batch-launcher CLIs like az.cmd on Windows (#4402)
Worktrunk Bot11e8035b64f1
diff --git a/src/commands/list/ci_status/mod.rs b/src/commands/list/ci_status/mod.rsindex 47e231682..44ed5eb5f 100644--- a/src/commands/list/ci_status/mod.rs+++ b/src/commands/list/ci_status/mod.rs@@ -120,8 +120,8 @@ pub(crate) fn non_interactive_cmd(program: &str) -> Cmd {/// Check if a CLI tool is available///-/// On Windows, CreateProcessW (via Cmd) searches PATH for .exe files.-/// Tests provide `.exe` mocks via `testing::mock_commands`, so this works consistently.+/// On Windows, `Cmd` also finds a `.cmd`/`.bat` launcher such as Azure CLI's+/// `az.cmd`. Tests provide `.exe` mocks via `testing::mock_commands`.pub(crate) fn tool_available(tool: &str, args: &[&str]) -> bool {Cmd::new(tool).args(args.iter().copied())diff --git a/src/shell_exec.rs b/src/shell_exec.rsindex 52c103cef..475816e48 100644--- a/src/shell_exec.rs+++ b/src/shell_exec.rs@@ -1396,6 +1396,45 @@ fn join_delayed_readers(result}+/// The batch launcher a bare program name resolves to on PATH, if any.+///+/// `std::process::Command` resolves a bare name like `az` to `az.exe` only: it+/// neither walks `PATHEXT` nor falls back to `az.cmd`. Some CLIs ship only a+/// batch launcher — Azure CLI's WinGet install has `az.cmd` plus an+/// extensionless bash script, and no `az.exe` — so a bare spawn fails with+/// `NotFound` although the shell runs them. `which` follows `PATHEXT` and skips+/// files Windows can't execute; when what it finds is a `.cmd` or `.bat`, its+/// full path is what to spawn, and std then applies its batch-file argument+/// quoting. Any other result keeps the bare name and std's own search order.+///+/// Resolved once per name: PATH doesn't change during a run, and the search+/// stats every `PATHEXT` candidate in every PATH directory.+#[cfg(windows)]+fn windows_batch_launcher(program: &str) -> Option<PathBuf> {+ use std::collections::HashMap;++ static RESOLVED: OnceLock<Mutex<HashMap<String, Option<PathBuf>>>> = OnceLock::new();++ let path = Path::new(program);+ if path.extension().is_some() || path.components().count() != 1 {+ return None;+ }+ let mut resolved = RESOLVED+ .get_or_init(Default::default)+ .lock()+ .unwrap_or_else(|e| e.into_inner());+ resolved+ .entry(program.to_string())+ .or_insert_with(|| {+ which::which(program).ok().filter(|found| {+ found.extension().is_some_and(|ext| {+ ext.eq_ignore_ascii_case("cmd") || ext.eq_ignore_ascii_case("bat")+ })+ })+ })+ .clone()+}+impl Cmd {fn builder(program: impl Into<String>, shell_wrap: bool) -> Self {Self {@@ -1444,6 +1483,12 @@ impl Cmd {}fn direct_command(&self) -> Command {+ #[cfg(windows)]+ let mut cmd = match windows_batch_launcher(&self.program) {+ Some(launcher) => Command::new(launcher),+ None => Command::new(&self.program),+ };+ #[cfg(not(windows))]let mut cmd = Command::new(&self.program);cmd.args(&self.args);cmddiff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex bcaf381ee..2ad9b43a2 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -1761,6 +1761,63 @@ fn test_config_show_full_azure_remote(});}+/// Azure CLI's WinGet install puts `az.cmd` on PATH beside an extensionless+/// bash script, with no `az.exe`. A bare `az` spawn has to reach the batch+/// launcher, or `wt` reports an installed CLI as missing (#4401).+#[cfg(windows)]+#[rstest]+fn test_config_show_finds_batch_launcher_az(mut repo: TestRepo, temp_home: TempDir) {+ use crate::common::mock_commands::MockConfig;++ repo.setup_mock_ci_tools_unauthenticated();+ let mock_bin = repo.mock_bin_path().unwrap().to_path_buf();+ fs::write(+ mock_bin.join("az_extensions.json"),+ r#"[{"name": "azure-devops", "version": "1.0.0"}]"#,+ )+ .unwrap();+ // The mock answers under another name, so the only `az` on the mock+ // directory is the launcher pair WinGet installs.+ MockConfig::new("azmock")+ .version("azure-cli 2.60.0 (mock)")+ .command("account show", MockResponse::exit(0))+ .command("extension list", MockResponse::file("az_extensions.json"))+ .command("_default", MockResponse::exit(1))+ .write(&mock_bin);+ let _ = fs::remove_file(mock_bin.join("az.exe"));+ fs::write(mock_bin.join("az.cmd"), "@\"%~dp0azmock.exe\" %*\r\n").unwrap();+ fs::write(+ mock_bin.join("az"),+ "#!/usr/bin/env bash\nexec \"$(dirname \"$0\")/azmock.exe\" \"$@\"\n",+ )+ .unwrap();++ repo.run_git(&[+ "remote",+ "set-url",+ "origin",+ "https://dev.azure.com/myorg/myproject/_git/test-repo",+ ]);++ let mut cmd = repo.wt_command();+ cmd.env("WORKTRUNK_TEST_LATEST_VERSION", env!("CARGO_PKG_VERSION"));+ cmd.args(["config", "show", "--full"])+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());+ set_xdg_config_path(&mut cmd, temp_home.path());+ let output = cmd.output().unwrap();+ let text = format!(+ "{}{}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ let text = text.ansi_strip();+ assert!(+ text.contains("az installed & authenticated"),+ "az.cmd was not found:\n{text}"+ );+}+#[rstest]fn test_config_show_github_remote(mut repo: TestRepo, temp_home: TempDir) {// Setup mock gh/glab for deterministic BINARIES output