fix(plugins): read the Pi version from stderr

Pi prints its version to stderr, so the host showed an empty version.
The fallback applies only to --version with empty stdout, so commands
whose stdout is parsed never see stderr noise.
This commit is contained in:
Willie
2026-09-21 15:50:19 +08:00
parent 0a16894265
commit cd79530cda
2 changed files with 18 additions and 0 deletions
+13
View File
@@ -3,8 +3,10 @@ package plugin
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"sync/atomic"
"testing"
)
@@ -47,6 +49,17 @@ func TestRunCommandMarksAnAbsentBinary(t *testing.T) {
}
}
func TestRunCommandReadsVersionFromStderr(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "pi"), []byte("#!/bin/sh\necho 0.73.1 >&2\n"), 0755); err != nil {
t.Fatal(err)
}
out, err := runCommand(context.Background(), dir, filepath.Join(dir, "pi"), "--version")
if err != nil || strings.TrimSpace(string(out)) != "0.73.1" {
t.Fatalf("version = %q %v", out, err)
}
}
func TestHostReportsReadyWhenTheCLIAnswers(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
+5
View File
@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
"time"
@@ -53,6 +54,10 @@ func runCommand(ctx context.Context, dir, bin string, args ...string) ([]byte, e
// Native output can contain credentials or command-source scripts.
return nil, agentError{key: "plugins.error.commandFailed", message: fmt.Sprintf("%s command failed; open the native client to resolve authentication, trust, or configuration", bin)}
}
// Some CLIs (Pi) print their version to stderr.
if stdout.Len() == 0 && slices.Equal(args, []string{"--version"}) {
return stderr.Bytes(), nil
}
return stdout.Bytes(), nil
}