diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index de7c7d30c..da0eee77e 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -207,7 +207,9 @@ func howInstalled(spec cliSpec, bin string) *updater { if len(spec.npm) > 0 { pkg = spec.npm[0] } - return &updater{via: "self", pkg: pkg, cmd: c, latest: spec.latest} + u := &updater{via: "self", pkg: pkg, cmd: c, latest: spec.latest} + u.path = selfPath(spec, bin, slash) + return u } } // a formula of the agent's own is Homebrew's, though a Node one keeps @@ -228,6 +230,28 @@ func howInstalled(spec cliSpec, bin string) *updater { return nil } +// selfPath is the folder to put first on PATH for a CLI's own updater, when +// a package manager installed it in a global node_modules: the updater runs +// that manager by its bare name (omo update runs npm i -g), and magpie's +// PATH can find another one first, a system node's in /usr/local/bin +// before a version manager's, whose npm then installs into a folder only +// root may change (#2021, ggbdpq). npm's global folder follows the node +// that runs it, so the folder holds that node too. +func selfPath(spec cliSpec, bin, real string) string { + for _, pkg := range spec.npm { + if u := fromNodeModules(pkg, bin, real); u != nil { + if u.via == "npm" { + return u.path + } + if u.via == "bun" || u.via == "pnpm" { + return filepath.Dir(u.cmd[0]) + } + return "" + } + } + return "" +} + // fromBrew is the updater of the CLI at real (forward slashes), which m, // brewPath's match in it, says Homebrew's formula or cask m[2] installed. func fromBrew(real string, m []string) *updater { diff --git a/internal/agent/omo_test.go b/internal/agent/omo_test.go index 7a40affb8..d9394108e 100644 --- a/internal/agent/omo_test.go +++ b/internal/agent/omo_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "runtime" "strings" "testing" @@ -134,3 +135,44 @@ func TestOmoCLI(t *testing.T) { t.Fatalf("%+v", u) } } + +// omo update runs npm by its bare name, so it is given the npm (and node) +// of the prefix OmO was installed in first on PATH: a system node's npm in +// /usr/local/bin, which magpie's PATH holds before a version manager's +// (fnm), would install into a folder only root may change (#2021, ggbdpq). +func TestOmoUpdateTakesItsOwnNpm(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("symlinks and sh scripts") + } + tmp, _ := filepath.EvalSymlinks(t.TempDir()) + ran := filepath.Join(tmp, "ran") + // the system's npm, found first on magpie's PATH + sys := filepath.Join(tmp, "usr/local/bin") + file(t, filepath.Join(sys, "npm"), "#!/bin/sh\necho system >> "+ran+"\nexit 1\n") + // fnm's node, where OmO is + prefix := filepath.Join(tmp, "fnm/node-versions/v24.21.0/installation") + file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\necho fnm >> "+ran+"\n") + file(t, filepath.Join(prefix, "bin/node"), "#!/bin/sh\n") + file(t, filepath.Join(prefix, "lib/node_modules/omo-ai/bin/omo.js"), "#!/bin/sh\n[ \"$1\" = update ] && npm i -g omo-ai@5.1.32\n") + omoBin := link(t, "../lib/node_modules/omo-ai/bin/omo.js", filepath.Join(prefix, "bin/omo")) + t.Setenv("PATH", sys+string(os.PathListSeparator)+"/usr/bin:/bin") + + u := howInstalled(cliSpecs["omo"], omoBin) + if u == nil || u.via != "self" || u.path != filepath.Join(prefix, "bin") { + t.Fatalf("%+v", u) + } + if out, err := runUpdate(t.Context(), u); err != nil { + t.Fatalf("%v: %s", err, out) + } + if b, _ := os.ReadFile(ran); string(b) != "fnm\n" { + t.Fatalf("omo update ran npm of %q, want fnm's", b) + } + // bun's OmO (bun add -g) keeps bun's folder first the same way + bunHome := filepath.Join(tmp, ".bun") + file(t, filepath.Join(bunHome, "bin/bun"), "#!/bin/sh\n") + file(t, filepath.Join(bunHome, "install/global/node_modules/omo-ai/bin/omo.js"), "") + bunOmo := link(t, "../install/global/node_modules/omo-ai/bin/omo.js", filepath.Join(bunHome, "bin/omo")) + if u := howInstalled(cliSpecs["omo"], bunOmo); u == nil || u.via != "self" || u.path != filepath.Join(bunHome, "bin") { + t.Fatalf("bun: %+v", u) + } +}