Files
magpie/internal/proc/path_other_test.go
T
yetone a27bd9ec96 proc: the desktop app takes the agents' folder variables from the login shell, as it does PATH (atie on Discord: PI_CODING_AGENT_DIR set in the shell, magpie still wrote Pi's config to ~/.pi/agent/models.json) — magpie reads PI_CODING_AGENT_DIR, CODEX_HOME, CLAUDE_CONFIG_DIR and the rest with os.Getenv, but an app opened from the Finder, the Dock or the login item gets launchd's environment, with nothing a shell profile exports, so every agent was wired into its default folder in the home. proc.UserPath already asked the login shell for its PATH (sh -ilc printf with markers); the same single call now prints PATH and every variable in agentenv.Vars between the markers, parted by NULs, and each one magpie's environment doesn't have and the shell gives a non-empty value is set with os.Setenv; one magpie was started with is kept. These are read as soon as the app starts (the library is synced into the agents at once, the backend wires them), so UserPath now waits for the shell's answer up to 2 s instead of not at all; a slower profile still doesn't hold the window up longer, and what it says later is still taken. LoginPath uses the same call. Windows is unchanged (an app gets the user's environment from the registry there). Only the GUI's runGUI/runPanel call UserPath; no test but proc's own does. TestUserPathTakesAgentVars (a fake $SHELL whose profile exports PI_CODING_AGENT_DIR with a space, CODEX_HOME and an empty GROK_HOME) fails on the old code (nothing taken) and passes: Pi's folder is set by the time UserPath returns, magpie's own CODEX_HOME is kept, the empty and absent ones stay unset. Tried with a built binary calling proc.UserPath under env -i (launchd's PATH, a temp HOME): with /bin/zsh, .zshrc's PI_CODING_AGENT_DIR="$HOME/my pi" and CLAUDE_CONFIG_DIR came through, CODEX_HOME=/kept stayed, .zprofile's PATH folder was added, in 19 ms; with /bin/bash, .bash_profile's value came through. go vet, builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but TestClaudeSignedOut, TestClaudeAccountNamedAsItsLogin, TestClaudeIdentityServedWhileAsked and TestDevinSignedInOnlyInMagpie, timing tests on fake CLIs (5 s waits) that pass when the package is rerun; none of them reaches UserPath.
2026-10-03 22:34:36 +08:00

85 lines
3.0 KiB
Go

//go:build !windows
package proc
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
// A desktop app with launchd's PATH finds a claude installed under a custom
// npm prefix: from the folders such tools use, and from the login shell's
// PATH, whatever its profile prints around it.
func TestUserPath(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
npm := filepath.Join(home, ".npm-global", "bin")
os.MkdirAll(npm, 0o755)
os.WriteFile(filepath.Join(npm, "claude"), []byte("#!/bin/sh\n"), 0o755)
sh := filepath.Join(home, "sh")
os.WriteFile(sh, []byte("#!/bin/sh\necho 'welcome back!'\nPATH=/from/profile:$PATH\neval \"$2\"\necho bye\n"), 0o755)
t.Setenv("SHELL", sh)
t.Setenv("PATH", "/usr/bin:/bin")
UserPath()
p := os.Getenv("PATH")
if !strings.HasPrefix(p, "/usr/bin:/bin:") || !strings.Contains(p, npm) {
t.Fatalf("known folders not added after the old PATH: %q", p)
}
for end := time.Now().Add(5 * time.Second); !strings.Contains(os.Getenv("PATH"), "/from/profile"); time.Sleep(20 * time.Millisecond) {
if time.Now().After(end) {
t.Fatalf("the login shell's PATH never came: %q", os.Getenv("PATH"))
}
}
if n := strings.Count(os.Getenv("PATH"), "/usr/bin:"); n != 1 {
t.Fatalf("a folder PATH had was added again: %q", os.Getenv("PATH"))
}
}
// A desktop app started from the Finder doesn't have the variables a shell
// profile exports either; the agents' folders among them come from the login
// shell by the time UserPath returns (atie on Discord: PI_CODING_AGENT_DIR in
// the shell, Pi's models.json still written to ~/.pi/agent). One magpie was
// started with is kept, and one the profile leaves empty isn't set.
func TestUserPathTakesAgentVars(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
pi := filepath.Join(home, "pi agent") // a space survives
sh := filepath.Join(home, "sh")
os.WriteFile(sh, []byte("#!/bin/sh\necho 'a profile that talks'\n"+
"export PI_CODING_AGENT_DIR='"+pi+"'\nexport CODEX_HOME=/from/profile/codex\nexport GROK_HOME=\n"+
"eval \"$2\"\necho bye\n"), 0o755)
t.Setenv("SHELL", sh)
t.Setenv("PATH", "/usr/bin:/bin")
for _, v := range []string{"PI_CODING_AGENT_DIR", "GROK_HOME", "CLAUDE_CONFIG_DIR"} {
t.Setenv(v, "")
os.Unsetenv(v)
}
t.Setenv("CODEX_HOME", "/started/with")
// the Mac checks a new script on its first run, which can take longer
// than UserPath waits; a login shell is no new script
exec.Command(sh, "-c", ":").Run()
UserPath()
if got := os.Getenv("PI_CODING_AGENT_DIR"); got != pi {
t.Fatalf("PI_CODING_AGENT_DIR = %q, want the profile's %q", got, pi)
}
if got := os.Getenv("CODEX_HOME"); got != "/started/with" {
t.Fatalf("CODEX_HOME = %q: the one magpie was started with was replaced", got)
}
for _, v := range []string{"GROK_HOME", "CLAUDE_CONFIG_DIR"} {
if _, set := os.LookupEnv(v); set {
t.Fatalf("%s was set though the shell has no value for it", v)
}
}
if !strings.Contains(os.Getenv("PATH"), "/usr/bin") {
t.Fatalf("PATH lost: %q", os.Getenv("PATH"))
}
}