From 22b80f8ce5fdaee307545d21fb5ad5a3be5e5782 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Fri, 1 May 2026 12:25:41 -0700 Subject: [PATCH] fix(terminal): split panes lose source cwd when shell omits OSC 7 (#1313) Co-authored-by: Orca --- src/main/providers/process-cwd.ts | 6 +++++- src/relay/pty-shell-utils.ts | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/providers/process-cwd.ts b/src/main/providers/process-cwd.ts index 3ff4a2528..326a61b42 100644 --- a/src/main/providers/process-cwd.ts +++ b/src/main/providers/process-cwd.ts @@ -70,7 +70,11 @@ async function doResolve(pid: number): Promise { } try { - const { stdout } = await execFile('lsof', ['-p', String(pid), '-d', 'cwd', '-Fn'], { + // Why: `-a` ANDs the -p and -d filters. Without it, macOS lsof ORs them + // and emits cwd records for every process on the system, so the n-line + // scan below picks up the first unrelated process (often pid ~391 with + // cwd `/`) and returns `/` regardless of the target pid's real cwd. + const { stdout } = await execFile('lsof', ['-a', '-p', String(pid), '-d', 'cwd', '-Fn'], { encoding: 'utf-8', timeout: LSOF_TIMEOUT_MS }) diff --git a/src/relay/pty-shell-utils.ts b/src/relay/pty-shell-utils.ts index ae9c2d2a0..72e26e1ec 100644 --- a/src/relay/pty-shell-utils.ts +++ b/src/relay/pty-shell-utils.ts @@ -42,10 +42,18 @@ export async function resolveProcessCwd(pid: number, fallbackCwd: string): Promi // lsof returns ALL open files (sockets, log files, TTYs) and the first `n`-line // could be any of them — not the actual working directory. try { - const { stdout: output } = await execFile('lsof', ['-p', String(pid), '-d', 'cwd', '-Fn'], { - encoding: 'utf-8', - timeout: 3000 - }) + // Why: `-a` ANDs the -p and -d filters. Without it, macOS lsof ORs them + // and emits cwd records for every process on the system, so the n-line + // scan below picks up the first unrelated process (often pid ~391 with + // cwd `/`) and returns `/` regardless of the target pid's real cwd. + const { stdout: output } = await execFile( + 'lsof', + ['-a', '-p', String(pid), '-d', 'cwd', '-Fn'], + { + encoding: 'utf-8', + timeout: 3000 + } + ) const lines = output.split('\n') for (const line of lines) { if (line.startsWith('n') && line.includes('/')) {