A Library server whose command is $HOME/bin/x (one value for a Mac's
/Users and Linux's /home) reached each agent as written. Claude Code
spawns '$HOME/bin/x' (it expands only ${NAME}), Codex and the rest
spawn it with no shell, and magpie's own check, router and Codex run
did the same.
library.json keeps what the user wrote. withCmdVars (cmdvars.go) makes
the copy an agent is written, in sync after runVia and in a project's
files:
- an agent that expands a reference in command and args itself
(Claude Code, CodeBuddy, WorkBuddy, Grok, Gemini, omp/Qoder/AtomCode,
OpenCode as {env:NAME}, Cursor as ${env:NAME}) gets the reference in
its syntax, $NAME written ${NAME}, so it is read from the agent's own
environment and a token in an arg stays out of its file;
- every other agent (Droid, which shares Claude Code's format, by the
new NoCmdRefs; Codex, Kiro, Crush, Copilot, Goose, Pi, Hermes, dsh,
Command Code, LiveAgent and those reading none) gets the values: HOME
is its own home, the rest from magpie's environment or a terminal's;
in WSL the distro's home and no variable of magpie's;
- a leading ~ is put in for all of them; a variable nothing has is
left as written.
Sync compares against that copy, so a second sync rewrites nothing.
magpie starting the command itself (startServer for the check and the
router, a distro's HOME asked of its login shell; specOf for Codex on
magpie) puts the same values in (launchCmd). Env and headers keep
their ${NAME} handling (envref.go) as before.
Run:
- mote: go vet ./internal/library for darwin, linux and windows;
gofmt -l clean; go test ./internal/library ./internal/gui: every test
but TestRunServerAsks (once, then 10/10 alone; filed #5433, my change
leaves a command without $ or ~ as it is) and TestMainWindowCorners
(macOS 27 on mote, #3949).
- wsl-run and win-run: TestCmdVars*, TestEnvRefs*, TestCheckStdio ok.
- Real Claude Code 2.1.316 in a sandbox HOME/CLAUDE_CONFIG_DIR, claude
mcp list: "${HOME}/bin/srv" Connected, "$HOME/bin/srv" ENOENT
posix_spawn '$HOME/bin/srv', the reporter's failure and why $NAME is
written ${NAME} for it.
Broken by hand: with the sync, specOf and startServer wiring taken out,
TestCmdVarsSync (claude and codex files) and TestCmdVarsLaunch ($VAR,
~ and the spec) are red.
Not tried: the reporter's own agent and server; a real Codex, Cursor,
OpenCode, Gemini or Droid starting the written entry (their expansion
is as envref.go's cited sources say); a real WSL distro (distroHome is
unit-free, the WSL target is tested with a set Home); npxDirect on a
command spelled with a variable (it still sees the written value).
138 lines
5.5 KiB
Go
138 lines
5.5 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// math_milian: a command's $HOME/bin/x, ${NAME} and ~ go to an agent that
|
|
// reads a reference in a command as that agent's reference, $NAME as
|
|
// ${NAME}; every other agent is given the values, HOME its own home, and
|
|
// a variable nothing has as written.
|
|
func TestCmdVarsPerAgent(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("MAGPIE_T_SET", "v1")
|
|
os.Unsetenv("MAGPIE_T_UNSET")
|
|
lv := loginVars
|
|
loginVars = func([]string) map[string]string { return nil }
|
|
t.Cleanup(func() { loginVars = lv })
|
|
|
|
s := &Server{Name: "x", Transport: "stdio", Command: "$HOME/bin/x",
|
|
Args: []string{"--notes", "~/notes", "${MAGPIE_T_SET}", "$MAGPIE_T_UNSET", "$1", "a~/b", "~"}}
|
|
keep := func(ref func(string) string) []string {
|
|
return []string{ref("HOME") + "/bin/x", "--notes", h + "/notes", ref("MAGPIE_T_SET"), ref("MAGPIE_T_UNSET"), "$1", "a~/b", h}
|
|
}
|
|
dollar := func(n string) string { return "${" + n + "}" }
|
|
put := []string{h + "/bin/x", "--notes", h + "/notes", "v1", "$MAGPIE_T_UNSET", "$1", "a~/b", h}
|
|
for _, c := range []struct {
|
|
name string
|
|
f mcpFile
|
|
want []string
|
|
}{
|
|
{"claude, codebuddy, workbuddy", mcpFile{Format: fmtClaude}, keep(dollar)},
|
|
{"droid", mcpFile{Format: fmtClaude, NoCmdRefs: true}, put},
|
|
{"grok", mcpFile{Format: fmtGrok}, keep(dollar)},
|
|
{"gemini", mcpFile{Format: fmtGemini}, keep(dollar)},
|
|
{"omp, qoder, atomcode", mcpFile{Format: fmtOmp}, keep(dollar)},
|
|
{"opencode", mcpFile{Format: fmtOpenCode}, keep(func(n string) string { return "{env:" + n + "}" })},
|
|
{"cursor", mcpFile{Format: fmtCursor}, keep(func(n string) string { return "${env:" + n + "}" })},
|
|
{"codex", mcpFile{Format: fmtCodex}, put},
|
|
{"kiro", mcpFile{Format: fmtKiro}, put},
|
|
{"copilot", mcpFile{Format: fmtCopilot}, put},
|
|
{"claude desktop", mcpFile{Format: fmtDesktop}, put},
|
|
{"pi-mcp-extension", mcpFile{Format: fmtPi, Literal: true}, put},
|
|
// in WSL: the distro's home, and no variable of magpie's
|
|
{"codex in WSL", mcpFile{Format: fmtCodex, WSL: true, Home: "/home/me"},
|
|
[]string{"/home/me/bin/x", "--notes", "/home/me/notes", "${MAGPIE_T_SET}", "$MAGPIE_T_UNSET", "$1", "a~/b", "/home/me"}},
|
|
{"claude in WSL", mcpFile{Format: fmtClaude, WSL: true, Home: "/home/me"},
|
|
[]string{"${HOME}/bin/x", "--notes", "/home/me/notes", "${MAGPIE_T_SET}", "${MAGPIE_T_UNSET}", "$1", "a~/b", "/home/me"}},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
got := c.f.withCmdVars(s)
|
|
if have := append([]string{got.Command}, got.Args...); !slices.Equal(have, c.want) {
|
|
t.Errorf("got %q\nwant %q", have, c.want)
|
|
}
|
|
})
|
|
}
|
|
if s.Command != "$HOME/bin/x" || s.Args[1] != "~/notes" {
|
|
t.Errorf("the library's server was changed: %+v", s)
|
|
}
|
|
plain := &Server{Name: "y", Transport: "stdio", Command: "npx", Args: []string{"-y", "pkg"}}
|
|
if (&mcpFile{Format: fmtCodex}).withCmdVars(plain) != plain {
|
|
t.Error("a command with nothing to put in is copied")
|
|
}
|
|
}
|
|
|
|
// The library keeps $HOME/bin/x as written; Claude Code's file has
|
|
// ${HOME}/bin/x, Codex's this home's path, and a second sync rewrites
|
|
// neither.
|
|
func TestCmdVarsSync(t *testing.T) {
|
|
h := sandbox(t)
|
|
s := Server{Name: "mine", Transport: "stdio", Command: "$HOME/bin/x", Args: []string{"~/notes"},
|
|
Agents: []string{"claude", "codex"}}
|
|
if r, err := SaveServer("", s); err != nil || len(r.Problems) != 0 {
|
|
t.Fatalf("%+v %v", r, err)
|
|
}
|
|
l, err := load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := l.server("mine"); got == nil || got.Command != "$HOME/bin/x" || !slices.Equal(got.Args, []string{"~/notes"}) {
|
|
t.Errorf("the library has %+v", got)
|
|
}
|
|
claude := read(t, filepath.Join(h, ".claude.json"))
|
|
if !strings.Contains(claude, `"${HOME}/bin/x"`) || !strings.Contains(claude, filepath.ToSlash(h)+`/notes"`) && !strings.Contains(claude, strings.ReplaceAll(h, `\`, `\\`)+`/notes"`) {
|
|
t.Errorf("claude:\n%s", claude)
|
|
}
|
|
codex := read(t, filepath.Join(h, ".codex/config.toml"))
|
|
if strings.Contains(codex, "$HOME") || strings.Contains(codex, "~/") || !strings.Contains(codex, "/bin/x") {
|
|
t.Errorf("codex:\n%s", codex)
|
|
}
|
|
if r, err := Sync(); err != nil || len(r.Changed) != 0 {
|
|
t.Errorf("sync: %+v %v", r, err)
|
|
}
|
|
if after := read(t, filepath.Join(h, ".claude.json")) + read(t, filepath.Join(h, ".codex/config.toml")); after != claude+codex {
|
|
t.Errorf("a sync rewrote them:\n%s", after)
|
|
}
|
|
}
|
|
|
|
// magpie starts a command with its ~ and variables put in: the check (and
|
|
// so the MCP router, which starts it the same way) and Codex on magpie.
|
|
func TestCmdVarsLaunch(t *testing.T) {
|
|
fake := fakeStdio(t, "ok", nil)
|
|
dir, base := filepath.Dir(fake.Command), filepath.Base(fake.Command)
|
|
t.Setenv("MAGPIE_T_DIR", dir)
|
|
byVar := *fake
|
|
byVar.Command = "$MAGPIE_T_DIR/" + base
|
|
if h := CheckServer(context.Background(), &byVar); h.State != "ok" || h.Tools != 3 {
|
|
t.Errorf("$MAGPIE_T_DIR: %+v", h)
|
|
}
|
|
// a home of the test's own (the fake refuses one outside it), with the
|
|
// server in it
|
|
h := t.TempDir()
|
|
b, err := os.ReadFile(fake.Command)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(h, base), b, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
byHome := *fake
|
|
byHome.Command = "~/" + base
|
|
if h := CheckServer(context.Background(), &byHome); h.State != "ok" || h.Tools != 3 {
|
|
t.Errorf("~: %+v", h)
|
|
}
|
|
spec, ok := specOf(&Server{Transport: "stdio", Command: "${HOME}/bin/x", Args: []string{"~/a", "$MAGPIE_T_DIR"}})
|
|
if want := []string{h + "/a", dir}; !ok || spec.Command != h+"/bin/x" || !slices.Equal(spec.Args, want) {
|
|
t.Errorf("spec: %+v", spec)
|
|
}
|
|
}
|