From 7e9d2c4169245072265b483ffaa6aa05f23f08d3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 21 Oct 2026 22:53:57 -0300 Subject: A terminal under a wrapper is where its foreground shell is, not where the wrapper stays A shell that re-execs itself under a wrapper (fish's `exec 9ns --mntgen -- fish` when NINE_MOUNT is unset) left /proc//cwd at the wrapper's first directory, so the pane never followed a cd and a relative path printed after one (fd's b/c.txt) looked up nothing. host_io.ttyCwd: while the forked process still runs the shell pardes started, its own directory, as before, so a job in another directory (a nested pardes, (cd sub && make)) never names the pane; once it is something else, the tty's foreground group's when that is the same shell, else no answer. Co-Authored-By: Claude Opus 5.5 --- src/CHANGELOG.md | 5 +++ src/detached/server.zig | 2 +- src/gui/gui.zig | 2 +- src/host_io.zig | 77 ++++++++++++++++++++++++++++++++++++++ src/macos.zig | 2 +- src/tty/tty.zig | 2 +- test/snapshots/look-fd-deep.golden | 30 +++++++++++++++ test/snapshots/look-fd-deep.snap | 34 +++++++++++++++++ 8 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 test/snapshots/look-fd-deep.golden create mode 100644 test/snapshots/look-fd-deep.snap diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index ab9432ef..24ee3b0e 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -13,6 +13,11 @@ Signatures use syntax colors; only calls to the queried function get background emphasis, and tree guides continue through multiline signatures and wrapped rows. Cross-directory call locations use shorter relative paths. +- A terminal follows the shell at its prompt (the tty's foreground group), + not the process pardes started: a shell that re-execs itself under a + wrapper (fish's `exec 9ns --mntgen -- fish`) no longer leaves the pane + in its first directory, where `b/c.txt` printed by `fd` after a `cd` + looked up nothing. - Workspace, column and pane tags share a tighter text inset. - A diff paged into a session (`git show | pardes -`) opens its `+Diff` alone; the `+Pager` no longer stays folded under it. diff --git a/src/detached/server.zig b/src/detached/server.zig index 41bc054f..d6a1f553 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -660,7 +660,7 @@ pub const Session = struct { for (&s.ptys, 0..) |*pt, pane| { if (pt.fd < 0) continue; var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |cwd| s.core.setCwd(pane, cwd); + if (host_io.ttyCwd(pt.pid, pt.fd, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(pane, cwd); } } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 46d2cc78..f9921111 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4949,7 +4949,7 @@ fn pollCwds(core: *pardes.Pardes, ptys: *[pardes.MAX_PANES]?Pty) void { for (ptys, 0..) |slot, id| if (slot) |pt| { if (pt.fd < 0) continue; var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |cwd| core.setCwd(id, cwd); + if (host_io.ttyCwd(pt.pid, pt.fd, core.settings.shell.effective.get(), &lbuf)) |cwd| core.setCwd(id, cwd); }; } diff --git a/src/host_io.zig b/src/host_io.zig index bef88172..c593551b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -1795,6 +1795,83 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { } } +/// Where a terminal is. The process forked for it, while it still runs the +/// shell pardes started (`shell_exe`), as ever: a job it runs, in another +/// directory (`(cd sub && make)`, a nested pardes), never names the pane. +/// One that exec'd into something else is a wrapper whose own directory +/// never moves (fish's `exec 9ns --mntgen -- fish`): the shell is the +/// foreground group when that is the same shell, and while a job holds the +/// foreground there is no answer, the last one standing. +pub fn ttyCwd(shell_pid: libc.pid_t, master_fd: c_int, shell_exe: []const u8, buf: []u8) ?[]const u8 { + var exe_buf: [std.fs.max_path_bytes]u8 = undefined; + var shell_buf: [std.fs.max_path_bytes]u8 = undefined; + const shell = realShell(shell_exe, &shell_buf) orelse return shellCwd(shell_pid, buf); + const exe = procExe(shell_pid, &exe_buf) orelse return shellCwd(shell_pid, buf); + if (std.mem.eql(u8, exe, shell)) return shellCwd(shell_pid, buf); + const fg = tcgetpgrp(master_fd); + if (fg <= 0 or fg == shell_pid) return null; + const fg_exe = procExe(fg, &exe_buf) orelse return null; + return if (std.mem.eql(u8, fg_exe, shell)) shellCwd(fg, buf) else null; +} + +/// `shell_exe` as /proc//exe would read it: its links followed. +fn realShell(shell_exe: []const u8, buf: *[std.fs.max_path_bytes]u8) ?[]const u8 { + var z: [std.fs.max_path_bytes:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&z, "{s}", .{shell_exe}, 0) catch return null; + const r = libc.realpath(path, buf) orelse return null; + return std.mem.span(@as([*:0]const u8, @ptrCast(r))); +} + +test "a terminal is where its shell is: under a wrapper the foreground shell, never a job's directory" { + if (comptime builtin.os.tag != .linux) return error.SkipZigTest; + if (!haveFile("/bin/sh") or !haveFile("/usr/bin/timeout")) return error.SkipZigTest; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(io, "start/a/b"); + var path_buf: [4096]u8 = undefined; + const start = path_buf[0..try tmp.dir.realPathFile(io, "start", &path_buf)]; + var inner_buf: [4096]u8 = undefined; + const inner = inner_buf[0..try tmp.dir.realPathFile(io, "start/a/b", &inner_buf)]; + const rcs: Shell.PromptFiles = .{}; + var result: [4096]u8 = undefined; + // A wrapper exec'd over the shell (timeout standing in for 9ns) runs + // another, two directories down, in the foreground. + { + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80, null); + defer { + _ = libc.kill(child.pid, libc.SIG.KILL); + _ = libc.waitpid(child.pid, null, 0); + _ = libc.close(child.file.handle); + } + var sh: TestShell = .{ .master = child.file.handle, .pid = child.pid }; + try std.testing.expect(writeFd(child.file.handle, "exec timeout --foreground 30 /bin/sh -c 'cd a/b && printf \"\\160ardes-in-\\n\" && exec /bin/sh -i'\n")); + try std.testing.expect(sh.waitText("pardes-in-", 5_000)); + // printf runs before the inner shell takes the foreground. + var tries: usize = 0; + while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) { + const ts: libc.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + try std.testing.expectEqualStrings(start, shellCwd(child.pid, &result) orelse return error.MissingCwd); + try std.testing.expectEqualStrings(inner, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd); + } + // A job of the shell's own, gone into a/b, leaves the pane where the + // shell is. + { + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80, null); + defer { + _ = libc.kill(child.pid, libc.SIG.KILL); + _ = libc.waitpid(child.pid, null, 0); + _ = libc.close(child.file.handle); + } + var sh: TestShell = .{ .master = child.file.handle, .pid = child.pid }; + try std.testing.expect(writeFd(child.file.handle, "set -m; (cd a/b && printf '\\160ardes-job\\n' && exec sleep 30)\n")); + try std.testing.expect(sh.waitText("pardes-job", 5_000)); + try std.testing.expectEqualStrings(start, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd); + } +} + test "shell cwd rejects truncation and preserves an owned child path longer than 1024 bytes" { if (comptime builtin.os.tag != .linux) return error.SkipZigTest; if (!haveFile("/bin/sh")) return error.SkipZigTest; diff --git a/src/macos.zig b/src/macos.zig index 21af41ae..50fa04fa 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -1110,7 +1110,7 @@ fn refreshCwds(ctx: ?*anyopaque) void { stale.* = false; const pt = st.ptys[id] orelse continue; var buf: [1024]u8 = undefined; - if (host_io.shellCwd(pt.pid, &buf)) |wd| st.core.setCwd(id, wd); + if (host_io.ttyCwd(pt.pid, pt.file.handle, st.core.settings.shell.effective.get(), &buf)) |wd| st.core.setCwd(id, wd); } } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 82cb40bd..4cd1bc9a 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1737,7 +1737,7 @@ const Shell = struct { }; for (&s.ptys, 0..) |*slot, id| if (slot.*) |pt| { var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |cwd| s.core.setCwd(id, cwd); + if (host_io.ttyCwd(pt.pid, pt.file.handle, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(id, cwd); }; if (s.caps_pending and s.vx.queries_done.load(.unordered)) { diff --git a/test/snapshots/look-fd-deep.golden b/test/snapshots/look-fd-deep.golden new file mode 100644 index 00000000..c0e0c304 --- /dev/null +++ b/test/snapshots/look-fd-deep.golden @@ -0,0 +1,30 @@ +== snap listed grid=100x31 cursor=4,9 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/look-fd-deep/cwd/a Tty+bash Save Mode Filter Collapse Del +| $ exec timeout --foreground 600 bash -i +| $ cd a && ls && fd +| b top.txt +| b/ +| b/c.txt +| top.txt +| $ +| +| +| +| +| +| +| +| +| +| +| +| /tmp/pardes-snap/look-fd-deep/cwd/+New Save Tty Collapse Del +| 1 +== snap fd-path-looked grid=100x31 cursor=7,11 +|10: /tmp/pardes-snap/look-fd-deep/cwd/a/b/c.txt Save Tty Collapse Del +|11: 1 deep-marker +== snap ls-name-looked grid=100x31 cursor=7,14 +|13: /tmp/pardes-snap/look-fd-deep/cwd/a/top.txt Save Tty Collapse Del +|14: 1 top-marker diff --git a/test/snapshots/look-fd-deep.snap b/test/snapshots/look-fd-deep.snap new file mode 100644 index 00000000..8df0aaaf --- /dev/null +++ b/test/snapshots/look-fd-deep.snap @@ -0,0 +1,34 @@ +# A shell run under a wrapper exec'd over the one pardes started, that +# stays where it started (fish's own `exec 9ns --mntgen -- fish`; timeout +# here): after a cd the pane is where the foreground shell is, not where +# the wrapper is. So a path fd prints two +# directories down (b/c.txt) and a name ls prints (top.txt) look from there. +# The fd path goes first: the file it opens sits in a/b, a directory that +# could not rescue the ls name after it. +dirmk a +dirmk a/b +file a/top.txt top-marker +file a/b/c.txt deep-marker +start 31 100 +wait 8000 $ +stable 700 20000 +text exec timeout --foreground 600 bash -i +key enter +stable 700 20000 +text cd a && ls && fd +key enter +wait 8000 c.txt +wait 8000 cwd/a Tty +stable 700 20000 +snap listed +press right 6 8 +release right 6 8 +wait 10000 deep-marker +stable 700 15000 +snap fd-path-looked +# the ls name, from the shell's directory +press right 8 6 +release right 8 6 +wait 10000 top-marker +stable 700 15000 +snap ls-name-looked -- cgit v1.3