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

101 lines
3.3 KiB
Go

package proc
import (
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows/registry"
"github.com/yetone/magpie/internal/appdir"
)
// UserPath gives magpie, and what it starts, the Node a terminal has from
// fnm (#2685). An app started from the Start menu gets the user's PATH from
// the registry, as a terminal does, but fnm puts its Node on PATH from the
// shell's profile (`fnm env | Invoke-Expression`) and never in the
// registry: an MCP server run with npx, even by npx.cmd's full path, died
// with "'node' is not recognized" once the package it fetched ran node.
// The folder fnm's default alias names is added after the ones PATH has.
func UserPath() {
home, _ := os.UserHomeDir()
addPath(fnmWinPath(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")))
}
// UserBinDirs are the folders a user's command-line tools are installed in
// that exist here — npm's global one, bun's, volta's, pnpm's, mise's
// shims, the standalone installers', each Node version of fnm's, its
// default first — for finding one PATH doesn't reach.
func UserBinDirs() []string {
home, _ := os.UserHomeDir()
// mise's data folder: MISE_DATA_DIR, else XDG_DATA_HOME's mise (which
// mise follows on Windows too), else LOCALAPPDATA's
mise := filepath.Join(appdir.Getenv("LOCALAPPDATA"), "mise")
if d := os.Getenv("XDG_DATA_HOME"); filepath.IsAbs(d) {
mise = filepath.Join(d, "mise")
}
if d := os.Getenv("MISE_DATA_DIR"); filepath.IsAbs(d) {
mise = d
}
known := []string{
filepath.Join(appdir.Getenv("APPDATA"), "npm"),
filepath.Join(home, ".local", "bin"),
filepath.Join(home, ".bun", "bin"),
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "Volta", "bin"),
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "pnpm"),
filepath.Join(mise, "shims"),
}
for _, r := range fnmWinRoots(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")) {
known = append(known, fnmWinBins(r)...)
}
var have []string
for _, d := range known {
if st, err := os.Stat(d); filepath.IsAbs(d) && err == nil && st.IsDir() {
have = append(have, d)
}
}
return have
}
// LoginPath is the PATH the registry has now, the machine's then the
// user's: what a terminal opened now gets, and what a magpie started
// before a CLI was installed doesn't.
func LoginPath() []string {
var dirs []string
for _, k := range []struct {
root registry.Key
path string
}{
{registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`},
{registry.CURRENT_USER, `Environment`},
} {
key, err := registry.OpenKey(k.root, k.path, registry.QUERY_VALUE)
if err != nil {
continue
}
if v, _, err := key.GetStringValue("Path"); err == nil {
// %SystemRoot%\system32 as Windows expands it: os.ExpandEnv of
// $SystemRoot$\system32 made C:\Windows$\system32
if x, err := registry.ExpandString(v); err == nil {
v = x
}
for _, d := range strings.Split(v, ";") {
if d = strings.TrimSpace(d); d != "" {
dirs = append(dirs, d)
}
}
}
key.Close()
}
return dirs
}
// LoginEnv is "" on Windows: an app started from the Start menu gets the
// user's variables from the registry, as a terminal does, so magpie's own
// environment has them.
func LoginEnv(string) string { return "" }
// ShellPath is LoginPath on Windows: PowerShell and cmd both take the
// registry's PATH.
func ShellPath(string) []string { return LoginPath() }