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

169 lines
5.3 KiB
Go

package proc
import (
"os"
"path/filepath"
"slices"
"strconv"
"strings"
)
// Node version managers' folders read the same on every system: the
// version folders a manager keeps, highest first, and fnm's Windows layout
// (fnmWin*), which magpie's own PATH and FindTool use on Windows and the
// tests take on any system.
// nodeVersions are the names of the version folders under root that have
// a folder bin (root/<version>/<bin>), highest version first (nodeVersionLess); a name that is
// no version (mise's "lts", "latest") after every one that is.
func nodeVersions(root, bin string) []string {
es, _ := os.ReadDir(root)
var names []string
for _, e := range es {
if st, err := os.Stat(filepath.Join(root, e.Name(), bin)); err == nil && st.IsDir() {
names = append(names, e.Name())
}
}
slices.SortStableFunc(names, func(a, b string) int {
switch {
case nodeVersionLess(b, a):
return -1
case nodeVersionLess(a, b):
return 1
}
return strings.Compare(a, b)
})
return names
}
// parseNodeVersion reads v22.12.0 (or 22.12.0, mise's, or 22, a partial
// one) as its numbers; false for a name that isn't one.
func parseNodeVersion(s string) ([3]int, int, bool) {
var v [3]int
parts := strings.Split(strings.TrimPrefix(s, "v"), ".")
if len(parts) > 3 || parts[0] == "" {
return v, 0, false
}
for i, p := range parts {
n, err := strconv.Atoi(p)
if err != nil || n < 0 {
return v, 0, false
}
v[i] = n
}
return v, len(parts), true
}
// nodeVersionLess says a is a lower version than b, by number: v9 before
// v22. A name that is no version is lower than one that is.
func nodeVersionLess(a, b string) bool {
va, _, oka := parseNodeVersion(a)
vb, _, okb := parseNodeVersion(b)
if oka != okb {
return okb
}
return oka && slices.Compare(va[:], vb[:]) < 0
}
func isDir(p string) bool {
st, err := os.Stat(p)
return err == nil && st.IsDir()
}
// fnm on Windows (#2685). A version's programs are in its installation
// folder itself — node-versions\<v>\installation\node.exe, npx.cmd — with
// no bin under it as on macOS and Linux, and aliases\default is a junction
// to the default version's installation folder (fnm's symlink_dir is
// junction::create there). A terminal set up with `fnm env` has its own
// link to aliases\default first on PATH, so its node, npx and the npm
// shims beside them are found; magpie, started from the Start menu, has
// the registry's PATH only and had none of them. An npx.cmd run by its
// full path started too, but the package it fetched runs `node` from
// PATH, and the server died with cmd's "'node' is not recognized".
// fnmWinRoots are the folders fnm keeps its Node versions in on Windows
// that exist here, the one fnm uses first: FNM_DIR, else its default,
// the data folder's fnm (etcetera's Windows data_dir, home\AppData\Roaming,
// which APPDATA names), else the legacy ~\.fnm.
func fnmWinRoots(home, appdata, fnmDir string) []string {
var roots []string
if filepath.IsAbs(fnmDir) {
roots = append(roots, fnmDir)
}
if filepath.IsAbs(appdata) {
roots = append(roots, filepath.Join(appdata, "fnm"))
}
if filepath.IsAbs(home) {
roots = append(roots, filepath.Join(home, "AppData", "Roaming", "fnm"), filepath.Join(home, ".fnm"))
}
var have []string
for _, r := range roots {
if isDir(r) && !slices.ContainsFunc(have, func(h string) bool { return strings.EqualFold(h, r) }) {
have = append(have, r)
}
}
return have
}
// fnmWinBins are the folders of the Node versions fnm keeps in dir on
// Windows: the default alias's (aliases\default, as it stands: a junction
// Stat follows) first, then each installation folder, newest first.
func fnmWinBins(dir string) []string {
var bins []string
if def := filepath.Join(dir, "aliases", "default"); isDir(def) {
bins = append(bins, def)
}
root := filepath.Join(dir, "node-versions")
for _, n := range nodeVersions(root, "installation") {
bins = append(bins, filepath.Join(root, n, "installation"))
}
return bins
}
// fnmWinUse is the folder of fnm's Node in dir a terminal gets on Windows:
// the default alias's; with no default alias, the highest installed. ""
// when the default leads nowhere: fnm's "system" alias points at a folder
// that can't exist (Z:\_fnm_\...), and such a terminal runs the system's
// Node, which PATH has already.
func fnmWinUse(dir string) string {
def := filepath.Join(dir, "aliases", "default")
if isDir(def) {
return def
}
if _, err := os.Lstat(def); err == nil {
return ""
}
root := filepath.Join(dir, "node-versions")
if ns := nodeVersions(root, "installation"); len(ns) > 0 {
return filepath.Join(root, ns[0], "installation")
}
return ""
}
// fnmWinPath is the folder of fnm's Node a terminal gets, in the root fnm
// uses (fnmWinRoots, fnmWinUse); none when fnm isn't here.
func fnmWinPath(home, appdata, fnmDir string) []string {
if roots := fnmWinRoots(home, appdata, fnmDir); len(roots) > 0 {
if d := fnmWinUse(roots[0]); d != "" {
return []string{d}
}
}
return nil
}
// addPath adds the folders PATH lacks, after the ones it has.
func addPath(dirs []string) {
cur := filepath.SplitList(os.Getenv("PATH"))
seen := map[string]bool{}
for _, d := range cur {
seen[d] = true
}
for _, d := range dirs {
if d != "" && filepath.IsAbs(d) && !seen[d] {
seen[d] = true
cur = append(cur, d)
}
}
os.Setenv("PATH", strings.Join(cur, string(os.PathListSeparator)))
}