TestFindTakesTheMachinesOwnFirst, TestClaudeSubscriptionRunsTheDownloadedClaudeCode
and TestClaudeSubscriptionWithoutClaudeCodeSaysHowToGetIt set PATH to an
empty folder to stand for a machine with no Claude Code, but
proc.FindTool falls back to proc.UserBinDirs, which lists the machine's
own /opt/homebrew/bin and /usr/local/bin. A Mac with
/opt/homebrew/bin/claude made Find() return it instead of the download.
proc.SystemDirs now gives every path of the machine's own that discovery
looks in. Outside tests it gives them as they are. In a test binary
testenv isolated (MAGPIE_TEST_SANDBOX set), each one is under the test's
home, at <home>/system/<path>, so the tests decide what is installed
there. The same leak was in the sibling finders, which now go through
it too:
- proc.UserBinDirs (Homebrew, /usr/local/bin), used by FindTool, the
agents' install check (proc.FindTool(a.Bin) in internal/agent) and codexExecutable;
- FindTool's and nodeHere's Windows fallback to the registry PATH and
%ProgramFiles%\nodejs;
- KiroExecutable, CursorExecutable and DevinExecutable's /usr/local/bin,
/opt/homebrew/bin and /Applications/Kiro CLI.app paths.
Production discovery doesn't change.
Not changed: update's and rtk's choice of a PATH folder to link into
(they only take a folder already on PATH, which the test sets) and
FindShell (a test that runs a shell needs the real one).
Reproduced on this Mac, which has /opt/homebrew/bin/gemini, npm, node and
opencode but no claude there: under testenv with PATH emptied,
FindTool("gemini") gave /opt/homebrew/bin/gemini. The new
TestSystemDirsLieInTheTestsHome checks that no tool is found with an
empty PATH, and that a claude put at SystemDirs("/opt/homebrew/bin") in
the test's home is still found. Making SystemDirs return its paths
unchanged turns it red (gemini, npm, node and opencode found on the
host).
Run: go test -tags nogui -count=1 ./internal/proc ./internal/claudecode
./internal/provider ./internal/agent . (all ok); the three tests from
the issue -run by name in ./internal/claudecode ./internal/gateway (pass);
gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on the touched
packages.
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package proc
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// FindTool is the command-line tool name as a terminal would run it: on
|
|
// PATH, else in one of the folders a user's tools are installed in
|
|
// (UserBinDirs), else, on Windows, on the PATH the registry has now, which
|
|
// a magpie started before the tool was installed doesn't have (#839: the
|
|
// Claude sign-in said Claude Code wasn't installed, its installer having
|
|
// put claude.exe in ~/.local/bin after magpie started). "" when none.
|
|
func FindTool(name string) string {
|
|
if p, err := exec.LookPath(name); err == nil {
|
|
return p
|
|
}
|
|
dirs := UserBinDirs()
|
|
if runtime.GOOS == "windows" {
|
|
dirs = append(dirs, SystemDirs(LoginPath()...)...)
|
|
}
|
|
return toolIn(name, runtime.GOOS, dirs)
|
|
}
|
|
|
|
// sandboxVar is testenv.Marker, which proc can't import: the home a test
|
|
// binary's testenv.Isolate made.
|
|
const sandboxVar = "MAGPIE_TEST_SANDBOX"
|
|
|
|
// SystemDirs are paths of the machine's own, outside any home (Homebrew's
|
|
// /opt/homebrew/bin, /usr/local/bin, an app in /Applications, the
|
|
// registry's PATH), as discovery looks in them: as they are, except in a
|
|
// test binary testenv isolated, where each lies under the test's home
|
|
// instead. A test that empties PATH means a machine without the tool; the
|
|
// host's /opt/homebrew/bin/claude was found anyway, so the tests of the
|
|
// downloaded Claude Code failed on a Mac with Homebrew's (#1525). A test
|
|
// that wants one there writes it under its home at that path.
|
|
func SystemDirs(paths ...string) []string {
|
|
root := os.Getenv(sandboxVar)
|
|
if root == "" {
|
|
return paths
|
|
}
|
|
out := make([]string, 0, len(paths))
|
|
for _, p := range paths {
|
|
if p == "" {
|
|
continue
|
|
}
|
|
out = append(out, filepath.Join(root, "system", strings.TrimPrefix(p, filepath.VolumeName(p))))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// toolIn is name's program in the first of dirs that has one: on Windows
|
|
// name.exe, name.cmd or name, as npm's shim or an installer's binary.
|
|
func toolIn(name, goos string, dirs []string) string {
|
|
names := []string{name}
|
|
if goos == "windows" {
|
|
names = []string{name + ".exe", name + ".cmd", name}
|
|
}
|
|
for _, d := range dirs {
|
|
if d == "" {
|
|
continue
|
|
}
|
|
for _, n := range names {
|
|
p := filepath.Join(d, n)
|
|
if st, err := os.Stat(p); err == nil && !st.IsDir() {
|
|
return p
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|