fix: use switch-client when already inside a tmux session #2

Closed
earthspacon wants to merge 1 commit from fix/nested-tmux-session into main
earthspacon commented 2026-03-18 10:00:30 +00:00 (Migrated from github.com)

Fixes #1

Problem

Running superharness from within an existing tmux session (common in Linux setups that use tmux as the default terminal multiplexer, e.g. Omarchy) always fails:

sessions should be nested with care, unset $TMUX to force
Error: tmux attach failed

tmux attach-session refuses to run when $TMUX is already set because it detects nesting and exits non-zero, which triggers bail!("tmux attach failed").

Fix

Detect $TMUX before attaching. When it is set, use switch-client -t superharness instead of attach-session -t superharness.

switch-client moves the current tmux client to the superharness session without creating a nested session — exactly the right behaviour. Because switch-client returns immediately (unlike attach-session, which blocks until the user detaches), the post-attach session-cleanup block is skipped in this path so the session is not killed right after switching to it.

Behaviour

Launch context Before After
Outside tmux ✅ works ✅ works (unchanged)
Inside tmux ❌ tmux attach failed ✅ switches current client to superharness session

Test

# from inside a tmux session
superharness   # previously failed, now switches to superharness session

Made with Cursor

Fixes #1 ## Problem Running `superharness` from within an existing tmux session (common in Linux setups that use tmux as the default terminal multiplexer, e.g. Omarchy) always fails: ``` sessions should be nested with care, unset $TMUX to force Error: tmux attach failed ``` `tmux attach-session` refuses to run when `$TMUX` is already set because it detects nesting and exits non-zero, which triggers `bail!("tmux attach failed")`. ## Fix Detect `$TMUX` before attaching. When it is set, use `switch-client -t superharness` instead of `attach-session -t superharness`. `switch-client` moves the current tmux client to the superharness session without creating a nested session — exactly the right behaviour. Because `switch-client` returns immediately (unlike `attach-session`, which blocks until the user detaches), the post-attach session-cleanup block is skipped in this path so the session is not killed right after switching to it. ## Behaviour | Launch context | Before | After | |---|---|---| | Outside tmux | ✅ works | ✅ works (unchanged) | | Inside tmux | ❌ `tmux attach failed` | ✅ switches current client to superharness session | ## Test ```bash # from inside a tmux session superharness # previously failed, now switches to superharness session ``` Made with [Cursor](https://cursor.com)
backmeupplz commented 2026-03-23 13:37:55 +00:00 (Migrated from github.com)

Thanks for the PR! This fix has already shipped in main — we landed the same switch-client approach in commit 10f7ede (feat/tmux-attach). Closing as already resolved, but appreciate you taking the time to track this down and submit a fix!

Thanks for the PR! This fix has already shipped in main — we landed the same `switch-client` approach in commit `10f7ede` (feat/tmux-attach). Closing as already resolved, but appreciate you taking the time to track this down and submit a fix!

Pull request closed

Sign in to join this conversation.
No description provided.