diff --git a/.nextchanges/cli/conpty-prompts.md b/.nextchanges/cli/conpty-prompts.md new file mode 100644 index 00000000000..7c1d45161de --- /dev/null +++ b/.nextchanges/cli/conpty-prompts.md @@ -0,0 +1 @@ +* Interactive prompts and pagers now work in Git for Windows' bash and other MSYS2 shells when they run under ConPTY (the default since Git for Windows 2.47) or directly in a Windows console. ([#7699](https://github.com/databricks/cli/pull/7699)) diff --git a/libs/cmdio/capabilities.go b/libs/cmdio/capabilities.go index 47017b53acc..1569354f445 100644 --- a/libs/cmdio/capabilities.go +++ b/libs/cmdio/capabilities.go @@ -3,7 +3,6 @@ package cmdio import ( "context" "io" - "strings" "github.com/databricks/cli/libs/env" ) @@ -15,19 +14,23 @@ type Capabilities struct { stdoutIsTTY bool stderrIsTTY bool + // stdinIsMSYSPipe is true when stdin is an MSYS2/Cygwin pty pipe rather + // than a real console (no ConPTY; see conpty.go). bubbletea can't read + // keystrokes from it, so prompts and pagers are disabled there. + stdinIsMSYSPipe bool + // Environment flags - color bool // Color output is enabled (NO_COLOR not set and TERM not dumb) - isGitBash bool // Git Bash on Windows + color bool // Color output is enabled (NO_COLOR not set and TERM not dumb) } // newCapabilities detects terminal capabilities from context and I/O streams. func newCapabilities(ctx context.Context, in io.Reader, out, err io.Writer) Capabilities { return Capabilities{ - stdinIsTTY: isTTY(in), - stdoutIsTTY: isTTY(out), - stderrIsTTY: isTTY(err), - color: env.Get(ctx, "NO_COLOR") == "" && env.Get(ctx, "TERM") != "dumb", - isGitBash: detectGitBash(ctx), + stdinIsTTY: isTTY(in), + stdoutIsTTY: isTTY(out), + stderrIsTTY: isTTY(err), + color: env.Get(ctx, "NO_COLOR") == "" && env.Get(ctx, "TERM") != "dumb", + stdinIsMSYSPipe: isMSYSPipe(in), } } @@ -37,9 +40,10 @@ func (c Capabilities) SupportsInteractive() bool { } // SupportsPrompt returns true if terminal supports user prompting. -// Prompts write to stderr and read from stdin, so we only need those to be TTYs. +// Prompts write to stderr and read keystrokes from stdin, so stderr must be a +// TTY and stdin a console rather than an MSYS2 pty pipe (see conpty.go). func (c Capabilities) SupportsPrompt() bool { - return c.SupportsInteractive() && c.stdinIsTTY && !c.isGitBash + return c.SupportsInteractive() && c.stdinIsTTY && !c.stdinIsMSYSPipe } // SupportsColor returns true if the given writer supports colored output. @@ -56,37 +60,20 @@ func (c Capabilities) SupportsStdoutColor() bool { } // SupportsPager returns true when we can drive an interactive pager. -// It builds on SupportsPrompt (stderr+stdin TTY, not Git Bash) and +// It builds on SupportsPrompt (stderr+stdin TTY, stdin not an MSYS2 pipe) and // additionally requires stdout to be a TTY so rendered rows land on // the terminal rather than a redirected file. func (c Capabilities) SupportsPager() bool { return c.SupportsPrompt() && c.stdoutIsTTY } -// detectGitBash returns true if running under a Cygwin/MSYS2 environment on -// Windows (Git Bash is the common case). -// -// We disable prompting there because bubbletea is not compatible with the -// Cygwin/MSYS2 pty emulation; making it work is a follow-up. -func detectGitBash(ctx context.Context) bool { - // Check if the MSYSTEM environment variable is set to "MINGW64" - msystem := env.Get(ctx, "MSYSTEM") - if strings.EqualFold(msystem, "MINGW64") { - // Check for typical Git Bash env variable for prompts - ps1 := env.Get(ctx, "PS1") - return strings.Contains(ps1, "MINGW") || strings.Contains(ps1, "MSYSTEM") - } - - return false -} - // InteractiveMode represents the level of terminal interactivity available. type InteractiveMode string // Interactive mode constants for user agent tracking. const ( InteractiveModeFull InteractiveMode = "full" // Both interactive output and prompts supported - InteractiveModeOutputOnly InteractiveMode = "output" // Interactive output only, no prompts (stdin not TTY or Git Bash) + InteractiveModeOutputOnly InteractiveMode = "output" // Interactive output only, no prompts (stdin not a TTY, or an MSYS2 pipe) InteractiveModeNone InteractiveMode = "none" // Non-interactive (CI, cron, stderr redirected) ) diff --git a/libs/cmdio/capabilities_test.go b/libs/cmdio/capabilities_test.go index cb3cf5954bc..aafc84931cd 100644 --- a/libs/cmdio/capabilities_test.go +++ b/libs/cmdio/capabilities_test.go @@ -4,7 +4,6 @@ import ( "io" "testing" - "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" ) @@ -56,18 +55,17 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: true, stderrIsTTY: true, color: true, - isGitBash: false, }, expected: true, }, { - name: "Git Bash blocks prompts", + name: "MSYS2 pipe stdin blocks prompts", caps: Capabilities{ - stdinIsTTY: true, - stdoutIsTTY: true, - stderrIsTTY: true, - color: true, - isGitBash: true, + stdinIsTTY: true, + stdoutIsTTY: true, + stderrIsTTY: true, + color: true, + stdinIsMSYSPipe: true, }, expected: false, }, @@ -78,7 +76,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: true, stderrIsTTY: true, color: true, - isGitBash: false, }, expected: false, }, @@ -89,7 +86,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: false, stderrIsTTY: true, color: true, - isGitBash: false, }, expected: true, }, @@ -100,7 +96,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: true, stderrIsTTY: false, color: true, - isGitBash: false, }, expected: false, }, @@ -111,7 +106,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: true, stderrIsTTY: true, color: false, - isGitBash: false, }, expected: false, }, @@ -122,7 +116,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { stdoutIsTTY: false, stderrIsTTY: false, color: true, - isGitBash: false, }, expected: false, }, @@ -135,16 +128,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { } } -func TestDetectGitBash(t *testing.T) { - ctx := t.Context() - assert.False(t, detectGitBash(ctx)) - - ctx = env.Set(ctx, "MSYSTEM", "MINGW64") - ctx = env.Set(ctx, "TERM", "xterm") - ctx = env.Set(ctx, "PS1", "\\[\033]0;$TITLEPREFIX:$PWD\007\\]\n\\[\033[32m\\]\\u@\\h \\[\033[35m\\]$MSYSTEM \\[\033[33m\\]\\w\\[\033[36m\\]`__git_ps1`\\[\033[0m\\]\n$") - assert.True(t, detectGitBash(ctx)) -} - func TestCapabilities_SupportsColor(t *testing.T) { tests := []struct { name string @@ -193,12 +176,11 @@ func TestCapabilities_InteractiveMode(t *testing.T) { expected InteractiveMode }{ { - name: "full interactive - all TTYs, color, no Git Bash", + name: "full interactive - all TTYs, color, console stdin", caps: Capabilities{ stdinIsTTY: true, stderrIsTTY: true, color: true, - isGitBash: false, }, expected: InteractiveModeFull, }, @@ -208,17 +190,16 @@ func TestCapabilities_InteractiveMode(t *testing.T) { stdinIsTTY: false, stderrIsTTY: true, color: true, - isGitBash: false, }, expected: InteractiveModeOutputOnly, }, { - name: "output only - Git Bash", + name: "output only - MSYS2 pipe stdin", caps: Capabilities{ - stdinIsTTY: true, - stderrIsTTY: true, - color: true, - isGitBash: true, + stdinIsTTY: true, + stderrIsTTY: true, + color: true, + stdinIsMSYSPipe: true, }, expected: InteractiveModeOutputOnly, }, @@ -228,7 +209,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) { stdinIsTTY: true, stderrIsTTY: false, color: true, - isGitBash: false, }, expected: InteractiveModeNone, }, @@ -238,7 +218,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) { stdinIsTTY: true, stderrIsTTY: true, color: false, - isGitBash: false, }, expected: InteractiveModeNone, }, @@ -248,7 +227,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) { stdinIsTTY: false, stderrIsTTY: false, color: false, - isGitBash: false, }, expected: InteractiveModeNone, }, diff --git a/libs/cmdio/conpty.go b/libs/cmdio/conpty.go new file mode 100644 index 00000000000..6e34abccace --- /dev/null +++ b/libs/cmdio/conpty.go @@ -0,0 +1,54 @@ +package cmdio + +import ( + "os" + + "github.com/mattn/go-isatty" +) + +// Native Windows programs started from an MSYS2/Cygwin terminal (mintty and +// the shells built on it) see one of two kinds of standard handles, depending +// on whether the MSYS2 runtime uses ConPTY, the Windows pseudo console: +// +// - With ConPTY, the handles are a real Windows console. isatty.IsTerminal +// is true, and everything works as in conhost or Windows Terminal, +// including bubbletea prompts and pagers. +// +// - Without ConPTY, the handles are named pipes that the MSYS2 runtime uses +// to emulate a pty (\\.\pipe\msys--pty-{from,to}-master). +// isatty.IsTerminal is false and isatty.IsCygwinTerminal is true. Output +// is still rendered by a terminal, so colors, spinners and line-based +// input work. Reading raw keystrokes does not: bubbletea's input reader +// calls GetConsoleMode on stdin and fails with +// "get console mode: The handle is invalid". Prompts and pagers must not +// run there. +// +// Whether ConPTY is used is a runtime setting (MSYS=enable_pcon or +// MSYS=disable_pcon, or mintty's ConPTY option), not something MSYSTEM or the +// shell reveals. It is on by default since Git for Windows 2.47; older +// installers wrote MSYS=disable_pcon by default. It only matters in mintty: a +// shell running directly in a console (conhost, Windows Terminal, the VS Code +// terminal) passes that console on to native programs. +// +// Hence the check is on the handle itself: see isMSYSPipe. To reproduce the +// pipe case, start mintty with MSYS=disable_pcon in its environment. +// +// References: +// - https://github.com/mintty/mintty/wiki/Tips#inputoutput-interaction-with-alien-programs +// - https://github.com/git-for-windows/build-extra/blob/main/installer/release.sh +// - https://github.com/git-for-windows/build-extra/blob/main/ReleaseNotes.md +// - https://github.com/mattn/go-isatty/blob/master/isatty_windows.go +// - https://code.visualstudio.com/docs/terminal/advanced#_windows-and-conpty + +// isMSYSPipe reports whether v is an MSYS2/Cygwin pty pipe rather than a real +// console, i.e. the "without ConPTY" case described above. It is always false +// outside Windows, and for anything that is not an *os.File. +func isMSYSPipe(v any) bool { + f, ok := v.(*os.File) + if !ok { + return false + } + fd := f.Fd() + // A Cygwin pty is a named pipe, so this never matches a console handle. + return isatty.IsCygwinTerminal(fd) //nolint:forbidigo +} diff --git a/libs/cmdio/conpty_test.go b/libs/cmdio/conpty_test.go new file mode 100644 index 00000000000..06d8d61ba67 --- /dev/null +++ b/libs/cmdio/conpty_test.go @@ -0,0 +1,53 @@ +package cmdio + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIsMSYSPipeNotAFile(t *testing.T) { + assert.False(t, isMSYSPipe(&bytes.Buffer{})) + assert.False(t, isMSYSPipe(FakeTTY(&bytes.Buffer{}))) + assert.False(t, isMSYSPipe(nil)) +} + +func TestIsMSYSPipeRegularFile(t *testing.T) { + f, err := os.Create(filepath.Join(t.TempDir(), "stdin")) + require.NoError(t, err) + defer f.Close() + assert.False(t, isMSYSPipe(f)) +} + +func TestIsMSYSPipeDevNull(t *testing.T) { + f, err := os.Open(os.DevNull) + require.NoError(t, err) + defer f.Close() + assert.False(t, isMSYSPipe(f)) +} + +func TestIsMSYSPipeAnonymousPipe(t *testing.T) { + // A plain pipe (e.g. `echo x | databricks ...`) is not an MSYS2 pty pipe; + // it is rejected for prompts because it is not a TTY at all. + r, w, err := os.Pipe() + require.NoError(t, err) + defer r.Close() + defer w.Close() + assert.False(t, isMSYSPipe(r)) + assert.False(t, isTTY(r)) +} + +func TestMSYSPipeBlocksPromptsOnly(t *testing.T) { + // An MSYS2 pty pipe counts as a TTY for output and line input, but not + // for prompts and pagers, which need raw keystrokes. + c := Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, color: true, stdinIsMSYSPipe: true} + assert.True(t, c.SupportsInteractive()) + assert.True(t, c.SupportsColor(FakeTTY(&bytes.Buffer{}))) + assert.False(t, c.SupportsPrompt()) + assert.False(t, c.SupportsPager()) + assert.Equal(t, InteractiveModeOutputOnly, c.InteractiveMode()) +} diff --git a/libs/cmdio/conpty_windows_test.go b/libs/cmdio/conpty_windows_test.go new file mode 100644 index 00000000000..614f06fce9d --- /dev/null +++ b/libs/cmdio/conpty_windows_test.go @@ -0,0 +1,76 @@ +package cmdio + +import ( + "fmt" + "os" + "testing" + + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/windows" +) + +// openNamedPipe creates a named pipe server and returns a client handle to +// it, mimicking the handles an MSYS2 runtime without ConPTY passes to native +// programs. +func openNamedPipe(t *testing.T, name string) *os.File { + t.Helper() + path, err := windows.UTF16PtrFromString(`\\.\pipe\` + name) + require.NoError(t, err) + + server, err := windows.CreateNamedPipe(path, + windows.PIPE_ACCESS_DUPLEX, windows.PIPE_TYPE_BYTE|windows.PIPE_WAIT, + 1, 4096, 4096, 0, nil) + require.NoError(t, err) + t.Cleanup(func() { _ = windows.CloseHandle(server) }) + + client, err := windows.CreateFile(path, + windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, 0, 0) + require.NoError(t, err) + f := os.NewFile(uintptr(client), name) + t.Cleanup(func() { f.Close() }) + return f +} + +func TestIsMSYSPipeWindows(t *testing.T) { + id := fmt.Sprintf("%016x", os.Getpid()) + + for _, name := range []string{ + "msys-" + id + "-pty0-from-master", + "msys-" + id + "-pty0-to-master", + "cygwin-" + id + "-pty1-from-master", + } { + t.Run(name, func(t *testing.T) { + f := openNamedPipe(t, name) + assert.True(t, isMSYSPipe(f)) + // It still counts as a TTY for output and line input. + assert.True(t, isTTY(f)) + }) + } + + t.Run("other named pipe", func(t *testing.T) { + f := openNamedPipe(t, "databricks-cli-test-"+id) + assert.False(t, isMSYSPipe(f)) + assert.False(t, isTTY(f)) + }) +} + +func TestNewCapabilitiesMSYSPipeStdin(t *testing.T) { + // End to end through newCapabilities: an MSYS2 pty pipe on stdin, with a + // terminal on stderr, allows interactive output but not prompts. + id := fmt.Sprintf("%016x", os.Getpid()+2) + in := openNamedPipe(t, "msys-"+id+"-pty0-from-master") + errw := openNamedPipe(t, "msys-"+id+"-pty0-to-master") + // Pin the environment so a developer's NO_COLOR or TERM=dumb can't leak in. + ctx := env.Set(env.Set(t.Context(), "NO_COLOR", ""), "TERM", "") + c := newCapabilities(ctx, in, errw, errw) + assert.True(t, c.stdinIsMSYSPipe) + assert.True(t, c.SupportsInteractive()) + assert.False(t, c.SupportsPrompt()) + + // The same pipe on stdout or stderr doesn't affect prompt support. + c = newCapabilities(ctx, FakeTTYReader(nil), errw, errw) + assert.False(t, c.stdinIsMSYSPipe) + assert.True(t, c.SupportsPrompt()) +} diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 368317e8ed5..5e4486db0c2 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -85,8 +85,8 @@ func IsPromptSupported(ctx context.Context) bool { // IsInputTTY reports whether stdin is a terminal, so that a user can type // input into the command. Unlike IsPromptSupported, it does not depend on -// stderr, NO_COLOR, TERM=dumb or Git Bash, which only matter for rendering -// prompts. +// stderr, NO_COLOR or TERM=dumb, which only matter for rendering prompts, and +// it accepts MSYS2 pty pipes, which carry line input but not keystrokes. func IsInputTTY(ctx context.Context) bool { c := fromContext(ctx) return c.capabilities.stdinIsTTY @@ -174,7 +174,6 @@ func MockDiscard(ctx context.Context) context.Context { stdoutIsTTY: false, stderrIsTTY: false, color: false, - isGitBash: false, }, outputFormat: flags.OutputText, in: io.NopCloser(strings.NewReader("")), diff --git a/libs/cmdio/testing.go b/libs/cmdio/testing.go index 5e35973ae33..bb8f7cc6202 100644 --- a/libs/cmdio/testing.go +++ b/libs/cmdio/testing.go @@ -76,7 +76,6 @@ func SetupTest(ctx context.Context, opts TestOptions) (context.Context, *Test) { stdoutIsTTY: opts.PromptSupported, stderrIsTTY: true, color: opts.PromptSupported, - isGitBash: false, }, in: rin, out: wout,