Files
magpie/internal/proc/nodever_test.go
T
yetoneandEdward4226 5b4da5bee3 proc: magpie on Windows finds fnm's node and npx (#2685)
What changed
- fnm puts its Node on a terminal's PATH from the shell profile
  (`fnm env | Invoke-Expression`), never in the registry, so magpie
  started from the Start menu had no node: a Library MCP server run by
  bare npx was "notfound", and one run by npx.cmd's full path (the
  reporter's C:\Users\...\fnm\aliases\default\npx.cmd, alone or under
  `cmd /c`) exited 1 with cmd's "'node' is not recognized", since the
  package npx fetched runs node by name.
- Windows UserPath (no-op before) adds the folder fnm's default alias
  names, after what PATH has. UserBinDirs (FindTool) gets each fnm
  Node: the default alias first, then installations newest by number.
- fnm's Windows layout, checked against Schniz/fnm master (config.rs,
  directories.rs, version.rs, fs.rs, commands/env.rs): root FNM_DIR,
  else etcetera's Windows data_dir\fnm (home\AppData\Roaming, APPDATA),
  else legacy ~\.fnm; versions in node-versions\<v>\installation with
  node.exe/npx.cmd directly in it (no bin on Windows); aliases\default
  a junction to an installation folder; the "system" alias points at
  Z:\_fnm_\..., read as none. XDG vars on Windows only move fnm's
  multishell links, not its versions. LOCALAPPDATA isn't used for them.
- The shared node-version helpers (nodeVersions, parseNodeVersion,
  nodeVersionLess, isDir) and addPath moved from the !windows files to
  nodever.go unchanged; fnmWin* there take home/APPDATA/FNM_DIR as
  arguments so the tests run them on every OS.
- The Library check (checkStdioAt, this machine only) starts a bare
  command PATH lacks from where proc.FindTool finds it and puts that
  folder on the child's PATH (stdioProgram, withPath), as the owner's
  plan on the issue said. Docs: agent-wiring.md, library.md.

Run
- macOS: go test -count=1 ./internal/proc/ ./internal/library/ ok
  (TestNoCommandBypassesProc and the other proc guards included);
  gofmt -l clean; go vet GOOS=windows/darwin for proc, library, cli,
  daemon; GOOS=linux vet proc, library and (-tags nogui) cli, daemon;
  GOOS=windows test binaries for proc and library build.
- Windows box (Win 11 x64), cross-compiled amd64 test binaries:
  TestUserPathFnmNode (real junction via mklink /J, npx.cmd that runs
  node by name) red on origin/main 4a1cd7765's code ("PATH after
  UserPath = [C:\windows\System32], want fnm's default alias"), green
  here; TestFnmWin* green.
- Real fnm 1.39.0 + Node v24.22.1 installed into a sandbox
  (USERPROFILE/APPDATA under C:\Users\yeton\fnm-2685, no FNM_DIR,
  registry PATH without node). A throwaway probe calling UserPath then
  library.CheckServer on chrome-devtools-mcp@latest:
  4a1cd7765: npx -> notfound; ...\aliases\default\npx.cmd -> exited 1;
  cmd /c ...npx.cmd -> exited 1 (the reporter's case).
  This commit: all three ok, 32 tools.
- Broken by hand: stdioProgram without FindTool ->
  TestStdioProgramFindsWhatPathLacks red; fnmWinBins without the
  default alias -> TestFnmWinBins red; Windows UserPath as before ->
  TestUserPathFnmNode red (above).
- Windows full proc/library binaries: TestNoCommandBypassesProc,
  TestGoneAgentListedToTakeOut, TestCheckMixedServerEachSide,
  TestCheckKeepsToolNames and TestRTKNoHookIssuesAreEachAgents fail
  the same with 4a1cd7765's binaries run the same way; the guard and
  two of them read the source tree, which a binary run outside a
  checkout doesn't have; the other two are in #1827.

Not tried
- The desktop GUI on Windows with the reporter's fnm (the probe runs
  the same UserPath and CheckServer calls the app does).
- magpie serve/web/tui on Windows don't call UserPath (as on macOS), so
  there an absolute npx.cmd still lacks node on PATH; a bare npx is
  covered by the check's FindTool.
- Agents magpie starts get the new PATH only from the GUI/daemon.

Refs #2685

Co-authored-by: Edward4226 <234070868+Edward4226@users.noreply.github.com>
2026-10-27 06:45:37 +08:00

117 lines
4.6 KiB
Go

