From b2fd411552e9c01ab06f94898564bc7b15192917 Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Tue, 14 Jul 2026 12:42:52 +0200 Subject: [PATCH 1/2] fix: stop resolving TUI scripts from cwd --- crates/goose-cli/src/commands/tui.rs | 50 ++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/crates/goose-cli/src/commands/tui.rs b/crates/goose-cli/src/commands/tui.rs index a0ebc3311c5b..48be007e7bb8 100644 --- a/crates/goose-cli/src/commands/tui.rs +++ b/crates/goose-cli/src/commands/tui.rs @@ -14,6 +14,10 @@ enum TuiSource { fn find_local_script() -> Option { let exe = std::env::current_exe().ok()?; + find_local_script_from(&exe) +} + +fn find_local_script_from(exe: &Path) -> Option { let exe_dir = exe.parent().unwrap_or_else(|| Path::new(".")); let mut dir = Some(exe_dir.to_path_buf()); @@ -27,13 +31,6 @@ fn find_local_script() -> Option { } } - if let Ok(cwd) = std::env::current_dir() { - let candidate = cwd.join(TUI_REL_PATH); - if candidate.is_file() { - return Some(candidate); - } - } - None } @@ -97,3 +94,42 @@ pub fn handle_tui(args: Vec) -> Result<()> { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + #[test] + fn find_local_script_ignores_current_working_directory() { + let _guard = env_lock::lock_env([("GOOSE_TUI_NPM_SPEC", None::<&str>)]); + + let temp_dir = tempfile::tempdir().expect("create temp dir"); + let executable = temp_dir.path().join("install/bin/goose"); + let working_dir = temp_dir.path().join("checkout"); + let planted_script = working_dir.join(TUI_REL_PATH); + fs::create_dir_all(planted_script.parent().unwrap()).expect("create script directory"); + fs::write(&planted_script, "process.exit(0)\n").expect("write planted script"); + + let original_dir = std::env::current_dir().expect("get current directory"); + std::env::set_current_dir(&working_dir).expect("set current directory"); + let result = find_local_script_from(&executable); + std::env::set_current_dir(original_dir).expect("restore current directory"); + + assert_eq!(result, None); + } + + #[test] + fn find_local_script_accepts_executable_ancestor() { + let temp_dir = tempfile::tempdir().expect("create temp dir"); + let executable = temp_dir.path().join("target/debug/goose"); + let bundled_script = temp_dir.path().join(TUI_REL_PATH); + fs::create_dir_all(bundled_script.parent().unwrap()).expect("create script directory"); + fs::write(&bundled_script, "process.exit(0)\n").expect("write bundled script"); + + assert_eq!( + find_local_script_from(&executable).as_deref(), + Some(bundled_script.as_path()) + ); + } +} From 6b0386de1e8eda122034215b6672024d0b847917 Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Tue, 14 Jul 2026 13:07:55 +0200 Subject: [PATCH 2/2] test: avoid changing cwd in TUI lookup coverage --- crates/goose-cli/src/commands/tui.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/crates/goose-cli/src/commands/tui.rs b/crates/goose-cli/src/commands/tui.rs index 48be007e7bb8..3b49a633f032 100644 --- a/crates/goose-cli/src/commands/tui.rs +++ b/crates/goose-cli/src/commands/tui.rs @@ -101,22 +101,14 @@ mod tests { use std::fs; #[test] - fn find_local_script_ignores_current_working_directory() { - let _guard = env_lock::lock_env([("GOOSE_TUI_NPM_SPEC", None::<&str>)]); - + fn find_local_script_ignores_unrelated_directories() { let temp_dir = tempfile::tempdir().expect("create temp dir"); let executable = temp_dir.path().join("install/bin/goose"); - let working_dir = temp_dir.path().join("checkout"); - let planted_script = working_dir.join(TUI_REL_PATH); + let planted_script = temp_dir.path().join("checkout").join(TUI_REL_PATH); fs::create_dir_all(planted_script.parent().unwrap()).expect("create script directory"); fs::write(&planted_script, "process.exit(0)\n").expect("write planted script"); - let original_dir = std::env::current_dir().expect("get current directory"); - std::env::set_current_dir(&working_dir).expect("set current directory"); - let result = find_local_script_from(&executable); - std::env::set_current_dir(original_dir).expect("restore current directory"); - - assert_eq!(result, None); + assert_eq!(find_local_script_from(&executable), None); } #[test]