From 550e9f8cc085f428429f5cf2da0bb2dba897e8b8 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Thu, 16 Jul 2026 23:13:06 +0300 Subject: fix: follow foreground process group leader cwd refs #1472 --- src/pane.rs | 26 ++++++++++++++++++++++++++ src/terminal/runtime.rs | 4 ++++ src/workspace/tab.rs | 11 +++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/pane.rs b/src/pane.rs index 5bb6fe4e..3c817876 100644 --- a/src/pane.rs +++ b/src/pane.rs @@ -2713,6 +2713,22 @@ impl PaneRuntime { (pid > 0).then_some(pid) } + pub fn follow_cwd(&self) -> Option { + #[cfg(unix)] + { + let leader_cwd = self + .io + .foreground_process_group_id() + .and_then(usable_process_cwd); + leader_cwd.or_else(|| self.cwd()) + } + + #[cfg(not(unix))] + { + self.cwd() + } + } + /// Get the current working directory of the process group controlling the pane PTY. pub fn foreground_cwd(&self) -> Option { #[cfg(unix)] @@ -2816,6 +2832,16 @@ impl PaneRuntime { mod tests { use super::*; + #[cfg(unix)] + #[tokio::test] + async fn follow_cwd_falls_back_to_reported_pane_cwd_without_foreground_group() { + let (runtime, _rx) = PaneRuntime::test_with_channel(80, 24); + let cwd = std::env::temp_dir(); + *runtime.reported_cwd.lock().unwrap() = Some(cwd.clone()); + + assert_eq!(runtime.follow_cwd(), Some(cwd)); + } + #[test] fn shutdown_liveness_treats_reaped_direct_child_as_gone() { assert!(!process_alive_for_shutdown(42, 42, true, |_| true)); diff --git a/src/terminal/runtime.rs b/src/terminal/runtime.rs index 1d19ad4a..0241f984 100644 --- a/src/terminal/runtime.rs +++ b/src/terminal/runtime.rs @@ -453,6 +453,10 @@ impl TerminalRuntime { self.0.cwd() } + pub fn follow_cwd(&self) -> Option { + self.0.follow_cwd() + } + pub fn foreground_cwd(&self) -> Option { self.0.foreground_cwd() } diff --git a/src/workspace/tab.rs b/src/workspace/tab.rs index 15ca7e45..4c86a6e8 100644 --- a/src/workspace/tab.rs +++ b/src/workspace/tab.rs @@ -578,7 +578,14 @@ impl Tab { terminals: &HashMap, terminal_runtimes: &TerminalRuntimeRegistry, ) -> Option { - self.foreground_cwd_for_pane(pane_id, terminal_runtimes) - .or_else(|| self.cwd_for_pane(pane_id, terminals, terminal_runtimes)) + let terminal_id = self.terminal_id(pane_id)?; + terminal_runtimes + .get(terminal_id) + .and_then(|runtime| runtime.follow_cwd()) + .or_else(|| { + terminals + .get(terminal_id) + .map(|terminal| terminal.cwd.clone()) + }) } } -- cgit v1.2.3