Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: replace panic with error when Git Bash not found on Windows (#1262)

worktrunk-bot2b98d11517cd
diff --git a/src/commands/for_each.rs b/src/commands/for_each.rs
index a52f1c136..6680c1679 100644
--- a/src/commands/for_each.rs
+++ b/src/commands/for_each.rs
@@ -164,7 +164,7 @@ fn run_command_streaming(
working_dir: &std::path::Path,
stdin_content: Option<&str>,
) -> Result<(), CommandError> {
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get().map_err(|e| CommandError::SpawnFailed(e.to_string()))?;
log::debug!("$ {} (streaming)", command);
diff --git a/src/commands/process.rs b/src/commands/process.rs
index ac209b972..f0c3deed3 100644
--- a/src/commands/process.rs
+++ b/src/commands/process.rs
@@ -314,7 +314,7 @@ fn spawn_detached_windows(
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
const DETACHED_PROCESS: u32 = 0x00000008;
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get()?;
// Build the command based on shell type
let mut cmd = if shell.is_posix() {
diff --git a/src/help_pager.rs b/src/help_pager.rs
index 8e2ab8774..4df784486 100644
--- a/src/help_pager.rs
+++ b/src/help_pager.rs
@@ -34,7 +34,7 @@ use crate::pager::{git_config_pager, parse_pager_value};
/// On Windows without Git Bash, returns None if only `less` would be selected
/// (since `less` isn't available without Git for Windows).
fn detect_help_pager() -> Option<String> {
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get().ok()?;
// Check environment variables in git's precedence order
let pager = std::env::var("GIT_PAGER")
@@ -116,7 +116,14 @@ pub(crate) fn show_help_in_pager(help_text: &str, use_pager: bool) -> std::io::R
// Spawn pager with TTY access (interactive, unlike detached diff renderer)
// Falls back to direct output if pager unavailable (e.g., less not installed)
- let shell = ShellConfig::get();
+ let shell = match ShellConfig::get() {
+ Ok(shell) => shell,
+ Err(e) => {
+ log::debug!("Shell unavailable for pager: {}", e);
+ eprint!("{}", help_text);
+ return Ok(());
+ }
+ };
log::debug!("$ {} (pager)", pager_cmd);
let mut cmd = shell.command(&final_cmd);
// Prevent subprocesses from writing to the directive file
diff --git a/src/llm.rs b/src/llm.rs
index 809d594a4..10ab4612a 100644
--- a/src/llm.rs
+++ b/src/llm.rs
@@ -296,7 +296,7 @@ pub(crate) fn execute_llm_command(command: &str, prompt: &str) -> anyhow::Result
log::debug!(" {}", line);
}
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get()?;
// TODO(claude-code-nesting): Claude Code sets CLAUDECODE=1 and blocks nested
// invocations, even non-interactive `claude -p`. Remove this env_remove if
// Claude Code relaxes the check for non-interactive mode. If they don't fix
diff --git a/src/output/global.rs b/src/output/global.rs
index e93b0fb86..0220069fc 100644
--- a/src/output/global.rs
+++ b/src/output/global.rs
@@ -298,7 +298,7 @@ pub fn execute(command: impl Into<String>) -> anyhow::Result<()> {
#[cfg(unix)]
fn execute_command(command: String, target_dir: Option<&Path>) -> anyhow::Result<()> {
let exec_dir = target_dir.unwrap_or_else(|| Path::new("."));
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get()?;
// Use exec() to replace wt process with the command.
// This gives the command full TTY access (stdin, stdout, stderr all inherited),
diff --git a/src/path.rs b/src/path.rs
index 4e820f918..d74bcdf04 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -28,7 +28,10 @@ use std::path::PathBuf;
/// - `/tmp/test/repo` → `/tmp/test/repo` (unchanged on Unix)
#[cfg(windows)]
pub fn to_posix_path(path: &str) -> String {
- let Some(cygpath) = find_cygpath_from_shell(ShellConfig::get()) else {
+ let Ok(shell) = ShellConfig::get() else {
+ return path.to_string();
+ };
+ let Some(cygpath) = find_cygpath_from_shell(shell) else {
return path.to_string();
};
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index d98d13318..f53c672ed 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -59,7 +59,7 @@ fn get_semaphore() -> &'static Semaphore {
}
/// Cached shell configuration for the current platform
-static SHELL_CONFIG: OnceLock<ShellConfig> = OnceLock::new();
+static SHELL_CONFIG: OnceLock<Result<ShellConfig, String>> = OnceLock::new();
/// Shell configuration for command execution
#[derive(Debug, Clone)]
@@ -77,9 +77,12 @@ pub struct ShellConfig {
impl ShellConfig {
/// Get the shell configuration for the current platform
///
- /// On Unix, returns sh. On Windows, returns Git Bash (panics if not installed).
- pub fn get() -> &'static ShellConfig {
- SHELL_CONFIG.get_or_init(detect_shell)
+ /// On Unix, returns sh. On Windows, returns Git Bash or an error if not installed.
+ pub fn get() -> anyhow::Result<&'static ShellConfig> {
+ SHELL_CONFIG
+ .get_or_init(detect_shell)
+ .as_ref()
+ .map_err(|e| anyhow::anyhow!("{e}"))
}
/// Create a Command configured for shell execution
@@ -106,15 +109,15 @@ impl ShellConfig {
}
/// Detect the best available shell for the current platform
-fn detect_shell() -> ShellConfig {
+fn detect_shell() -> Result<ShellConfig, String> {
#[cfg(unix)]
{
- ShellConfig {
+ Ok(ShellConfig {
executable: PathBuf::from("sh"),
args: vec!["-c".to_string()],
is_posix: true,
name: "sh".to_string(),
- }
+ })
}
#[cfg(windows)]
@@ -125,22 +128,22 @@ fn detect_shell() -> ShellConfig {
/// Detect Git Bash on Windows
///
-/// Panics if Git for Windows is not installed, since hooks require bash syntax.
+/// Returns an error if Git for Windows is not installed, since hooks require
+/// bash syntax.
#[cfg(windows)]
-fn detect_windows_shell() -> ShellConfig {
+fn detect_windows_shell() -> Result<ShellConfig, String> {
if let Some(bash_path) = find_git_bash() {
- return ShellConfig {
+ return Ok(ShellConfig {
executable: bash_path,
args: vec!["-c".to_string()],
is_posix: true,
name: "Git Bash".to_string(),
- };
+ });
}
- panic!(
- "Git for Windows is required but not found.\n\
+ Err("Git for Windows is required but not found.\n\
Install from https://git-scm.com/download/win"
- );
+ .to_string())
}
/// Find Git Bash executable on Windows
@@ -728,7 +731,7 @@ impl Cmd {
// Build the command - either shell-wrapped or direct
let (mut cmd, exec_mode) = if self.shell_wrap {
- let shell = ShellConfig::get();
+ let shell = ShellConfig::get()?;
let mode = format!("shell: {}", shell.name);
(shell.command(&self.program), mode)
} else {
@@ -977,7 +980,7 @@ mod tests {
#[test]
fn test_shell_config_is_available() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
assert!(!config.name.is_empty());
assert!(!config.args.is_empty());
}
@@ -985,14 +988,14 @@ mod tests {
#[test]
#[cfg(unix)]
fn test_unix_shell_is_posix() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
assert!(config.is_posix);
assert_eq!(config.name, "sh");
}
#[test]
fn test_command_creation() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
let cmd = config.command("echo hello");
// Just verify it doesn't panic
let _ = format!("{:?}", cmd);
@@ -1000,7 +1003,7 @@ mod tests {
#[test]
fn test_shell_command_execution() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
let output = config
.command("echo hello")
.output()
@@ -1026,7 +1029,7 @@ mod tests {
#[test]
#[cfg(windows)]
fn test_windows_uses_git_bash() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
assert_eq!(config.name, "Git Bash");
assert!(config.is_posix, "Git Bash should support POSIX syntax");
assert!(
@@ -1038,7 +1041,7 @@ mod tests {
#[test]
#[cfg(windows)]
fn test_windows_echo_command() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
let output = config
.command("echo test_output")
.output()
@@ -1056,7 +1059,7 @@ mod tests {
#[test]
#[cfg(windows)]
fn test_windows_posix_redirection() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
// Test POSIX-style redirection: stdout redirected to stderr
let output = config
.command("echo redirected 1>&2")
@@ -1074,7 +1077,7 @@ mod tests {
#[test]
fn test_shell_config_debug() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
let debug = format!("{:?}", config);
assert!(debug.contains("ShellConfig"));
assert!(debug.contains(&config.name));
@@ -1082,7 +1085,7 @@ mod tests {
#[test]
fn test_shell_config_clone() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
let cloned = config.clone();
assert_eq!(config.name, cloned.name);
assert_eq!(config.is_posix, cloned.is_posix);
@@ -1091,7 +1094,7 @@ mod tests {
#[test]
fn test_shell_is_posix_method() {
- let config = ShellConfig::get();
+ let config = ShellConfig::get().unwrap();
// is_posix method should match the field
assert_eq!(config.is_posix(), config.is_posix);
}