John on Discord: 二进制文件没有从用户环境变量找到,我终端环境使用的fish. The desktop app gets launchd's PATH and asks the login shell ($SHELL) for its own. fish isn't a POSIX shell, so many keep zsh or bash as the login shell and have the terminal open fish (its profile setting, or exec fish from .zshrc). magpie then asked only zsh, and a claude or codex in a folder config.fish adds (fish_add_path, set -gx PATH) wasn't seen. The suspected cause, fish joining "$PATH" with spaces, isn't it: PATH is a path variable in fish and is joined with ':' in double quotes. Checked with real fish 4.9.3 (macOS), 3.6 and 4.0.2 (Debian, Docker) under -ilc with the probe's printf and \0: PATH whole, the variables in place. With SHELL=fish the old code already found the agent. - shellEnv asks fish and nushell too, in parallel with the login shell, when they are installed and their config folder exists (each makes it on its first run) and they aren't the login shell. Their PATH folders come after the login shell's; a variable of agentenv.Vars the login shell leaves empty is taken from them. - proc.FindShell finds a shell in the system's, Homebrew's, MacPorts', Nix's, ~/.local/bin and cargo's folders, then /etc/shells, then PATH. Settings > Command line (update.installedShells) uses it too, so a fish from MacPorts or Nix is listed there as well. - The shellProbe comment now says why fish's "$PATH" is right. Tests: TestShellEnvAsksFishBesideLoginShell (a fake fish answering with the bytes real fish 4.9.3 printed) and TestShellEnvRealFish (real fish, skipped where it isn't installed) fail on the old shellEnv: PATH = "/from/zsh:/usr/bin:/bin", want the login shell's first and fish's folder after PATH = "/usr/bin:/bin", want fish's ".../fish bin" in it TestShellEnvLeavesUnusedFish and TestShellEnvFishLogin cover fish with no config and fish as the login shell. Run: internal/proc shell tests on macOS with the downloaded fish 4.9.3 and on Linux (golang:1.26.8 + Debian fish 4.0.2) all pass; go test ./... under a temp HOME: all pass except gateway (10m timeout under load; the failing TestClaudeUnloadableSessionStartsAnew passes alone) and gui TestOTelPreferencesAPI (TempDir cleanup race, passes alone -count=3), neither touching this code; gofmt -l clean; go vet and GOOS=linux, GOOS=windows go vet on internal/proc and internal/update clean. Real use: a sandbox magpie (go build -tags dev, own HOME, ports 3486/3487, launchd-like PATH=/usr/bin:/bin:/usr/sbin:/sbin, SHELL=/bin/zsh with no .zshrc, fish at ~/.local/bin/fish, a fake claude only in a folder config.fish adds with fish_add_path): the old build reports no claude in /api/agents/cli and /api/state; this one finds it (version 2.1.99 of the fake). With SHELL=fish both builds find it.
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
//go:build !windows
|
|
|
|
package update
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// installedShells are the shells installed here that a terminal may open:
|
|
// zsh, bash and fish, the login shell ($SHELL) marked, and that one too
|
|
// when it is another (nu, sh), with no profile magpie writes.
|
|
func installedShells() []cliShell {
|
|
login := os.Getenv("SHELL")
|
|
var out []cliShell
|
|
for _, n := range []string{"zsh", "bash", "fish"} {
|
|
p := proc.FindShell(n)
|
|
if p == "" {
|
|
continue
|
|
}
|
|
out = append(out, cliShell{Name: n, Path: p, Default: filepath.Base(login) == n})
|
|
}
|
|
if login != "" && filepath.IsAbs(login) && !slices.ContainsFunc(out, func(s cliShell) bool { return s.Default }) {
|
|
out = append([]cliShell{{Name: filepath.Base(login), Path: login, Default: true}}, out...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// cliDir is the folder a link to exe goes in for a shell with PATH dirs:
|
|
// where the magpie it runs now (cmd) is, when that is another copy in a
|
|
// folder magpie can write — the link takes its place, as install.sh's
|
|
// ln -sf does — else the first of the folders a user's commands usually go
|
|
// in that is on it and can be written to; "" when none is.
|
|
func cliDir(exe string, dirs []string, cmd string) string {
|
|
if cmd != "" {
|
|
if d := filepath.Dir(cmd); Writable(d) {
|
|
return d
|
|
}
|
|
return ""
|
|
}
|
|
var cands []string
|
|
if h := os.Getenv("HOME"); filepath.IsAbs(h) {
|
|
cands = append(cands, filepath.Join(h, ".local", "bin"), filepath.Join(h, "bin"))
|
|
}
|
|
home := len(cands)
|
|
cands = append(cands, "/opt/homebrew/bin", "/usr/local/bin", "/home/linuxbrew/.linuxbrew/bin")
|
|
for i, d := range cands {
|
|
if !slices.Contains(dirs, d) {
|
|
continue
|
|
}
|
|
st, err := os.Stat(d)
|
|
if err == nil && st.IsDir() && Writable(d) {
|
|
return d
|
|
}
|
|
// on PATH but not made yet: the home's are made
|
|
if i < home && os.IsNotExist(err) {
|
|
return d
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// cliPlace links exe as magpie in dir, over what is there.
|
|
func cliPlace(exe, dir string) error {
|
|
dst := filepath.Join(dir, "magpie")
|
|
if sameFile(dst, exe) {
|
|
return nil
|
|
}
|
|
if st, err := os.Lstat(dst); err == nil && st.IsDir() {
|
|
return fmt.Errorf("%s is a folder", dst)
|
|
}
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
tmp := dst + ".magpie-new"
|
|
os.Remove(tmp)
|
|
if err := os.Symlink(exe, tmp); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(tmp, dst); err != nil {
|
|
os.Remove(tmp)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|