deluxe (Omarchy Linux, magpie started from the desktop) never saw the
Claude sign-in link. ~/.local/bin/claude was a script ending
`exec mise x "claude" -- "claude" "$@"`; without the shell's mise
environment, `mise x` looks claude up on PATH, finds the same script
(~/.local/bin is before mise's shims) and it runs itself for ever.
What changed:
- proc.Unwrap / proc.LookPath (internal/proc/wrapper.go): a small #!
script whose line is `[exec] mise|rtx x|exec [opts] [tool] -- <name>`
is resolved with `mise which <name>` (then `mise which --tool <tool>
<name>`), run as a probe (process group killed) with a 5s timeout,
magpie's environment and the home folder as cwd. The answer must be an
executable file that isn't the wrapper or another wrapper; otherwise,
or with no mise, the old path is kept. Answers are cached per file
(path, mtime, size); a failed ask is retried after a minute.
Windows is unchanged.
- proc.FindTool (claude via claudecode.Find: the sign-in, the gateway's
Claude subscription, `magpie claude-code`) unwraps what it finds.
- The siblings that looked an agent CLI up with exec.LookPath use
proc.LookPath: agent.binAt (detection, CLI version/update), codex,
kiro-cli, devin, cursor-agent, Air's opencode, nativemodels' npm
package lookup.
- The Claude sign-in already gave up after linkWait (30s) with "claude
auth login gave no link to open". It now adds the last line the CLI
printed, or, when it printed nothing, the program it ran and to run
`<path> auth login` in a terminal to see what it does.
asdf shims (`exec asdf exec "claude" "$@"`) are left alone: asdf exec
resolves the real program itself and doesn't recurse.
Verified (macOS, sandbox HOME, Go caches pinned):
- New tests: internal/proc/wrapper_test.go (wrapper -> mise's program;
no mise -> wrapper; mise fails / names the wrapper / names a folder /
hangs -> wrapper, and the hung mise's pid is gone; deluxe's own fixed
script, a node launcher, a wrapper of another command, a commented
line -> unchanged and mise never asked; parse cases; --tool retry),
internal/provider/signin_claude_wrapper_test.go (the full Claude
sign-in through the wrapper with a fake mise that really recurses:
link shown, account added; with no `mise which`, the sign-in fails in
linkWait naming the wrapper).
- Break checks: with find.go put back, TestFindToolUnwrapsMiseWrapper and
TestFindToolAsksWithWrappersTool FAIL (FindTool = the wrapper), and
TestClaudeSignInThroughMiseWrapper FAILs with "claude auth login gave
no link to open: .../local-bin/claude printed nothing in 5s". With the
old error string, TestClaudeSignInWrapperThatNeverStarts FAILs.
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; GOOS=linux go build -tags nogui;
GOOS=windows go build.
- go test -tags nogui ./...: every package ok except internal/agent and
internal/gateway, which hit the default 10m timeout with the machine
at load ~45 (different tests each run, none in the new code). Those
two were rerun with -timeout 40m; origin/main 217d2f8f2's
internal/agent alone took 2094s under the same load.
- Rebased on 9964535ad, -timeout 40m: internal/agent ok (1689s),
internal/proc ok, internal/provider ok (681s), internal/claudecode ok;
internal/gateway FAIL in one test, TestCodexHeardWithinItsIdleTimeout
(3.78s against its 3s wall-clock bound, body right, no retry taken).
Alone with -count=5 it passes 5 of 5. Filed as #1800; unrelated to
binary lookup.
Not verified: no real mise or the reporter's Omarchy machine; the fake
mise stands in for mise's which/x. The wrapper fixture is the line the
reporter quoted, not their whole file.
230 lines
7.8 KiB
Go
230 lines
7.8 KiB
Go
//go:build !windows
|
|
|
|
package proc
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// miseWrapperScript is the script deluxe (Discord, Omarchy Linux) had in
|
|
// ~/.local/bin/claude: its last line, as they gave it, runs claude
|
|
// through mise, and inside `mise x` that is the script again.
|
|
const miseWrapperScript = `#!/usr/bin/env bash
|
|
# claude, through mise
|
|
set -e
|
|
exec mise x "claude" -- "claude" "$@"
|
|
`
|
|
|
|
// wrapperFixture is a machine with the wrapper first on PATH, mise's real
|
|
// claude in its installs folder, and, when withMise, a fake mise beside
|
|
// which answers `mise which claude` with it and writes down each ask.
|
|
type wrapperFixture struct {
|
|
dir, wrapper, real, asked string
|
|
}
|
|
|
|
func newWrapperFixture(t *testing.T, wrapper, mise string) wrapperFixture {
|
|
t.Helper()
|
|
resetUnwrapped(t)
|
|
d := t.TempDir()
|
|
f := wrapperFixture{
|
|
dir: d,
|
|
wrapper: filepath.Join(d, "local-bin", "claude"),
|
|
real: filepath.Join(d, "mise", "installs", "claude", "2.1.0", "bin", "claude"),
|
|
asked: filepath.Join(d, "asked"),
|
|
}
|
|
for _, x := range []string{filepath.Dir(f.wrapper), filepath.Dir(f.real), filepath.Join(d, "mise-bin")} {
|
|
if err := os.MkdirAll(x, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
testenv.Program(t, f.wrapper, wrapper)
|
|
testenv.Program(t, f.real, "#!/bin/sh\necho 2.1.0 '(Claude Code)'\n")
|
|
path := filepath.Dir(f.wrapper)
|
|
if mise != "" {
|
|
mise = strings.NewReplacer("{asked}", f.asked, "{real}", f.real, "{wrapper}", f.wrapper).Replace(mise)
|
|
testenv.Program(t, filepath.Join(d, "mise-bin", "mise"), mise)
|
|
path += string(os.PathListSeparator) + filepath.Join(d, "mise-bin")
|
|
}
|
|
// /bin for the scripts' own sh, nothing of the host's mise
|
|
t.Setenv("PATH", path+string(os.PathListSeparator)+"/bin")
|
|
return f
|
|
}
|
|
|
|
// fakeMise answers `mise which claude` with the real program; `mise x`
|
|
// does what mise does, running the command found on PATH, which is the
|
|
// wrapper again.
|
|
const fakeMise = `#!/bin/sh
|
|
echo "$*" >> '{asked}'
|
|
if [ "$1 $2" = "which claude" ]; then echo '{real}'; exit 0; fi
|
|
exit 1
|
|
`
|
|
|
|
func resetUnwrapped(t *testing.T) {
|
|
t.Helper()
|
|
clear := func() {
|
|
unwrapped.Lock()
|
|
unwrapped.m = map[unwrapKey]unwrapEntry{}
|
|
unwrapped.Unlock()
|
|
}
|
|
clear()
|
|
t.Cleanup(clear)
|
|
}
|
|
|
|
func (f wrapperFixture) asks(t *testing.T) string {
|
|
t.Helper()
|
|
b, _ := os.ReadFile(f.asked)
|
|
return string(b)
|
|
}
|
|
|
|
// The wrapper is run as the program mise says it stands for, which runs
|
|
// Claude Code, rather than as itself, which runs itself for ever.
|
|
func TestFindToolUnwrapsMiseWrapper(t *testing.T) {
|
|
f := newWrapperFixture(t, miseWrapperScript, fakeMise)
|
|
if got := FindTool("claude"); got != f.real {
|
|
t.Fatalf("FindTool(claude) = %q, want mise's %q", got, f.real)
|
|
}
|
|
if got, err := LookPath("claude"); err != nil || got != f.real {
|
|
t.Fatalf("LookPath(claude) = %q, %v, want %q", got, err, f.real)
|
|
}
|
|
// asked once; the answer is kept while the wrapper is unchanged
|
|
if a := f.asks(t); a != "which claude\n" {
|
|
t.Fatalf("mise was asked %q", a)
|
|
}
|
|
}
|
|
|
|
// Without mise to ask, the wrapper is what it was before.
|
|
func TestFindToolKeepsWrapperWithoutMise(t *testing.T) {
|
|
f := newWrapperFixture(t, miseWrapperScript, "")
|
|
if got := FindTool("claude"); got != f.wrapper {
|
|
t.Fatalf("FindTool(claude) = %q, want the wrapper %q", got, f.wrapper)
|
|
}
|
|
}
|
|
|
|
// A mise that fails, names the wrapper itself, or doesn't answer leaves
|
|
// the wrapper as it was; one that doesn't answer is killed.
|
|
func TestFindToolKeepsWrapperWhenMiseDoesntSay(t *testing.T) {
|
|
for name, mise := range map[string]string{
|
|
"fails": "#!/bin/sh\necho \"$*\" >> '{asked}'\necho 'mise ERROR claude not installed' >&2\nexit 1\n",
|
|
"names it": "#!/bin/sh\necho \"$*\" >> '{asked}'\necho '{wrapper}'\n",
|
|
"names a dir": "#!/bin/sh\necho \"$*\" >> '{asked}'\necho /bin\n",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f := newWrapperFixture(t, miseWrapperScript, mise)
|
|
if got := FindTool("claude"); got != f.wrapper {
|
|
t.Fatalf("FindTool(claude) = %q, want the wrapper %q", got, f.wrapper)
|
|
}
|
|
if f.asks(t) == "" {
|
|
t.Fatal("mise wasn't asked")
|
|
}
|
|
})
|
|
}
|
|
t.Run("hangs", func(t *testing.T) {
|
|
was := miseWait
|
|
miseWait = 300 * time.Millisecond
|
|
t.Cleanup(func() { miseWait = was })
|
|
f := newWrapperFixture(t, miseWrapperScript, "#!/bin/sh\necho $$ > '{asked}'\nexec sleep 60\n")
|
|
start := time.Now()
|
|
if got := FindTool("claude"); got != f.wrapper {
|
|
t.Fatalf("FindTool(claude) = %q, want the wrapper %q", got, f.wrapper)
|
|
}
|
|
if d := time.Since(start); d > 30*time.Second {
|
|
t.Fatalf("waited %s for a mise that doesn't answer", d)
|
|
}
|
|
pid := strings.TrimSpace(f.asks(t))
|
|
if pid == "" {
|
|
t.Fatal("mise wasn't asked")
|
|
}
|
|
// the ask was killed with its group: nothing of it outlives the test
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for alive(pid) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("mise %s still running", pid)
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
})
|
|
}
|
|
|
|
func alive(pid string) bool {
|
|
_, err := os.Stat("/proc/" + pid)
|
|
if err == nil {
|
|
return true
|
|
}
|
|
if !os.IsNotExist(err) || runtimeHasProcFS() {
|
|
return false
|
|
}
|
|
// no /proc (macOS): ps says
|
|
out, _ := Command("ps", "-o", "stat=", "-p", pid).Output()
|
|
s := strings.TrimSpace(string(out))
|
|
return s != "" && !strings.HasPrefix(s, "Z")
|
|
}
|
|
|
|
func runtimeHasProcFS() bool {
|
|
_, err := os.Stat("/proc/self")
|
|
return err == nil
|
|
}
|
|
|
|
// A script that isn't such a wrapper is run as it is, and mise isn't
|
|
// asked: the user's own fix (the program `mise which` gave, in "$bin"),
|
|
// a launcher of node, a program.
|
|
func TestFindToolLeavesOtherScripts(t *testing.T) {
|
|
for name, script := range map[string]string{
|
|
"deluxe's fix": "#!/usr/bin/env bash\nbin=$(mise which claude) || exit 1\nexec mise x \"claude\" -- \"$bin\" \"$@\"\n",
|
|
"node": "#!/bin/sh\nexec node /opt/claude/cli.js \"$@\"\n",
|
|
"another tool": "#!/bin/sh\nexec mise x node -- codex \"$@\"\n",
|
|
"commented": "#!/bin/sh\n# exec mise x claude -- claude \"$@\"\nexec /opt/claude/claude \"$@\"\n",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f := newWrapperFixture(t, script, fakeMise)
|
|
if got := FindTool("claude"); got != f.wrapper {
|
|
t.Fatalf("FindTool(claude) = %q, want it as it is, %q", got, f.wrapper)
|
|
}
|
|
if a := f.asks(t); a != "" {
|
|
t.Fatalf("mise was asked %q", a)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The ways a wrapper names mise and its command.
|
|
func TestParseMiseWrapper(t *testing.T) {
|
|
for _, c := range []struct {
|
|
script string
|
|
ok bool
|
|
mise, tool string
|
|
}{
|
|
{miseWrapperScript, true, "mise", "claude"},
|
|
{"#!/bin/sh\nexec mise exec claude@latest -- claude \"$@\"\n", true, "mise", "claude@latest"},
|
|
{"#!/bin/sh\nexec /usr/bin/mise x -q 'npm:@anthropic-ai/claude-code' -- 'claude' \"$@\"\n", true, "/usr/bin/mise", "npm:@anthropic-ai/claude-code"},
|
|
{"#!/bin/zsh\n exec rtx x claude -- claude \"$@\" # old name\n", true, "rtx", "claude"},
|
|
{"#!/bin/sh\nmise x -- claude \"$@\"\n", true, "mise", ""},
|
|
{"#!/bin/sh\nexec mise x claude -- /opt/claude \"$@\"\n", false, "", ""},
|
|
{"#!/bin/sh\nexec mise run claude -- claude\n", false, "", ""},
|
|
{"#!/bin/sh\nexec asdf exec \"claude\" \"$@\"\n", false, "", ""},
|
|
} {
|
|
w, ok := parseMiseWrapper([]byte(c.script), "claude")
|
|
if ok != c.ok || w.mise != c.mise || w.tool != c.tool {
|
|
t.Errorf("%q: %+v %v, want %q %q %v", c.script, w, ok, c.mise, c.tool, c.ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
// When plain `mise which` doesn't know the command (the tool isn't in the
|
|
// home's config), the wrapper's own tool is asked for.
|
|
func TestFindToolAsksWithWrappersTool(t *testing.T) {
|
|
f := newWrapperFixture(t, "#!/bin/sh\nexec mise x claude@2.1.0 -- claude \"$@\"\n",
|
|
"#!/bin/sh\necho \"$*\" >> '{asked}'\n[ \"$*\" = \"which --tool claude@2.1.0 claude\" ] || exit 1\necho '{real}'\n")
|
|
if got := FindTool("claude"); got != f.real {
|
|
t.Fatalf("FindTool(claude) = %q, want %q", got, f.real)
|
|
}
|
|
if a := f.asks(t); a != "which claude\nwhich --tool claude@2.1.0 claude\n" {
|
|
t.Fatalf("mise was asked %q", a)
|
|
}
|
|
}
|