Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/conpty-prompts.md
Original file line number Diff line number Diff line change
@@ -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))
45 changes: 16 additions & 29 deletions libs/cmdio/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmdio
import (
"context"
"io"
"strings"

"github.com/databricks/cli/libs/env"
)
Expand All @@ -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),
}
}

Expand All @@ -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
Comment thread
pietern marked this conversation as resolved.
Resolved
}

// SupportsColor returns true if the given writer supports colored output.
Expand All @@ -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)
)

Expand Down
46 changes: 12 additions & 34 deletions libs/cmdio/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"testing"

"github.com/databricks/cli/libs/env"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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,
},
Expand All @@ -78,7 +76,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
stdoutIsTTY: true,
stderrIsTTY: true,
color: true,
isGitBash: false,
},
expected: false,
},
Expand All @@ -89,7 +86,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
stdoutIsTTY: false,
stderrIsTTY: true,
color: true,
isGitBash: false,
},
expected: true,
},
Expand All @@ -100,7 +96,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
stdoutIsTTY: true,
stderrIsTTY: false,
color: true,
isGitBash: false,
},
expected: false,
},
Expand All @@ -111,7 +106,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
stdoutIsTTY: true,
stderrIsTTY: true,
color: false,
isGitBash: false,
},
expected: false,
},
Expand All @@ -122,7 +116,6 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
stdoutIsTTY: false,
stderrIsTTY: false,
color: true,
isGitBash: false,
},
expected: false,
},
Expand All @@ -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
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -228,7 +209,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) {
stdinIsTTY: true,
stderrIsTTY: false,
color: true,
isGitBash: false,
},
expected: InteractiveModeNone,
},
Expand All @@ -238,7 +218,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) {
stdinIsTTY: true,
stderrIsTTY: true,
color: false,
isGitBash: false,
},
expected: InteractiveModeNone,
},
Expand All @@ -248,7 +227,6 @@ func TestCapabilities_InteractiveMode(t *testing.T) {
stdinIsTTY: false,
stderrIsTTY: false,
color: false,
isGitBash: false,
},
expected: InteractiveModeNone,
},
Expand Down
54 changes: 54 additions & 0 deletions libs/cmdio/conpty.go
Original file line number Diff line number Diff line change
@@ -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-<id>-pty<N>-{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
}
53 changes: 53 additions & 0 deletions libs/cmdio/conpty_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Comment thread
pietern marked this conversation as resolved.
Resolved

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())
}
Loading
Loading