From 7af7634022c215d43d319c854438912fcb1bcd61 Mon Sep 17 00:00:00 2001 From: gastown/crew/dennis Date: Fri, 16 Jan 2026 15:27:45 -0800 Subject: [PATCH] fix(tmux): use switch-client when already inside tmux session When attaching to a session from within tmux, use 'tmux switch-client' instead of 'tmux attach-session' to avoid the nested session error. Fixes #603 Co-Authored-By: Claude Opus 4.5 --- internal/cmd/crew_helpers.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/cmd/crew_helpers.go b/internal/cmd/crew_helpers.go index c8a0fa6d..302a9f7a 100644 --- a/internal/cmd/crew_helpers.go +++ b/internal/cmd/crew_helpers.go @@ -214,14 +214,22 @@ func isInTmuxSession(targetSession string) bool { } // attachToTmuxSession attaches to a tmux session. -// Should only be called from outside tmux. +// If already inside tmux, uses switch-client instead of attach-session. func attachToTmuxSession(sessionID string) error { tmuxPath, err := exec.LookPath("tmux") if err != nil { return fmt.Errorf("tmux not found: %w", err) } - cmd := exec.Command(tmuxPath, "attach-session", "-t", sessionID) + // Check if we're already inside a tmux session + var cmd *exec.Cmd + if os.Getenv("TMUX") != "" { + // Inside tmux: switch to the target session + cmd = exec.Command(tmuxPath, "switch-client", "-t", sessionID) + } else { + // Outside tmux: attach to the session + cmd = exec.Command(tmuxPath, "attach-session", "-t", sessionID) + } cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr