On Windows with Node managed by mise, the Agents page read "无法判断安装方式" for an agent installed with npm install -g. howInstalled decided from the binary's path, and a mise shim leads nowhere near node_modules: on a Mac or Linux it is a symlink to the mise binary; on Windows a copy of mise-shim.exe (exe mode, the default), a .cmd plus a bash script running `mise x --` (file), or a hard or symbolic link to mise.exe. A shim is now recognised (a link to mise, or a file in mise's shims folder: parent "mise", a .mode file, or MISE_DATA_DIR/shims) and mise is asked which program it runs, `mise which <cmd>` from the home folder; how it was installed is read from that program. On Windows mise names npm's extensionless sh script, so the .cmd beside it is used. The update runs that Node version's own npm with --prefix, then `mise reshim`: on Windows npm install -g makes no shim for a new command (seen on the box), mise's Unix npm wrapper reshims itself, so it is harmless there. Siblings: - asdf shims (scripts with "# asdf-plugin:" and `exec asdf exec`) are asked with `asdf which` the same way; asdf-nodejs's npm reshims itself and asdf has no Windows build. - proc.UserBinDirs on Windows also looks in mise's shims folder (MISE_DATA_DIR, else XDG_DATA_HOME\mise, which mise follows on Windows, else %LOCALAPPDATA%\mise) for an agent PATH doesn't reach. - Install side: npmIn accepts npm.exe beside node as well as npm.cmd. - WSL agents get no update path (CLI skips them) and wsl.go's probe already lists mise/asdf shims: unchanged. - MISE_DATA_DIR and ASDF_DIR join agentenv.Vars, so a test sandbox clears them (TestFolderVarsAreListed). Fixtures are real bytes: internal/agent/testdata/mise-windows from mise 2026.10.7 on the Windows box (file-mode snow.cmd and snow, .mode files, npm's snow.cmd/snow in the Node folder); the Mac layout and the asdf shim's text from mise 2026.10.7 and asdf 0.20.2 release binaries in a sandbox home. Run: - flp's own layout (where.exe claude: installs\node\24.20.0\claude, claude.cmd, shims\claude.exe; mise which claude names the extensionless script): TestMiseShimWindowsFiveAgents covers claude, pi, codex, opencode and codebuddy through shims\<cmd>.exe; run on the Windows box, red with the fix removed by hand, green restored. - go test -tags nogui ./internal/agent ./internal/proc -run 'Shim|HowInstalled|UpdateCLI|NpmIn|InstalledVersion'; with installedAt skipping the shim by hand TestMiseShimIsNodesNpm, TestUpdateCLIThroughMiseShim and TestAsdfShimIsNodesNpm go red, green again restored. - Real mise on the Mac (sandbox home, mise 2026.10.7, node 22.23.3, snow-ai): detection through the real shim and `mise which`, and a real update 0.9.1 -> 0.9.2 through UpdateCLI. - Real mise on Windows (the box, sandbox USERPROFILE/LOCALAPPDATA, mise 2026.10.7, node 22.23.3, snow-ai): the Windows test binary's unit tests (TestMiseShimWindows, TestHowInstalledWindowsShim, TestNpmIn) pass; a temporary test through the real shim in exe, file and hardlink modes found npm.cmd --prefix <node>, version 0.9.2; after npm install -g snow-ai@0.9.1 a real UpdateCLI took it to 0.9.2 and reshimmed; snow.exe ran 0.9.2. Symlink mode was not tried (needs developer mode); it is the link-to-mise case the Mac covers. flp's own machine was not tried. - go test -tags nogui -timeout 40m ./...; gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui ./...
204 lines
6.1 KiB
Go
204 lines
6.1 KiB
Go
package agent
|
|
|
|
// A version manager's shim (mise's, asdf's) stands where PATH finds an
|
|
// agent's CLI, and runs the program of the Node version it picks. The shim
|
|
// itself says nothing of how that program was installed: on a Mac or Linux
|
|
// mise's is a link to the mise binary, on Windows a copy of mise-shim.exe,
|
|
// a hard or symbolic link to mise.exe, or a .cmd running `mise x --`; none
|
|
// leads into node_modules or names it (flp on Discord: an npm -g agent under
|
|
// mise on Windows read "can't tell how it was installed"). The program is
|
|
// asked of the manager, `mise which <cmd>` / `asdf which <cmd>`, and how it
|
|
// was installed read from that: npm's own, of that Node version, with its
|
|
// prefix.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// shim is a version manager's shim an agent's CLI was found as.
|
|
type shim struct {
|
|
kind string // mise, asdf
|
|
mgr string // the manager's program, to ask which program the shim runs
|
|
name string // the command the shim stands for: claude, not claude.exe
|
|
}
|
|
|
|
// asdfExec is the line asdf's shims run: `exec asdf exec "claude" "$@"`
|
|
// (asdf 0.16 on), `exec /home/u/.asdf/bin/asdf exec "claude" "$@"` before.
|
|
var asdfExec = regexp.MustCompile(`(?m)^exec (\S*asdf) exec "`)
|
|
|
|
// shimAt says whose shim bin is; nil when it is none, or the manager to ask
|
|
// isn't here.
|
|
func shimAt(bin string) *shim {
|
|
name := filepath.Base(bin)
|
|
if runtime.GOOS == "windows" {
|
|
switch strings.ToLower(filepath.Ext(name)) {
|
|
case ".exe", ".cmd", ".bat":
|
|
name = strings.TrimSuffix(name, filepath.Ext(name))
|
|
}
|
|
}
|
|
if s := miseShim(bin); s != nil {
|
|
s.name = name
|
|
return s
|
|
}
|
|
// asdf's: a script of its own (it has no Windows build)
|
|
if st, err := os.Stat(bin); err == nil && st.Size() < 16<<10 {
|
|
if b, err := os.ReadFile(bin); err == nil && bytes.Contains(b, []byte("\n# asdf-plugin: ")) {
|
|
if m := asdfExec.FindSubmatch(b); m != nil {
|
|
mgr := string(m[1])
|
|
if !filepath.IsAbs(mgr) || !isFile(mgr) {
|
|
mgr = lookTool("asdf", filepath.Join(os.Getenv("ASDF_DIR"), "bin", "asdf"), filepath.Join(filepath.Dir(filepath.Dir(bin)), "bin", "asdf"))
|
|
}
|
|
if mgr != "" {
|
|
return &shim{kind: "asdf", mgr: mgr, name: name}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// miseShim is bin as mise's shim: a link to mise itself (mise's shims on a
|
|
// Mac or Linux, symlink mode on Windows), or a file in mise's shims folder
|
|
// (exe, file and hardlink modes on Windows, where mise keeps the mode in
|
|
// shims\.mode).
|
|
func miseShim(bin string) *shim {
|
|
if real, err := proc.RealPath(bin); err == nil && filepath.Base(real) != filepath.Base(bin) && isMise(real) {
|
|
return &shim{kind: "mise", mgr: real}
|
|
}
|
|
dir := filepath.Dir(bin)
|
|
if !strings.EqualFold(filepath.Base(dir), "shims") {
|
|
return nil
|
|
}
|
|
ours := strings.EqualFold(filepath.Base(filepath.Dir(dir)), "mise") || isFile(filepath.Join(dir, ".mode"))
|
|
if d := os.Getenv("MISE_DATA_DIR"); d != "" && sameFolder(filepath.Join(d, "shims"), dir) {
|
|
ours = true
|
|
}
|
|
if !ours {
|
|
return nil
|
|
}
|
|
// mise-shim.exe runs the mise on PATH; the installers' own folders
|
|
// after it, for a magpie whose PATH lacks it
|
|
home, _ := os.UserHomeDir()
|
|
mgr := lookTool("mise", filepath.Join(home, ".local", "bin", exe("mise")),
|
|
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "mise", "bin", exe("mise")))
|
|
if mgr == "" {
|
|
return nil
|
|
}
|
|
return &shim{kind: "mise", mgr: mgr}
|
|
}
|
|
|
|
func isMise(p string) bool {
|
|
b := strings.ToLower(filepath.Base(p))
|
|
return b == "mise" || b == "mise.exe"
|
|
}
|
|
|
|
func sameFolder(a, b string) bool {
|
|
a, b = filepath.Clean(a), filepath.Clean(b)
|
|
if runtime.GOOS == "windows" {
|
|
return strings.EqualFold(a, b)
|
|
}
|
|
return a == b
|
|
}
|
|
|
|
// lookTool is name on PATH, else the first of at that is a file.
|
|
func lookTool(name string, at ...string) string {
|
|
if p, err := exec.LookPath(name); err == nil {
|
|
return p
|
|
}
|
|
for _, p := range at {
|
|
if filepath.IsAbs(p) && isFile(p) {
|
|
return p
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// shimWhich asks a version manager which program its shim runs, from the
|
|
// home folder (whose config a terminal opened there has); a var so tests can
|
|
// stand in for it.
|
|
var shimWhich = func(s *shim) (string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
cmd := proc.CommandContext(ctx, s.mgr, "which", s.name)
|
|
cmd.Stdin = nil
|
|
cmd.Dir, _ = os.UserHomeDir()
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return lastLines(string(out), 1), nil
|
|
}
|
|
|
|
var shimTargets memo
|
|
|
|
// throughShim is the program bin runs when bin is a version manager's
|
|
// shim, with the shim it is; bin itself and nil when it is no shim, or
|
|
// the manager can't say.
|
|
func throughShim(bin string) (string, *shim) {
|
|
s := shimAt(bin)
|
|
if s == nil {
|
|
return bin, nil
|
|
}
|
|
p := shimTargets.get(bin, s.mgr, 10*time.Minute, time.Minute, func() (string, error) {
|
|
p, err := shimWhich(s)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if p = runnable(strings.TrimSpace(p)); p == "" {
|
|
return "", errors.New(s.kind + " which " + s.name + ": no program")
|
|
}
|
|
return p, nil
|
|
})
|
|
if p == "" {
|
|
return bin, nil
|
|
}
|
|
return p, s
|
|
}
|
|
|
|
// runnable is p as a program that runs here: on Windows, `mise which`
|
|
// names npm's extensionless sh script, beside which are its .cmd and .ps1;
|
|
// "" when there is none.
|
|
func runnable(p string) string {
|
|
if !filepath.IsAbs(p) {
|
|
return ""
|
|
}
|
|
if runtime.GOOS == "windows" && filepath.Ext(p) == "" {
|
|
for _, x := range []string{".exe", ".cmd", ".bat"} {
|
|
if isFile(p + x) {
|
|
return p + x
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
if !isFile(p) {
|
|
return ""
|
|
}
|
|
return p
|
|
}
|
|
|
|
// installedAt is how the CLI found at bin was installed, read from the
|
|
// program it is: bin itself, or what the version manager's shim at bin
|
|
// runs. A program npm puts under mise's Node needs a new shim (mise
|
|
// reshim), which mise's npm runs itself on a Mac or Linux but not on
|
|
// Windows: the update is followed by one.
|
|
func installedAt(spec cliSpec, bin string) (string, *updater) {
|
|
real, s := throughShim(bin)
|
|
u := howInstalled(spec, real)
|
|
if u != nil && s != nil && s.kind == "mise" {
|
|
u.reshim = []string{s.mgr, "reshim"}
|
|
}
|
|
return real, u
|
|
}
|