Files
magpie/internal/library/rtk_winget_windows.go
T
yetone da77e825ff library: RTK installed with winget --location into any folder is upgraded with winget (#1527, whyIMBA)
rtkUpgrader took rtk for winget's only when its path had "/winget/" in
it. winget install --location "D:\codes\tools\rtk" puts rtk.exe in that
folder, and the link winget adds in WinGet\Links points there, so the
path once resolved had no "winget" in it: no Upgrade on the card, and
"magpie library rtk upgrade" said magpie can't tell how rtk was
installed, though winget upgrade --id rtk-ai.rtk works on it.

Now, on Windows, after the brew/winget-folder/cargo checks:
- the path heuristic also looks at the path rtk was found at, not only
  the one its link leads to (WinGet\Links\rtk.exe);
- otherwise winget's own record decides: the entry winget writes for a
  portable package under HKCU/HKLM ...\Uninstall
  (rtk-ai.rtk_<source>, WinGetPackageIdentifier rtk-ai.rtk) names its
  InstallLocation; rtk is winget's when it is in that folder, so an
  rtk.exe copied elsewhere by hand isn't taken for winget's;
- with no record to read, winget list --id rtk-ai.rtk --exact is asked
  through proc with a 15s timeout. The answer is kept 10 minutes per
  rtk path, so the Library card doesn't run winget each time it is
  drawn.

Siblings checked: rtkUpgrader is the only Library upgrader with a winget
path heuristic; its brew case matches /Cellar/rtk/, which every Homebrew
prefix has. Agents' CLI updates (internal/agent/cliupdate.go) and
magpie's own update know Homebrew by its Cellar only, and have no
winget, scoop or choco path rules.

Run:
- go test -tags nogui ./internal/library (sandbox HOME). New
  rtk_winget_test.go with a fake winget (its list output from the test
  box): the reporter's folder via the record (no winget run), a record
  for another folder (and a name-prefix sibling folder) is not winget's,
  winget list asked once per 10 minutes, unlisted/empty record/hung
  winget are not winget's, the WinGet\Links link. Broke it four ways
  (record check off, link check off, folder check as a bare prefix,
  cache off) and each failed a test.
- Windows box (winget v1.29.380): winget install rtk-ai.rtk --location
  D:\tools-1527\rtk. With rtk on PATH as D:\tools-1527\rtk\rtk.exe and
  as WinGet\Links\rtk.exe: origin/main's "magpie library rtk upgrade"
  says "magpie can't tell how the rtk ... was installed"; this build
  runs winget upgrade ("winget's RTK is 0.50.0 so far; RTK 0.51.0 is
  out"). A copy of rtk.exe in another folder is still "can't tell".
  rtk uninstalled afterwards.
- gofmt -l, go vet, GOOS=linux and GOOS=windows go vet on
  internal/library.
2026-10-11 00:37:16 +08:00

47 lines
1.3 KiB
Go

package library
import (
"strings"
"golang.org/x/sys/windows/registry"
)
// rtkWingetLocations is the InstallLocation of each entry winget wrote for
// rtk-ai.rtk among the installed programs (HKCU's for a user install,
// HKLM's for a machine one): its key is rtk-ai.rtk_<source>, with
// WinGetPackageIdentifier rtk-ai.rtk. ok is false when none of them could
// be opened.
func rtkWingetLocations() (locs []string, ok bool) {
const uninstall = `Software\Microsoft\Windows\CurrentVersion\Uninstall`
for _, root := range []struct {
key registry.Key
view uint32
}{
{registry.CURRENT_USER, 0},
{registry.LOCAL_MACHINE, registry.WOW64_64KEY},
{registry.LOCAL_MACHINE, registry.WOW64_32KEY},
} {
k, err := registry.OpenKey(root.key, uninstall, registry.ENUMERATE_SUB_KEYS|registry.QUERY_VALUE|root.view)
if err != nil {
continue
}
ok = true
names, _ := k.ReadSubKeyNames(-1)
for _, n := range names {
sk, err := registry.OpenKey(k, n, registry.QUERY_VALUE|root.view)
if err != nil {
continue
}
id, _, _ := sk.GetStringValue("WinGetPackageIdentifier")
if strings.EqualFold(id, "rtk-ai.rtk") {
if loc, _, _ := sk.GetStringValue("InstallLocation"); loc != "" {
locs = append(locs, loc)
}
}
sk.Close()
}
k.Close()
}
return locs, ok
}