From 76e1fc1050ac799ffe21d06d5a1928a6c499ec0a Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Fri, 5 Apr 2024 10:56:32 -0400 Subject: [PATCH] Guard against non-tty stdin in bubbletea (#547) Turns out bubbletea errors when handed a non-tty stdin on Linux. https://github.com/charmbracelet/bubbletea/issues/964 This prevent the `run` command from running into this scenario. --- internal/cmd/project_loader.go | 6 +++--- internal/cmd/run.go | 10 +++++++++- internal/cmd/tui_common.go | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/cmd/project_loader.go b/internal/cmd/project_loader.go index 800bc669..50a8cfc2 100644 --- a/internal/cmd/project_loader.go +++ b/internal/cmd/project_loader.go @@ -23,9 +23,9 @@ type projectLoader struct { } func newProjectLoader(cmd *cobra.Command, allowUnknown, allowUnnamed bool) (*projectLoader, error) { - fd := os.Stdout.Fd() + ofd := os.Stdout.Fd() - if int(fd) < 0 { + if int(ofd) < 0 { return nil, fmt.Errorf("invalid file descriptor due to restricted environments, redirected standard output, system configuration issues, or testing/simulation setups") } @@ -35,7 +35,7 @@ func newProjectLoader(cmd *cobra.Command, allowUnknown, allowUnnamed bool) (*pro ctx: cmd.Context(), w: cmd.OutOrStdout(), r: cmd.InOrStdin(), - isTerminal: isTerminal(fd), + isTerminal: isTerminal(ofd) && isTerminal(os.Stdin.Fd()), }, nil } diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 541ddad9..5f912d06 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "context" "fmt" "io" @@ -182,10 +183,17 @@ func runCmd() *cobra.Command { return err } + // non-tty fails on linux otherwise + var stdin io.Reader + stdin = bytes.NewBuffer([]byte{}) + if isTerminal(os.Stdout.Fd()) { + stdin = cmd.InOrStdin() + } + runnerOpts = append( runnerOpts, client.WithinShellMaybe(), - client.WithStdin(cmd.InOrStdin()), + client.WithStdin(stdin), client.WithStdout(cmd.OutOrStdout()), client.WithStderr(cmd.ErrOrStderr()), client.WithProject(proj), diff --git a/internal/cmd/tui_common.go b/internal/cmd/tui_common.go index bc7226d2..c99524bd 100644 --- a/internal/cmd/tui_common.go +++ b/internal/cmd/tui_common.go @@ -26,7 +26,7 @@ func newProgramWithOutputs(output io.Writer, input io.Reader, model tea.Model, o opts = append(opts, tea.WithOutput(output)) } - if input != nil { + if input != nil && isTerminal(os.Stdin.Fd()) { opts = append(opts, tea.WithInput(input)) }