proc.UserPath added "the newest-looking" nvm Node to magpie's PATH, the
last folder of ~/.nvm/versions/node/*/bin in string order. That isn't
the Node a terminal gets, and string order puts v9.11.2 after v22.12.0
and v22.3.0 after v22.12.0. So magpie could find, run and update a
codex/claude/gemini under a Node the user's terminal doesn't use (found
while answering 悠悠哥 on Discord, 9f407cafa). UserBinDirs (FindTool,
the Codex executable, nodeHere) took every version in reverse name
order, with the same flaw.
Now (internal/proc/nodever_other.go) the nvm folder is $NVM_DIR or
~/.nvm, and its default is resolved as nvm.sh does on load (nvm_auto
use -> nvm_resolve_local_alias default, read from nvm v0.40.3's
source): alias/default and each alias it names in turn (lts/* ->
lts/jod -> v22.x, lts/-N as nvm_normalize_lts counts it, user aliases),
a cycle being none; node/stable the highest installed; a partial
version (22, v20, 22.3) the highest installed it starts; an exact one
when its bin/node is installed. UserPath adds that one; with no usable
default (unset, not installed, cycle, iojs/unstable) the highest
version by number; with "system" none of nvm's, since the terminal then
runs the system Node. UserBinDirs lists the default first, then the
rest highest first.
Siblings:
- mise's installs and fnm's node-versions (UserBinDirs only) were in
reverse name order too: now by number, fnm's aliases/default first
(and $FNM_DIR looked in). mise's active Node is reached through its
shims, which come earlier.
- volta, asdf and mise shims, and n (one copy in /usr/local/bin), pick
the version themselves: no change.
- Windows (path_windows.go) lists no per-version folder: nvm-windows'
current Node is its NVM_SYMLINK on the registry PATH. No change.
- The WSL probe's wslbin (internal/agent/wsl.go) globbed
"$HOME"/.nvm/versions/node/*/bin, first in name order (v18 before the
default v22). It now asks nvm itself (nvm.sh sourced by bash with
--no-use, nvm_version default), then the rest highest first.
- NVM_DIR and FNM_DIR join agentenv.Vars: test sandboxes clear them, and
UserPath takes them from the login shell like the agents' variables.
Run: TestUserPathNvmDefault (15 real nvm layouts: v9 vs v22, lts/*,
lts/iron, lts/-1, 22, v20, exact v9, node, stable, a user alias, a
default not installed, lts/* naming a line not installed, a cycle,
system direct and through an alias), TestFindToolNvmDefaultFirst,
TestUserPathNvmDir, TestUserBinDirsFnmMise, TestWSLProbeNvmDefault.
With the old path_other.go 14 of the 15 nvm cases go red (v9.11.2
picked; the exact-v9 default passes), as do the other three; with the
default ignored by hand 9 go red; with the old probe line all 3 WSL
cases go red. Cross-checked nvmDefault against
nvm v0.40.3's own nvm_version default on 19 alias layouts: same version
each time (nvm says N/A where magpie falls back, and N/A for system
with no system node). The new probe run with the real nvm.sh under
/bin/sh and /bin/dash resolved lts/*, lts/iron and 18 right. go test
./internal/proc ./internal/agent ./internal/agentenv ./internal/provider
./internal/testenv; full go test -tags nogui ./...; gofmt -l; go vet,
GOOS=linux and GOOS=windows go vet -tags nogui on the touched packages.
All under a temp HOME; no real ~/.nvm read.
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// A command the WSL probe finds only under nvm is the one under the Node a
|
|
// terminal there gets: nvm's default, as nvm itself resolves it, else the
|
|
// highest version. The probe's glob took nvm's folders in name order, so
|
|
// a codex under v18 was taken for the default v22's, and v9's came last.
|
|
func TestWSLProbeNvmDefault(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the probe runs in a Linux distro")
|
|
}
|
|
for _, c := range []struct {
|
|
name, nvmSh, want string
|
|
}{
|
|
{"no nvm.sh: the highest, not the first by name", "", "v22.12.0"},
|
|
// a stand-in for nvm.sh: the probe asks nvm_version default, which
|
|
// the real one resolves through alias/default
|
|
{"nvm's default", "nvm_version() { [ \"$1\" = default ] && echo v9.11.2; }\n", "v9.11.2"},
|
|
{"nvm's default not installed: the highest", "nvm_version() { echo N/A; }\n", "v22.12.0"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
sys := filepath.Join(home, "sys") // PATH: nothing of codex's
|
|
os.MkdirAll(sys, 0o755)
|
|
nvm := filepath.Join(home, ".nvm")
|
|
for _, v := range []string{"v18.20.4", "v22.12.0", "v9.11.2"} {
|
|
bin := filepath.Join(nvm, "versions", "node", v, "bin")
|
|
os.MkdirAll(bin, 0o755)
|
|
testenv.Program(t, filepath.Join(bin, "codex"), "#!/bin/sh\n")
|
|
}
|
|
if c.nvmSh != "" {
|
|
to, err := exec.LookPath("timeout")
|
|
if err != nil {
|
|
t.Skip("no timeout(1)")
|
|
}
|
|
if err := os.Symlink(to, filepath.Join(sys, "timeout")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.WriteFile(filepath.Join(nvm, "nvm.sh"), []byte(c.nvmSh), 0o644)
|
|
}
|
|
cmd := exec.Command("/bin/sh", "-c", wslProbeScript)
|
|
cmd.Env = []string{"HOME=" + home, "PATH=" + sys + ":/usr/bin:/bin"}
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
want := "bin:codex " + filepath.Join(nvm, "versions", "node", c.want, "bin", "codex") + "\n"
|
|
if !strings.Contains(string(out), want) {
|
|
t.Fatalf("probe said:\n%s\nwant %q", out, want)
|
|
}
|
|
})
|
|
}
|
|
}
|