Skip to content

Commit

Permalink
Guard against non-tty stdin in bubbletea (#547)
Browse files Browse the repository at this point in the history
Turns out bubbletea errors when handed a non-tty stdin on Linux.

charmbracelet/bubbletea#964

This prevent the `run` command from running into this scenario.
  • Loading branch information
sourishkrout committed Apr 5, 2024
1 parent 77916bc commit 76e1fc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/project_loader.go
Expand Up @@ -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")
}

Expand All @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/run.go
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/tui_common.go
Expand Up @@ -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))
}

Expand Down

0 comments on commit 76e1fc1

Please sign in to comment.