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('/')) {