package proc
import (
"os"
"path/filepath"
"slices"
"testing"
)
// fnmWin lays fnm out at root as it is on Windows (#2685): each version's
// programs in node-versions\<v>\installation itself, no bin, and
// aliases\default leading to def's installation folder (a junction there,
// a symlink here; a folder of its own where neither can be made). "" for
// def leaves the alias out.
func fnmWin(t *testing.T, root, def string, versions ...string) {
t.Helper()
for _, v := range versions {
inst := filepath.Join(root, "node-versions", v, "installation")
if err := os.MkdirAll(inst, 0o755); err != nil {
t.Fatal(err)
}
for _, f := range []string{"node.exe", "npx.cmd"} {
if err := os.WriteFile(filepath.Join(inst, f), nil, 0o644); err != nil {
t.Fatal(err)
}
}
}
if def == "" {
return
}
alias := filepath.Join(root, "aliases", "default")
if err := os.MkdirAll(filepath.Dir(alias), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(root, "node-versions", def, "installation"), alias); err != nil {
if err := os.MkdirAll(alias, 0o755); err != nil {
t.Fatal(err)
}
}
}
// fnm's Windows folders in the order a terminal reaches them: the default
// alias's, as it stands (where the reporter's npx.cmd is), then each
// installation folder by version number, v24 before v9.
func TestFnmWinBins(t *testing.T) {
root := filepath.Join(t.TempDir(), "fnm")
fnmWin(t, root, "v20.18.0", "v9.11.2", "v24.19.0", "v20.18.0")
if err := os.MkdirAll(filepath.Join(root, "node-versions", "v25.0.0"), 0o755); err != nil {
t.Fatal(err) // being installed: no installation folder yet
}
inst := func(v string) string { return filepath.Join(root, "node-versions", v, "installation") }
want := []string{filepath.Join(root, "aliases", "default"), inst("v24.19.0"), inst("v20.18.0"), inst("v9.11.2")}
if got := fnmWinBins(root); !slices.Equal(got, want) {
t.Fatalf("fnmWinBins = %q\nwant %q", got, want)
}
if got, want := fnmWinUse(root), filepath.Join(root, "aliases", "default"); got != want {
t.Fatalf("fnmWinUse = %q, want the default alias %q", got, want)
}
if _, err := os.Stat(filepath.Join(fnmWinUse(root), "node.exe")); err != nil {
t.Fatalf("no node.exe in the default alias's folder: %v", err)
}
}
// With no default alias a terminal's fnm has no Node of its own; magpie
// takes the highest installed, as it does for nvm. A default that leads
// nowhere is fnm's "system" alias (Z:\_fnm_\...): the system's Node, which
// PATH has already, so none.
func TestFnmWinUseWithoutDefault(t *testing.T) {
root := filepath.Join(t.TempDir(), "fnm")
fnmWin(t, root, "", "v9.11.2", "v22.12.0")
if got, want := fnmWinUse(root), filepath.Join(root, "node-versions", "v22.12.0", "installation"); got != want {
t.Fatalf("no default: fnmWinUse = %q, want %q", got, want)
}
alias := filepath.Join(root, "aliases", "default")
if err := os.MkdirAll(filepath.Dir(alias), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(t.TempDir(), "_fnm_", "Nothing", "installation"), alias); err != nil {
t.Skipf("no symlink here: %v", err)
}
if got := fnmWinUse(root); got != "" {
t.Fatalf(`"system" default: fnmWinUse = %q, want none`, got)
}
}
// fnm's root on Windows: FNM_DIR, else %APPDATA%\fnm (etcetera's
// data_dir), else the legacy ~\.fnm; the folder a terminal gets is the
// default alias's in the first of them. Given as Windows sets them, under
// a temp folder.
func TestFnmWinPath(t *testing.T) {
home := t.TempDir()
appdata := filepath.Join(home, "AppData", "Roaming")
roaming := filepath.Join(appdata, "fnm")
if got := fnmWinPath(home, appdata, ""); got != nil {
t.Fatalf("no fnm: fnmWinPath = %q", got)
}
fnmWin(t, roaming, "v24.19.0", "v24.19.0")
if got, want := fnmWinPath(home, appdata, ""), []string{filepath.Join(roaming, "aliases", "default")}; !slices.Equal(got, want) {
t.Fatalf("APPDATA's fnm: fnmWinPath = %q, want %q", got, want)
}
// APPDATA unset or relative: the home's AppData\Roaming, where etcetera
// puts it
if got := fnmWinRoots(home, "AppData", ""); !slices.Equal(got, []string{roaming}) {
t.Fatalf("relative APPDATA: fnmWinRoots = %q, want %q", got, []string{roaming})
}
legacy := filepath.Join(home, ".fnm")
fnmWin(t, legacy, "v18.0.0", "v18.0.0")
moved := filepath.Join(home, "D", "fnm")
fnmWin(t, moved, "v22.12.0", "v22.12.0")
if got, want := fnmWinRoots(home, appdata, moved), []string{moved, roaming, legacy}; !slices.Equal(got, want) {
t.Fatalf("fnmWinRoots = %q, want %q", got, want)
}
if got, want := fnmWinPath(home, appdata, moved), []string{filepath.Join(moved, "aliases", "default")}; !slices.Equal(got, want) {
t.Fatalf("FNM_DIR: fnmWinPath = %q, want %q", got, want)
}
}