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",