From 5c1c650848573c2357d8ff4cb073638467a13fa6 Mon Sep 17 00:00:00 2001 From: tdwd <111124579+tdwd@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:18:02 +0200 Subject: [PATCH] Name a session's missing directory instead of blaming the agent A session whose directory had gone failed to start with "fork/exec : no such file or directory", which reads as a missing executable. pty.StartWithSize sets SysProcAttr, and with it set os.StartProcess skips its own check of the directory, so the failed chdir in the child is reported against the program's path. Start now checks the directory first and reports "start : session directory: stat : no such file or directory". --- internal/agent/agent.go | 9 +++++++++ internal/agent/agent_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 7f90a64..998b855 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -92,6 +92,15 @@ func Start(cfg Config) (*Runner, error) { if _, err := exec.LookPath(cfg.Command); err != nil { return nil, fmt.Errorf("agent %q not found on PATH: %w", cfg.Command, err) } + // Checked here because the PTY start cannot say it. pty.StartWithSize sets + // SysProcAttr, and with that set os.StartProcess skips its own check of the + // directory, so a missing one is reported as "fork/exec : no such + // file or directory", which blames an executable that is there. + if cfg.Dir != "" { + if _, err := os.Stat(cfg.Dir); err != nil { + return nil, fmt.Errorf("start %s: session directory: %w", cfg.Command, err) + } + } cmd := exec.Command(cfg.Command, cfg.Args...) cmd.Dir = cfg.Dir diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 4de4f3c..bcc1671 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -2,6 +2,7 @@ package agent import ( "os" + "path/filepath" "strings" "testing" "time" @@ -22,6 +23,22 @@ func screenContains(t *testing.T, r *Runner, want string, timeout time.Duration) return false } +// TestStartNamesAMissingDirectory: a session whose directory is gone says so. +// Through a PTY the start itself reports it as "fork/exec : no such +// file or directory", which sent the reader looking for an executable that was +// there all along. +func TestStartNamesAMissingDirectory(t *testing.T) { + dir := filepath.Join(t.TempDir(), "gone") + r, err := Start(Config{Command: "bash", Dir: dir, Width: 80, Height: 24}) + if err == nil { + r.Stop() + t.Fatal("started in a directory that does not exist") + } + if !strings.Contains(err.Error(), dir) || strings.Contains(err.Error(), "fork/exec") { + t.Errorf("err = %q, want it to name %s rather than the executable", err, dir) + } +} + func TestRunnerShowsOutput(t *testing.T) { r, err := Start(Config{ Command: "bash",