fix: use os.Pipe implementation for Windows CLI tests to reduce flakiness (#21874)

On Windows, `pty.New()` was creating a `ConPTY` (`PseudoConsole`) even
when no process would be attached. `ConPTY` requires a real process to
function correctly - without one, the pipe handles become invalid
intermittently, causing flaky test failures like `read |0: The handle is
invalid.`
This affected tests using the `ptytest.New()` + `Attach()` pattern for
in-process CLI testing.
The fix splits Windows PTY creation into two paths:
- `newPty()` now returns a simple pipe-based PTY for the `Attach()` use
case
- `newConPty()` creates a real `ConPTY`, called by `Start()` when a
process will be attached
AFAICT this will result in no change in behaviour outside of tests.

Fixes coder/internal#1277   

_Disclaimer: investigated and implemented by Claude Opus 4.5, reviewed
by me._

---------

Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
Danny Kopping
2026-02-03 11:50:28 +02:00
committed by GitHub
parent 353ebd9664
commit 24b20df7d5
5 changed files with 117 additions and 3 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ import (
func New(t *testing.T, opts ...pty.Option) *PTY {
t.Helper()
ptty, err := pty.New(opts...)
ptty, err := newTestPTY(opts...)
require.NoError(t, err)
e := newExpecter(t, ptty.Output(), "cmd")