Files
lazygit/pkg/gui/tasks_adapter_test.go
Stefan HallerandClaude Fable 5 f000ce9f1c Never hand NewCmdTask a nil reader when a command fails to start
NewCmdTask feeds the reader returned by its start func into a
bufio.Scanner, and Scanner.Scan panics with a nil pointer dereference
when that reader is nil. Two start funcs could produce one:

- newPtyTask's fallback for a failed StartPty returned a literal nil
  reader, alongside an ExecCmd that was never started, so the intended
  "fall back to a plain cmd task" never worked. This crashed lazygit on
  Windows when using a custom pager with the main view zero-sized, e.g.
  after pressing + twice to enter full-screen mode with a side panel
  focused: ConPTY rejects zero dimensions, making StartPty fail.

- startCmdWithPipe returned nil when the pipe couldn't be created,
  which the Unix pty fallback path can trigger, since a failed pty
  start can leave the tty assigned to the command's stdout.

Make startCmdWithPipe never return a nil reader: when the pipe can't be
created, don't start the command at all and return an empty reader so
the task shuts down cleanly with the error in the log. Then route
newPtyTask's fallback through it, so a StartPty failure degrades to
running the command without a pty: the pager is lost, but the command's
output still renders.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 17:31:26 +02:00

25 lines
679 B
Go

package gui
import (
"bytes"
"os/exec"
"testing"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/stretchr/testify/assert"
)
func TestStartCmdWithPipeWhenPipeCannotBeCreated(t *testing.T) {
cmd := exec.Command("non-existent-command")
// Assigning stdout up front makes cmd.StdoutPipe fail. This happens in
// practice on the Unix pty fallback path: a failed pty start can leave
// the tty assigned to the command's stdout.
cmd.Stdout = &bytes.Buffer{}
_, r := startCmdWithPipe(cmd, utils.NewDummyLog())
// NewCmdTask's scanner panics on a nil reader, so startCmdWithPipe must
// not return one even when it can't create the pipe.
assert.NotNil(t, r)
}