Files
magpie/internal/proc/path_windows_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

68 lines
2.5 KiB
Go

package proc
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// #2685: an MCP server started by fnm's npx.cmd, by its full path, runs
// the package it fetched, which runs `node` by name. A terminal's fnm put
// node's folder on PATH; magpie's PATH, the registry's, didn't have it,
// and the server exited 1 with cmd's "'node' is not recognized as an
// internal or external command, operable program or batch file." After
// UserPath the child finds fnm's node, and FindTool finds a bare npx.
func TestUserPathFnmNode(t *testing.T) {
home := t.TempDir()
appdata := filepath.Join(home, "AppData", "Roaming")
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", appdata)
t.Setenv("FNM_DIR", "")
sys := filepath.Join(os.Getenv("SystemRoot"), "System32")
t.Setenv("PATH", sys)
root := filepath.Join(appdata, "fnm")
inst := filepath.Join(root, "node-versions", "v24.19.0", "installation")
if err := os.MkdirAll(inst, 0o755); err != nil {
t.Fatal(err)
}
// npx's package bin, as npm's cmd shim runs it: node from PATH
npx := filepath.Join(inst, "npx.cmd")
for p, s := range map[string]string{npx: "@echo off\r\nnode %*\r\n", filepath.Join(inst, "node.cmd"): "@echo fnm node %*\r\n"} {
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
alias := filepath.Join(root, "aliases", "default")
if err := os.MkdirAll(filepath.Dir(alias), 0o755); err != nil {
t.Fatal(err)
}
// a junction, as fnm makes it
if out, err := Command("cmd", "/c", "mklink", "/J", alias, inst).CombinedOutput(); err != nil {
t.Fatalf("mklink /J: %v %s", err, out)
}
run := func() (string, error) {
cmd := Command(npx, "-y", "chrome-devtools-mcp@latest")
cmd.Dir = t.TempDir() // cmd looks in its folder before PATH
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
if out, err := run(); err == nil {
t.Fatalf("before UserPath node was found already (%q): the test proves nothing", out)
}
UserPath()
if dirs := filepath.SplitList(os.Getenv("PATH")); !slices.Contains(dirs, alias) {
t.Fatalf("PATH after UserPath = %q, want fnm's default alias %q on it", dirs, alias)
}
if out, err := run(); err != nil || out != "fnm node -y chrome-devtools-mcp@latest" {
t.Fatalf("npx.cmd by its full path: %v %q, want fnm's node run", err, out)
}
t.Setenv("PATH", sys)
Forget()
if got := FindTool("npx"); !strings.EqualFold(got, filepath.Join(alias, "npx.cmd")) {
t.Fatalf("FindTool(npx) = %q, want the default alias's %q", got, filepath.Join(alias, "npx.cmd"))
}
}