On Windows the official Node.js installer leaves npm's global prefix at C:\Program Files\nodejs, which only an administrator can change. magpie runs as the user, and writable() on Windows always said true, so the Agents page offered the update and npm failed with EPERM (mkdir ...\node_modules\@scope), with nothing saying why. The same user then reported RTK and other actions needing elevation all failing, and asked for a setting to run magpie as administrator. There is no such setting: an elevated magpie would write the agents' configs as the administrator, couldn't start with Windows without a UAC prompt at each login, and widens what can go wrong. Instead each action that needs an administrator asks Windows for one, alone. - agent: writable() on Windows makes and removes a file in the folder (update.Writable) instead of always saying yes. An npm CLI in a folder the user can't write in is still offered on Windows, marked Admin, and its update runs through update.RunElevated, never as the user. Elsewhere (/usr/lib, would want sudo) nothing is offered, as before. - update: RunElevated (Windows) runs the command through ShellExecuteEx "runas" on a .cmd script that sets the env that differs from an elevated process's own plus PATH, cds to the folder, and sends output to a log read back; the exit code comes from GetExitCodeProcess (a non-zero one is update.ExitError, for its code). A declined prompt is ErrCanceled; no prompt to be had is the new ErrNoAdmin. Other systems: CanRunElevated false. - library: RTK installed by winget for every user (under Program Files) is upgraded through the same prompt when only an administrator can change rtk's folder; the card's Upgrade title says Windows asks first. Install, and an upgrade whose folder looked writable, run winget as the user and go through the prompt only when winget answers 0x8A150019 (command requires admin). winget's codes from the elevated run are read as before (0x8A15002B: no newer RTK, no failure). - A declined or impossible prompt is agent.AdminError (Via npm or winget); the GUI answers it with code needs_admin and says, in the reader's language, what needs an administrator and what to do: try again and allow it, or run the command in a terminal opened as administrator, and for npm also moving npm's prefix (npm config set prefix "%APPDATA%\npm") and reinstalling. - Install another agent: its npm commands note when npm's prefix (npm prefix -g, memoized) is such a folder. - Checked, not changed: autostart uses HKCU's Run key, no admin needed; magpie never runs npm uninstall; plugins install into magpie's own folder with bun; agent CLIs update only on a click; RTK's PATH fix writes HKCU's Environment and rtk init -g the user's own agent folders; the library's only other exec is the MCP server check. Run: - go test -tags nogui ./internal/agent ./internal/update ./internal/library (and the full ./... under the lock). TestNPMUpdateNeedsAdmin fails with the admin branch removed and with runAdmin bypassed; TestRTKUpgradeAsksForAdmin and TestRTKInstallRetriesAsAdmin fail with runRTKInstaller never elevating (checked by hand). - gofmt -l; go vet (darwin), GOOS=linux and GOOS=windows go vet. - GUI, Chromium and WebKit: cli-update-admin (en, zh), library-rtk-admin (en, zh exact; ja, de, zh-TW translated, no npm advice; fails with the old Upgrade title), library-rtk-upgrade/-path/-nohook/-gain, gui-ja, gui-de, gui-zh-tw, cli-update, i18n-keys. - Windows box (10.0.0.132): TestWritableSeesDeniedFolder passes, and fails on the old writable(). A portable Node 22 with Program Files' ACL (Users RX, Administrators F): as a non-elevated token (runas /trustlevel:0x20000) npm install -g failed with the reporter's EPERM, Writable said false, and dsh's CLI() said Via npm, Admin true; elevated it said Admin false. RunElevated from the elevated ssh session installed @deepseek-ai/dsh into that folder for real, and a missing version came back as exit status 1 with npm's ETARGET output. The library's RTK tests ran there too (TestRTKInstallRetriesAsAdmin passes; the shell-fake ones skip on Windows). - Not tried: the UAC prompt itself, allowing or declining it, as nobody sits at the box; the ErrNoAdmin path on a real system; a real machine-scope winget RTK (none is installed on the box).
86 lines
3.1 KiB
Go
86 lines
3.1 KiB
Go
package library
|
|
|
|
// RTK installed for every user of a Windows computer (winget --scope
|
|
// machine, under C:\Program Files\WinGet) is one only an administrator can
|
|
// upgrade, and magpie runs as the user (新v学员 on Discord: "RTK and
|
|
// other actions that need elevation all fail"). magpie isn't run as
|
|
// administrator for it: an elevated magpie would write the agents' files
|
|
// as the administrator, and couldn't start with Windows without asking each
|
|
// time. Instead the one winget command that needs it goes through Windows'
|
|
// administrator prompt (update.RunElevated), as an agent's npm update in
|
|
// Program Files does; a prompt declined or not to be had says so, and what
|
|
// to do, as agent.AdminError.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// wingetNeedsAdmin is winget saying the command needs an administrator
|
|
// (APPINSTALLER_CLI_ERROR_COMMAND_REQUIRES_ADMIN).
|
|
const wingetNeedsAdmin uint32 = 0x8A150019
|
|
|
|
var (
|
|
// rtkElevates is whether an installer can be run through an
|
|
// administrator prompt here (Windows); vars so tests can on any system
|
|
rtkElevates = update.CanRunElevated()
|
|
runElevated = update.RunElevated
|
|
rtkWritable = update.Writable
|
|
// runPlain is runInstaller: winget's HRESULTs are past what a test's
|
|
// sh can exit with
|
|
runPlain = runInstaller
|
|
)
|
|
|
|
// rtkAdminFolder is the folder of the rtk at bin when winget is to upgrade
|
|
// it and only an administrator can change that folder; "" otherwise.
|
|
func rtkAdminFolder(c []string, bin string) string {
|
|
if !rtkElevates || len(c) == 0 || c[0] != "winget" {
|
|
return ""
|
|
}
|
|
real, err := filepath.EvalSymlinks(bin)
|
|
if err != nil {
|
|
real = bin
|
|
}
|
|
if d := filepath.Dir(real); !rtkWritable(d) {
|
|
return d
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// runRTKInstaller runs installer c as runInstaller does, but winget's
|
|
// through Windows' administrator prompt when admin, the folder RTK is in,
|
|
// needs one, or when winget answers that it does.
|
|
func runRTKInstaller(c []string, timeout time.Duration, admin string, env ...string) (string, error) {
|
|
elevates := rtkElevates && c[0] == "winget"
|
|
if admin == "" || !elevates {
|
|
out, err := runPlain(c, timeout, env...)
|
|
var ie *installError
|
|
if !elevates || !errors.As(err, &ie) || uint32(ie.code) != wingetNeedsAdmin {
|
|
return out, err
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
b, err := runElevated(ctx, home(), append(append(os.Environ(), "NONINTERACTIVE=1"), env...), c...)
|
|
out := string(b)
|
|
var ee *update.ExitError
|
|
switch {
|
|
case err == nil:
|
|
return out, nil
|
|
case errors.Is(err, update.ErrCanceled) || errors.Is(err, update.ErrNoAdmin):
|
|
return out, &agent.AdminError{Agent: "RTK", Prefix: admin, Command: shown(c), Via: "winget", Canceled: errors.Is(err, update.ErrCanceled), Err: err}
|
|
case errors.Is(ctx.Err(), context.DeadlineExceeded):
|
|
return out, installFailed(c, out, -1, fmt.Errorf("didn't finish in %s", timeout))
|
|
case errors.As(err, &ee):
|
|
return out, installFailed(c, out, int(ee.Code), err)
|
|
}
|
|
return out, installFailed(c, out, -1, fmt.Errorf("couldn't run: %w", err))
|
|
}
|