Repository navigation
fix(cmd): respect SetStdin/SetStdout/SetStderr for tea.Exec compatibility - #522
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #522 +/- ##
==========================================
- Coverage 77.23% 73.78% -3.45%
==========================================
Files 19 21 +2
Lines 940 946 +6
==========================================
- Hits 726 698 -28
- Misses 160 180 +20
- Partials 54 68 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
aymanbagabas
left a comment
There was a problem hiding this comment.
Hey, this looks good! Could you look at our x/xpty library and use it to spawn the process instead?
|
@aymanbagabas Thanks for the suggestion! I’ve updated the implementation to use the x/xpty library for spawning the process. Please let me know if this matches what you had in mind. |
|
Hi! Just a gentle follow-up on this PR. I’ve addressed the requested changes (switched process spawning to x/xpty) and CI is passing. Whenever you have time, I’d appreciate a re-review. Thanks! |
aymanbagabas
left a comment
There was a problem hiding this comment.
Looks good! Could you back port this to the v2-exp branch in a separate PR?
|
I’ve opened a backport PR targeting the v2-exp branch with the same changes. Thanks! |
…lity Previously, wish.Cmd's Set* methods were no-ops, causing alt screen escape sequences to be out of sync with child process output when using tea.Exec with bubbletea.Middleware. Now wish.Cmd stores and uses custom I/O handles when set by tea.Exec, ensuring proper sequencing of alt screen commands. Fixes charmbracelet#506
- Use xpty.WaitProcess on Windows for proper ConPTY waiting - Implement SetStdin/SetStdout/SetStderr to store and use custom I/O - Require all stdio to be set for custom path (avoids PTY deadlocks) Fixes charmbracelet#506
|
this looks awesome! thank you for fixing this |
7816ef4 to
9556373
Compare
The all-three-set check was copy-pasted across cmd_unix.go and cmd_windows.go with diverging comments (one claimed "any handle", the other justified the gate via "no ppty.Slave on Windows"). The policy is identical on both platforms and cannot be otherwise: ppty.Start overwrites all of Stdin/Stdout/Stderr with the PTY slave, so custom handles are necessarily all-or-nothing. Move the gate into Run as hasCustomStdio, leaving doRun with only the platform-specific start/wait logic, and make the no-PTY branch honor partially set handles with nil-checked fallback to the session so the setter contract holds there too.
xpty.WaitProcess was called with context.Background(), so a hung child on Windows had no cancellation path. Pass c.sess.Context() instead. On the error shape: WaitProcess returns os.Process.Wait's error, which is *exec.ExitError on non-zero exit, the same concrete type cmd.Wait yields on unix, so passing it through unwrapped keeps both platforms consistent. Requires xpty v0.1.4: earlier versions swallowed context cancellation and did not report non-zero exits on Windows.
TestCommandSetStdio used echo with an empty stdin reader, so it only proved stdout redirection. Switch to a cat round trip: output can only appear in the custom buffer if both stdin and stdout are wired through. Add TestCommandSetStdioPartial asserting that setting only stdout keeps the PTY wiring: if the all-or-nothing gate fired on a partial set, the output would land in the custom sink instead of the session. Also fix the comment on runEcho's nil-storm calls to say what they actually exercise: with all handles nil the gate stays closed.
v0.1.4 fixes WaitProcess to report context cancellation and to return *exec.ExitError for non-zero exits on Windows.
This PR fixes issue #506 where Bubble Tea programs running under Wish's
bubbletea.Middlewarewithssh.AllocatePty()would not properly release the alternate screen when executing child processes viatea.Exec.Problem
When using
tea.Execwithwish.Command, the child process output appeared "stacked" below the TUI instead of replacing it. This happened becausewish.Cmd's SetStdin, [SetStdout](cci:1: wish/cmd.go:76:0-79:1), and SetStderr methods were no-ops, causing Bubble Tea's alt screen escape sequences to be written to a different output path than the child process.Solution
Modified
wish.Cmdto properly store and use the I/O handles set by Bubble Tea's exec flow:stdin,stdout,stderrfields to [Cmd](cci:2 /wish/cmd.go:31:0-37:1) structChanges
ppty.Slaveon Windows)Testing
Before/After
Before: Child process output appears below TUI frame; scrollback shows "stacked" output
After: Child process replaces TUI cleanly; alt screen properly exits/enters around exec
Fixes #506