diff --git a/internal/library/bili.go b/internal/library/bili.go index 30f086636..b47db01e3 100644 --- a/internal/library/bili.go +++ b/internal/library/bili.go @@ -16,6 +16,7 @@ import ( "github.com/tidwall/jsonc" "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/claudecode" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/lazyre" "github.com/yetone/magpie/internal/proc" @@ -133,15 +134,14 @@ var biliInstaller = func() []string { return []string{npm, "install", "-g", biliPackage, "--prefix=" + filepath.Join(home(), ".local")} } -// biliRun runs bili with nothing to answer it. -var biliRun = func(bin string, timeout time.Duration, args ...string) (string, error) { +// biliRun runs bili with nothing to answer it. tools are the programs it +// runs by name (biliTool), whose folders go on its PATH. +var biliRun = func(bin string, tools []string, timeout time.Duration, args ...string) (string, error) { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() cmd := proc.CommandContext(ctx, bin, args...) cmd.Stdin = nil - // a bili in ~/.local/bin is a node script: its folder goes first, and - // node is found where the user's shell finds it - cmd.Env = append(os.Environ(), "PATH="+filepath.Dir(bin)+string(os.PathListSeparator)+os.Getenv("PATH")) + cmd.Env = append(os.Environ(), "PATH="+biliPATH(bin, tools, os.Getenv("PATH"))) out, err := cmd.CombinedOutput() text := strings.TrimSpace(string(out)) if err != nil { @@ -156,6 +156,47 @@ var biliRun = func(bin string, timeout time.Duration, args ...string) (string, e return text, nil } +// biliPATH is the PATH bili runs with: a bili in ~/.local/bin is a node +// script, so its folder goes first, and node is found where the user's +// shell finds it; then the folder of each of tools that isn't on path. +// bili's installer runs an agent's CLI by name (Claude Code's: `claude mcp +// add bili`; OpenCode's: `opencode --version`, which picks the config key), +// and magpie may have found that CLI where its own PATH doesn't reach: +// in a folder only proc.FindTool looks in (an nvm Node other than the +// default, pnpm's, mise's), or the Claude Code magpie downloaded. bili then failed with "spawnSync claude ENOENT" after it had +// already written Claude Code's settings.json (fwxkogaia on X, 0.1.4179). +func biliPATH(bin string, tools []string, path string) string { + have := filepath.SplitList(path) + dirs := []string{filepath.Dir(bin)} + for _, t := range tools { + if t == "" { + continue + } + d := filepath.Dir(t) + if !slices.Contains(have, d) && !slices.Contains(dirs, d) { + dirs = append(dirs, d) + } + } + if path != "" { + dirs = append(dirs, path) + } + return strings.Join(dirs, string(os.PathListSeparator)) +} + +// biliTool is the program of a's that bili's install and remove run by +// name: the agent's own, or for Claude Code with none of the user's, the +// one magpie runs (claudecode.Find: the one magpie downloaded); "" when +// there is none. +func biliTool(a *agent.Agent) string { + if p := a.BinPath(); p != "" { + return p + } + if a.ID == "claude" { + return claudecode.Find() + } + return "" +} + // biliAgents are the agents here bili's plugin can be given to. One in // WSL isn't: bili there is the distro's, and magpie runs this machine's. func biliAgents(agents []*agent.Agent) []*agent.Agent { @@ -223,11 +264,11 @@ func readBili(agents []*agent.Agent) *BiliView { v.Install = biliShown(c) } } else { - if out, err := biliRun(v.Path, 30*time.Second, "--version"); err == nil { + if out, err := biliRun(v.Path, nil, 30*time.Second, "--version"); err == nil { v.Version = biliVersion(out) } v.Saved = readBiliSaved() - out, err := biliRun(v.Path, 30*time.Second, "plugin", "list") + out, err := biliRun(v.Path, nil, 30*time.Second, "plugin", "list") if st = parseBiliList(out); err != nil || len(st) == 0 { v.ListErr = lastLines(out, 2) if err != nil { @@ -348,7 +389,7 @@ func SetBili(id string, on bool) (*BiliView, error) { } snap = append(snap, f) } - out, err := biliRun(bin, 2*time.Minute, "plugin", "install", biliLane(id)) + out, err := biliRun(bin, []string{biliTool(a)}, 2*time.Minute, "plugin", "install", biliLane(id)) if err != nil { return nil, err } @@ -378,7 +419,7 @@ func SetBili(id string, on bool) (*BiliView, error) { for _, p := range paths { cur[p] = biliRead(p) } - if _, err := biliRun(bin, 2*time.Minute, "plugin", "remove", biliLane(id)); err != nil { + if _, err := biliRun(bin, []string{biliTool(a)}, 2*time.Minute, "plugin", "remove", biliLane(id)); err != nil { return nil, err } snap := state[id] diff --git a/internal/library/bili_path_test.go b/internal/library/bili_path_test.go new file mode 100644 index 000000000..6bceac488 --- /dev/null +++ b/internal/library/bili_path_test.go @@ -0,0 +1,75 @@ +package library + +import ( + "os" + "path/filepath" + "runtime" + "slices" + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/proc" + "github.com/yetone/magpie/internal/testenv" +) + +// TestBiliFindsClaudeOffPath: magpie found Claude Code in a folder its +// PATH doesn't reach (proc.FindTool's; ~/.local/bin stands for it here), +// and bili's install, which runs `claude mcp add bili` by name, failed with +// "spawnSync claude ENOENT" (fwxkogaia on X). The real biliRun is given a +// bili that runs claude by name, as bili's installer does. +func TestBiliFindsClaudeOffPath(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("the stand-ins are sh scripts") + } + run := biliRun // the real one, before the fake takes its place + h := sandbox(t) + f := newFakeBili(t, h) + os.Remove(filepath.Join(h, "bin", "claude")) + claude := filepath.Join(h, ".local", "bin", "claude") + testenv.Program(t, claude, "#!/bin/sh\necho \"claude $*\"\n") + write(t, filepath.Join(h, ".claude", "settings.json"), "{}\n") + proc.Forget() + + // bili's own folder is another: npm's prefix, not ~/.local + bili := filepath.Join(h, "npm", "bin", "bili") + testenv.Program(t, bili, "#!/bin/sh\nexec claude mcp add bili\n") + var cc *agent.Agent + for _, a := range biliAgents(nil) { + if a.ID == "claude" { + cc = a + } + } + if cc == nil { + t.Fatal("Claude Code in ~/.local/bin not offered") + } + out, err := run(bili, []string{biliTool(cc)}, 30*time.Second, "plugin", "install", "claude") + if err != nil || out != "claude mcp add bili" { + t.Fatalf("bili ran claude: %q, %v", out, err) + } + + // SetBili tells bili's install and remove of Claude Code's program + if _, err := SetBili("claude", true); err != nil { + t.Fatal(err) + } + if _, err := SetBili("claude", false); err != nil { + t.Fatal(err) + } + if n := len(slices.DeleteFunc(slices.Clone(f.tools), func(p string) bool { return p != claude })); n != 2 { + t.Fatalf("install and remove told of %v, want %s twice", f.tools, claude) + } +} + +// TestBiliPATH: bili's folder first, then each tool's folder PATH lacks, +// then PATH as it was. +func TestBiliPATH(t *testing.T) { + sep := string(os.PathListSeparator) + bin := filepath.Join("/x", "bili", "bili") + on := filepath.Join("/usr", "bin") + got := biliPATH(bin, []string{filepath.Join(on, "opencode"), filepath.Join("/h", ".local", "bin", "claude"), "", filepath.Join("/h", ".local", "bin", "other")}, on+sep+"/bin") + want := strings.Join([]string{filepath.Join("/x", "bili"), filepath.Join("/h", ".local", "bin"), on, "/bin"}, sep) + if got != want { + t.Fatalf("PATH %q, want %q", got, want) + } +} diff --git a/internal/library/bili_test.go b/internal/library/bili_test.go index 1f58255a5..c1d3067a6 100644 --- a/internal/library/bili_test.go +++ b/internal/library/bili_test.go @@ -33,13 +33,17 @@ type fakeBili struct { runs []string last map[string]string // each file as bili last wrote it proxy string + tools []string // the programs each run was told of } func newFakeBili(t *testing.T, h string) *fakeBili { f := &fakeBili{t: t, h: h, on: map[string]bool{}, last: map[string]string{}, proxy: "http://127.0.0.1:18787/bili/"} path, run := biliPath, biliRun biliPath = func() string { return filepath.Join(h, ".local", "bin", "bili") } - biliRun = func(_ string, _ time.Duration, args ...string) (string, error) { return f.run(args...) } + biliRun = func(_ string, tools []string, _ time.Duration, args ...string) (string, error) { + f.tools = append(f.tools, tools...) + return f.run(args...) + } t.Cleanup(func() { biliPath, biliRun = path, run }) return f }