proc, update: an agent installed in a folder only fish's config adds is found when the terminal opens fish but the login shell is zsh or bash (John on Discord)

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.
This commit is contained in:
yetone
2026-10-10 21:42:00 +08:00
parent f1fc8c65d3
commit 9bad11449b
3 changed files with 257 additions and 13 deletions
+121 -2
View File
@@ -5,6 +5,7 @@ package proc
import (
"context"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
@@ -159,6 +160,14 @@ func npmPrefix(home string) string {
// shellEnv asks the user's login shell for its PATH and the values it has
// for agentenv.Vars ("" for one it hasn't); "" and nil when it can't say
// within a few seconds.
//
// fish and nushell are asked too, when they are installed and set up here
// but aren't the login shell: they aren't POSIX shells, so many keep zsh or
// bash as the login shell and have the terminal open fish instead (its
// profile setting, or exec fish from .zshrc). An agent installed in a folder
// only config.fish adds wasn't found (John on Discord: 二进制文件没有从用户环境变量
// 找到,我终端环境使用的fish). Their PATH comes after the login shell's, and
// a variable the login shell leaves empty is taken from them.
func shellEnv() (string, map[string]string) {
sh := os.Getenv("SHELL")
if sh == "" || !filepath.IsAbs(sh) {
@@ -167,7 +176,115 @@ func shellEnv() (string, map[string]string) {
sh = "/bin/sh"
}
}
return askShell(sh)
asks := append([]string{sh}, termShells(sh)...)
paths := make([]string, len(asks))
vars := make([]map[string]string, len(asks))
var wg sync.WaitGroup
for i, s := range asks {
wg.Add(1)
go func() {
defer wg.Done()
paths[i], vars[i] = askShell(s)
}()
}
wg.Wait()
return mergeShellEnv(paths, vars)
}
// termShells are the shells other than the login shell sh that a terminal
// here may open instead of it: fish and nushell, when installed and their
// config folder is there (each makes it on its first run).
func termShells(sh string) []string {
home, _ := os.UserHomeDir()
cfg := os.Getenv("XDG_CONFIG_HOME")
if !filepath.IsAbs(cfg) && home != "" {
cfg = filepath.Join(home, ".config")
}
confs := map[string][]string{
"fish": {filepath.Join(cfg, "fish")},
"nu": {filepath.Join(cfg, "nushell"), filepath.Join(home, "Library", "Application Support", "nushell")},
}
var out []string
for _, n := range []string{"fish", "nu"} {
if filepath.Base(sh) == n || cfg == "" {
continue
}
set := slices.ContainsFunc(confs[n], func(d string) bool {
st, err := os.Stat(d)
return err == nil && st.IsDir()
})
if p := FindShell(n); set && p != "" {
out = append(out, p)
}
}
return out
}
// shellDirs are the folders FindShell looks in, before /etc/shells and
// PATH: the system's, Homebrew's, MacPorts', Nix's, cargo's.
var shellDirs = func() []string {
home, _ := os.UserHomeDir()
dirs := []string{"/bin", "/usr/bin", "/opt/homebrew/bin", "/usr/local/bin", "/opt/local/bin",
"/home/linuxbrew/.linuxbrew/bin", "/run/current-system/sw/bin", "/nix/var/nix/profiles/default/bin"}
if home != "" {
dirs = append(dirs, filepath.Join(home, ".nix-profile", "bin"), filepath.Join(home, ".local", "bin"), filepath.Join(home, ".cargo", "bin"))
}
return dirs
}
// FindShell is the program of the shell name (zsh, bash, fish, nu)
// installed here, "" when there is none. A desktop app's PATH is launchd's,
// so the folders shells are installed in are looked in first, then the
// shells /etc/shells lists, then PATH.
func FindShell(name string) string {
isProg := func(p string) bool {
st, err := os.Stat(p)
return err == nil && !st.IsDir() && st.Mode()&0o111 != 0
}
for _, d := range shellDirs() {
if p := filepath.Join(d, name); isProg(p) {
return p
}
}
if b, err := os.ReadFile("/etc/shells"); err == nil {
for _, l := range strings.Split(string(b), "\n") {
l = strings.TrimSpace(l)
if filepath.IsAbs(l) && filepath.Base(l) == name && isProg(l) {
return l
}
}
}
p, _ := exec.LookPath(name)
return p
}
// mergeShellEnv puts shellEnv's answers together, the login shell's first:
// the PATH folders the first lacks after its own, and each variable from
// the first that has it.
func mergeShellEnv(paths []string, vars []map[string]string) (string, map[string]string) {
var dirs []string
for _, p := range paths {
for _, d := range filepath.SplitList(p) {
if d != "" && !slices.Contains(dirs, d) {
dirs = append(dirs, d)
}
}
}
var merged map[string]string
for _, vs := range vars {
if vs == nil {
continue
}
if merged == nil {
merged = make(map[string]string, len(vs))
}
for k, v := range vs {
if merged[k] == "" {
merged[k] = v
}
}
}
return strings.Join(dirs, string(os.PathListSeparator)), merged
}
// askShell runs sh as a terminal opens it and reads what shellEnv says.
@@ -210,7 +327,9 @@ const shellMark = "__magpie_path__"
// agentenv.Vars, between two marks. A NUL (which no variable can hold)
// parts the values: PATH first, then each of agentenv.Vars in order.
//
// A POSIX shell (zsh, bash, fish too) expands "$PATH" itself. nushell
// A POSIX shell (zsh, bash) expands "$PATH" itself, and so does fish:
// PATH, as each variable whose name ends in PATH, is a path variable there,
// joined with ':' in double quotes, not with the spaces another list gets. nushell
// doesn't: a double-quoted string is literal there, so it printed "$PATH",
// "$CLAUDE_CONFIG_DIR", ... and magpie set each variable to its own name
// (#738: then Claude sign-in failed with "mkdir $CLAUDE_CONFIG_DIR:
+133
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
@@ -177,3 +178,135 @@ func TestAskShellOwnSession(t *testing.T) {
t.Fatalf("zsh asked in a session of its own gave no PATH")
}
}
// fishProbeOutput is what fish 4.9.3 printed for shellProbe under a
// config.fish that echoes a line, puts /Users/john/.fishbin first on PATH
// and sets CODEX_HOME: PATH joined with ':' (a path variable), then each of
// agentenv.Vars. The captured tail after CODEX_HOME was all empty values;
// it is padded to agentenv.Vars' count as it is now.
func fishProbeOutput() string {
s := "hello from config.fish\n" + shellMark +
"/Users/john/.fishbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/pmk/env/global/bin" +
"\x00\x00/Users/john/codex"
return s + strings.Repeat("\x00", len(agentenv.Vars)-2) + shellMark
}
// fakeShells makes HOME a temp folder with a login shell (SHELL) whose
// PATH lacks fish's folder, and a fish that answers as the real one did
// (fishProbeOutput), where FindShell looks.
func fakeShells(t *testing.T) (home string) {
home = t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", "")
t.Setenv("PATH", "/usr/bin:/bin")
for _, v := range []string{"CODEX_HOME", "CLAUDE_CONFIG_DIR"} {
t.Setenv(v, "")
os.Unsetenv(v)
}
login := filepath.Join(home, "zsh")
testenv.Program(t, login, "#!/bin/sh\nPATH=/from/zsh:/usr/bin:/bin\nexport CLAUDE_CONFIG_DIR=/from/zsh/claude\neval \"$2\"\n")
t.Setenv("SHELL", login)
bin := filepath.Join(home, "shells")
os.MkdirAll(bin, 0o755)
out := filepath.Join(home, "fish.out")
os.WriteFile(out, []byte(fishProbeOutput()), 0o644)
testenv.Program(t, filepath.Join(bin, "fish"), "#!/bin/sh\ncat '"+out+"'\n")
old := shellDirs
shellDirs = func() []string { return []string{bin} }
t.Cleanup(func() { shellDirs = old })
return home
}
// A terminal that opens fish while the login shell is zsh (John on
// Discord: 二进制文件没有从用户环境变量找到,我终端环境使用的fish): fish is asked
// too, its PATH comes after zsh's and a variable zsh leaves unset is
// fish's, so an agent in a folder only config.fish adds is found.
func TestShellEnvAsksFishBesideLoginShell(t *testing.T) {
home := fakeShells(t)
os.MkdirAll(filepath.Join(home, ".config", "fish"), 0o755)
p, vars := shellEnv()
dirs := filepath.SplitList(p)
if len(dirs) < 2 || dirs[0] != "/from/zsh" || !slices.Contains(dirs, "/Users/john/.fishbin") {
t.Fatalf("PATH = %q, want the login shell's first and fish's folder after", p)
}
if n := len(slices.DeleteFunc(slices.Clone(dirs), func(d string) bool { return d != "/usr/bin" })); n != 1 {
t.Fatalf("a folder both shells have is there twice: %q", p)
}
if got := vars["CODEX_HOME"]; got != "/Users/john/codex" {
t.Fatalf("CODEX_HOME = %q, want fish's", got)
}
if got := vars["CLAUDE_CONFIG_DIR"]; got != "/from/zsh/claude" {
t.Fatalf("CLAUDE_CONFIG_DIR = %q, want the login shell's", got)
}
}
// fish installed but never set up (no ~/.config/fish) isn't the terminal's
// shell, and isn't asked.
func TestShellEnvLeavesUnusedFish(t *testing.T) {
fakeShells(t)
if p, _ := shellEnv(); strings.Contains(p, "fishbin") {
t.Fatalf("fish was asked with no config of its own: %q", p)
}
}
// The login shell being fish itself, it is asked once, and what it printed
// is read: PATH whole, CODEX_HOME, and nothing for the variables it hasn't.
func TestShellEnvFishLogin(t *testing.T) {
home := fakeShells(t)
os.MkdirAll(filepath.Join(home, ".config", "fish"), 0o755)
fish := filepath.Join(home, "shells", "fish")
t.Setenv("SHELL", fish)
if got := termShells(fish); len(got) != 0 {
t.Fatalf("fish asked again beside itself: %q", got)
}
p, vars := shellEnv()
if !strings.HasPrefix(p, "/Users/john/.fishbin:/usr/local/bin:") || strings.Contains(p, "/from/zsh") {
t.Fatalf("PATH = %q", p)
}
if vars["CODEX_HOME"] != "/Users/john/codex" || vars["CLAUDE_CONFIG_DIR"] != "" {
t.Fatalf("variables: %v", vars)
}
}
// The real fish, where it is installed: a folder fish_add_path adds in
// config.fish and a variable it sets reach magpie with zsh (here a plain
// sh) as the login shell, and ShellPath(fish) has the folder.
func TestShellEnvRealFish(t *testing.T) {
fish := FindShell("fish")
if fish == "" {
t.Skip("fish isn't installed")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", "")
t.Setenv("PATH", "/usr/bin:/bin")
for _, v := range []string{"CODEX_HOME", "CLAUDE_CONFIG_DIR"} {
t.Setenv(v, "")
os.Unsetenv(v)
}
bin := filepath.Join(home, "fish bin") // a space survives
os.MkdirAll(bin, 0o755)
os.MkdirAll(filepath.Join(home, ".config", "fish"), 0o755)
os.WriteFile(filepath.Join(home, ".config", "fish", "config.fish"),
[]byte("echo hi from fish\nfish_add_path -g '"+bin+"'\nset -gx CODEX_HOME "+home+"/codex\n"), 0o644)
login := filepath.Join(home, "zsh")
testenv.Program(t, login, "#!/bin/sh\neval \"$2\"\n")
t.Setenv("SHELL", login)
old := shellDirs
shellDirs = func() []string { return []string{filepath.Dir(fish)} }
t.Cleanup(func() { shellDirs = old })
p, vars := shellEnv()
if !slices.Contains(filepath.SplitList(p), bin) {
t.Fatalf("PATH = %q, want fish's %q in it", p, bin)
}
if got := vars["CODEX_HOME"]; got != home+"/codex" {
t.Fatalf("CODEX_HOME = %q, want fish's", got)
}
if got := vars["CLAUDE_CONFIG_DIR"]; got != "" {
t.Fatalf("CLAUDE_CONFIG_DIR = %q, want none", got)
}
if !slices.Contains(ShellPath(fish), bin) {
t.Fatalf("ShellPath(fish) lacks %q", bin)
}
}
+3 -11
View File
@@ -5,9 +5,10 @@ package update
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"github.com/yetone/magpie/internal/proc"
)
// installedShells are the shells installed here that a terminal may open:
@@ -17,16 +18,7 @@ func installedShells() []cliShell {
login := os.Getenv("SHELL")
var out []cliShell
for _, n := range []string{"zsh", "bash", "fish"} {
p := ""
for _, d := range []string{"/bin", "/usr/bin", "/opt/homebrew/bin", "/usr/local/bin"} {
if st, err := os.Stat(filepath.Join(d, n)); err == nil && !st.IsDir() {
p = filepath.Join(d, n)
break
}
}
if p == "" {
p, _ = exec.LookPath(n)
}
p := proc.FindShell(n)
if p == "" {
continue
}