limactl-menu: do not leak limactl list warnings to the terminal

`listInstances` passed the stderr of `limactl list --json` through to the
terminal. On a fresh installation it printed "No instance found. Run
`limactl create` to create an instance." before the TUI started. The
warning stayed hidden behind the TUI and reappeared when the menu
suspended to run `limactl create`, which was confusing.

Buffer the stderr and only include it in the error when the command fails.
Also fix the error from `cmd.Start()` being discarded.

Fixes #5681

Assisted-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda committed 2026-10-26 17:31:01 +09:00
1 parent 6b72522a05
commit b0a0d62f7e
1 file changed
+11 -3
+11 -3
View File
@@ -5,6 +5,7 @@ package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
@@ -514,9 +515,16 @@ func (s *State) showEmptyState() {
func listInstances(ctx context.Context, limactl string) ([]*limatype.Instance, error) {
cmd := exec.CommandContext(ctx, limactl, "list", "--json")
cmd.Stderr = os.Stderr
// Buffer stderr rather than writing it to the terminal, so that warnings such as
// "No instance found. Run `limactl create` ..." do not reappear when the menu
// suspends the TUI to run another limactl command.
var stderr bytes.Buffer
cmd.Stderr = &stderr
stdout, err := cmd.StdoutPipe()
if err != nil || cmd.Start() != nil {
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
var res []*limatype.Instance
@@ -529,7 +537,7 @@ func listInstances(ctx context.Context, limactl string) ([]*limatype.Instance, e
res = append(res, &in)
}
if err := cmd.Wait(); err != nil {
return nil, err
return nil, fmt.Errorf("failed to run `limactl list --json`: %w: %s", err, strings.TrimSpace(stderr.String()))
}
slices.SortFunc(res, func(a, b *limatype.Instance) int {
if (a.Status == "Running") != (b.Status == "Running") {