Files
magpie/internal/library/rtk_path_other.go
T
yetone 9848a9062a library: RTK says when rtk isn't on the PATH the agents get, and puts it there when asked (#601, Moody-Sin: magpie好像没有正确安装RTK — Pi said "RTK disabled: rtk binary not found in PATH" while the Library showed RTK working) — rtk's hooks run it by name (Pi's extension probes rtk --version and turns itself off when that fails; Claude Code's and Codex's are rtk hook …; the commands they rewrite to are rtk git status and so on), but magpie looked for rtk in more places than an agent's PATH: ~/.local/bin and ~/.cargo/bin, which the app adds to its own PATH whether the user's shell has them or not (rtk's own install.sh puts it in ~/.local/bin and only warns when that isn't on PATH), and WinGet\Links on Windows, which winget doesn't always link into. So rtk was "installed", the agents were switched on with it by its whole path, and none of them could run it. Now the PATH a terminal opened now has is read — the login shell's (proc.LoginPath, the one UserPath already asks for, kept a minute) or on Windows the registry's (moved from provider's registryPath) — and when rtk isn't in it the RTK card says so, with what Put RTK on PATH will do, and each agent that has rtk is tagged Not on PATH (不在 PATH 中). Put RTK on PATH (magpie library rtk path) links rtk into the first of ~/.local/bin, ~/bin, /opt/homebrew/bin, /usr/local/bin, linuxbrew's bin, ~/.cargo/bin that is on that PATH and writable, or on Windows adds rtk's own folder to the user's PATH in the registry (a link needs Developer Mode, and a copy is left behind when winget upgrades rtk; winget's package folder doesn't change across upgrades) and tells Explorer; with no such folder it says which folder to add to PATH in the shell profile — shell profiles are never edited. rtk is also found in winget's package folder (WinGet\Packages\rtk-ai.rtk_*), and rtk's script upgrades a linked rtk where the link points, not over the link. TestRTKOffPath and library-rtk-path.test.cjs (en, zh) fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet, go test -tags nogui -count=1 ./... under a temp HOME pass but the known TestFXInState flake. The Windows registry path is built, not run on Windows.
2026-10-03 02:38:17 +08:00

40 lines
1.0 KiB
Go

//go:build !windows
package library
import (
"os"
"path/filepath"
"slices"
)
// rtkPathDir is the folder on the user's PATH a link to rtk goes in: the
// first of those a user's tools usually are in that is on it, can be
// written to and has no rtk in it; "" when none is. link is always true:
// a link keeps up with rtk upgraded where it is.
func rtkPathDir(string) (dir string, link bool) {
on := userPath()
h := home()
for _, d := range []string{
filepath.Join(h, ".local", "bin"), filepath.Join(h, "bin"),
"/opt/homebrew/bin", "/usr/local/bin", "/home/linuxbrew/.linuxbrew/bin",
filepath.Join(h, ".cargo", "bin"),
} {
if !slices.Contains(on, d) {
continue
}
if _, err := os.Lstat(filepath.Join(d, "rtk")); err == nil {
continue // something called rtk that doesn't run
}
if st, err := os.Stat(d); err == nil && st.IsDir() && writable(d) {
return d, true
}
}
return "", true
}
// rtkOnPath links rtk into dir.
func rtkOnPath(bin, dir string, _ bool) error {
return os.Symlink(bin, filepath.Join(dir, "rtk"))
}