Files
magpie/internal/plugin/leftover_test.go
T
yetone ec97a4d7bb plugins: a dependency no plugin needs no longer fails another's install
CHENGJIAHEGOXIS on X (v0.1.4178, Windows) couldn't install GoAnyAPI from
Plugins: "agency-agents" failed with 从缓存复制到目标目录失败, and it was
on no plugin's row, so nothing could remove it.

Cause: every bun add/update/remove installs every dependency in
plugins/package.json again, and a failed `bun add` writes its package
there before exiting 1. agency-agents (a repository with no
package.json) was left by such an add; on Windows a file of its copy in
node_modules is held open, Bun fails "EBUSY: failed copying files from
cache to destination for package agency-agents", and so every later
install failed over a package nothing used.

Fix (internal/plugin/leftover.go, store.go):
- A package Bun says it couldn't install that no plugin is installed as
  (on the list, on or off, an add in flight, pi's agent) is taken out of
  package.json and node_modules and the command runs again: install()
  and runBun, so add, update, check and remove all get it.
- A failed install puts back the dependencies and bun.lock it found.
- An add whose own package went in while another listed plugin's
  failed succeeds; that plugin keeps its dependency.
- Remove and add's clean-ups drop the dependency by hand when
  bun remove fails.
- What is left from before is listed under Installed as "Not used by
  any plugin", each with a Remove (/api/plugins leftovers,
  POST /api/plugins/leftover; pmLeftovers in plugininstalled.js,
  imported when Plugins first draws), in every language.
- plugins.json there and unreadable: nothing is a leftover.
- Core script budget +51 for the one call left in plugins.js (reason in
  core_budget_test.go).

Run:
- mote-run: go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. go test ./internal/plugin (169s) and
  ./internal/provider (148s): ok.
- mica-run go test -count=1 ./internal/gui with the leftovers drawing
  moved into plugininstalled.js: ok (133s); main since touches none of
  internal/gui's core scripts, internal/plugin or plugins.go.
- win-run go test -run 'Leftover|FailedAdd|AnotherPlugin|RemoveTakes|
  BunFailed' ./internal/plugin on xiaomi-gpu: ok.
- Reproduced the EBUSY with Bun 1.4.2 on xiaomi-gpu (a file in
  node_modules/agency-agents held open, then bun add another package).
- Red on the old store.go: TestLeftoverDoesNotFailAnotherAdd,
  TestFailedAddLeavesNoDependency, TestAnotherPluginsFailureDoesNotFailAdd,
  TestRemoveTakesDependencyOutWhenBunFails.
- GUI: plugin-leftover.test.cjs 42/42 (chromium + webkit, en zh zh-TW
  ja de, 980/420px, with and without a plugin); old plugins.js: 21/21
  chromium red.
- mote-run, rebased on fdf42299e: plugin-leftover + i18n-keys 46/46;
  every other plugin*.test.cjs, old-webkit and i18n-en-no-cjk: 255 ok,
  reds only in old-webkit (#6030, #5919), plugin-move-unserved (#4392),
  plugin-option-placement (#6559) and plugin-page-foreign-error
  (#6117), each red the same on a clean origin/main fdf42299e checkout.

Not run: the full go test ./... and the whole GUI suite (mote over
its cores, mica running another session's full suite; one per machine); a real Windows
install through the magpie app.
2026-11-15 03:07:38 +08:00

219 lines
8.1 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
// agency is CHENGJIAHEGOXIS's leftover: a repository with no package.json,
// in plugins/package.json from an add that failed, on no plugin's entry.
const agency = "agency-agents"
// depsSandbox is offSandbox with plugins/package.json's dependencies and
// node_modules as given (a folder each, with a package.json for those
// with a version), and plugins.json as list.
func depsSandbox(t *testing.T, deps map[string]string, list string) {
t.Helper()
offSandbox(t)
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := setDeps(deps); err != nil {
// no package.json yet: setDeps reads one first
if err := os.WriteFile(filepath.Join(Dir(), "package.json"), []byte(`{"name":"magpie-plugins","private":true}`), 0o644); err != nil {
t.Fatal(err)
}
if err := setDeps(deps); err != nil {
t.Fatal(err)
}
}
for n, s := range deps {
at := filepath.Join(Dir(), "node_modules", filepath.FromSlash(n))
if err := os.MkdirAll(at, 0o755); err != nil {
t.Fatal(err)
}
if IsGit(s) {
// agency-agents: markdown, no package.json
if err := os.WriteFile(filepath.Join(at, "README.md"), []byte("# agents\n"), 0o644); err != nil {
t.Fatal(err)
}
continue
}
v := strings.TrimPrefix(strings.TrimPrefix(s, n), "@")
if err := os.WriteFile(filepath.Join(at, "package.json"), []byte(`{"name":"`+n+`","version":"`+v+`","main":"index.js"}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(at, "index.js"), []byte("export default async () => ({})\n"), 0o644); err != nil {
t.Fatal(err)
}
}
writeList(t, list)
}
func depsOf(t *testing.T) map[string]string {
t.Helper()
d, err := pkgDeps()
if err != nil {
t.Fatal(err)
}
return d
}
func ctxMinute(t *testing.T) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
return ctx
}
// The reporter's case: a dependency no plugin is installed as, which Bun
// can't install again (a file of its copy held open on Windows), failed
// every add after it. It is taken out, and the add goes on.
func TestLeftoverDoesNotFailAnotherAdd(t *testing.T) {
depsSandbox(t, map[string]string{agency: "github:msitarzewski/agency-agents"}, `{"plugins":[]}`)
t.Setenv("MAGPIE_FAKE_BUN_BUSY", agency)
const spec = "opencode-goany-auth@1.0.0"
if _, err := AddExact(ctxMinute(t), spec); err != nil {
t.Fatalf("add failed over a leftover no plugin needs: %v", err)
}
if !slices.ContainsFunc(Load().Plugins, func(e Entry) bool { return e.Spec == spec }) {
t.Fatalf("%s not listed: %+v", spec, Load().Plugins)
}
d := depsOf(t)
if _, in := d[agency]; in {
t.Fatalf("the leftover is still a dependency: %v", d)
}
if d["opencode-goany-auth"] != spec {
t.Fatalf("the plugin isn't a dependency: %v", d)
}
if l := Leftovers(); len(l) != 0 {
t.Fatalf("leftovers after: %+v", l)
}
}
// A failed add takes out what Bun wrote into package.json for it: else it
// is a leftover that every bun command installs again.
func TestFailedAddLeavesNoDependency(t *testing.T) {
depsSandbox(t, map[string]string{}, `{"plugins":[]}`)
t.Setenv("MAGPIE_FAKE_BUN_BUSY", "opencode-broken-auth")
if _, err := AddExact(ctxMinute(t), "opencode-broken-auth@1.0.0"); err == nil {
t.Fatal("an add Bun failed succeeded")
}
if d := depsOf(t); len(d) != 0 {
t.Fatalf("a failed add left %v in package.json", d)
}
if _, err := os.Stat(filepath.Join(Dir(), "node_modules", "opencode-broken-auth")); !os.IsNotExist(err) {
t.Fatalf("its folder stays: %v", err)
}
if len(Load().Plugins) != 0 {
t.Fatalf("listed: %+v", Load().Plugins)
}
}
// Another plugin's package Bun can't install fails no other add once that
// one is in: the other plugin keeps its dependency (it is listed), and
// its own row says what's wrong with it.
func TestAnotherPluginsFailureDoesNotFailAdd(t *testing.T) {
const other = "opencode-other-auth"
depsSandbox(t, map[string]string{other: other + "@1.0.0"}, `{"plugins":[{"spec":"`+other+`@1.0.0"}]}`)
t.Setenv("MAGPIE_FAKE_BUN_BUSY", other)
const spec = "opencode-goany-auth@1.0.0"
if _, err := AddExact(ctxMinute(t), spec); err != nil {
t.Fatalf("add failed over another plugin's package: %v", err)
}
d := depsOf(t)
if d[other] != other+"@1.0.0" {
t.Fatalf("a listed plugin's dependency was taken out: %v", d)
}
if d["opencode-goany-auth"] != spec {
t.Fatalf("the plugin added isn't a dependency: %v", d)
}
}
// Removing a plugin takes its dependency out though bun remove fails
// (which installs every other dependency again, and one of them can fail
// it): ignored, it stayed, a leftover.
func TestRemoveTakesDependencyOutWhenBunFails(t *testing.T) {
const p = "opencode-gone-auth"
depsSandbox(t, map[string]string{p: p + "@1.0.0", "opencode-kept-auth": "opencode-kept-auth@1.0.0"},
`{"plugins":[{"spec":"`+p+`@1.0.0"},{"spec":"opencode-kept-auth@1.0.0"}]}`)
t.Setenv("MAGPIE_FAKE_BUN_NOREMOVE", "1")
if err := Remove(ctxMinute(t), p); err != nil {
t.Fatal(err)
}
d := depsOf(t)
if _, in := d[p]; in {
t.Fatalf("still a dependency after Remove: %v", d)
}
if d["opencode-kept-auth"] == "" {
t.Fatalf("another plugin's dependency went too: %v", d)
}
if _, err := os.Stat(filepath.Join(Dir(), "node_modules", p)); !os.IsNotExist(err) {
t.Fatalf("its folder stays: %v", err)
}
if l := Leftovers(); len(l) != 0 {
t.Fatalf("leftovers: %+v", l)
}
}
// Leftovers lists what no plugin is installed as, and RemoveLeftover takes
// out only that.
func TestLeftoversListedAndRemoved(t *testing.T) {
const p = "opencode-kept-auth"
depsSandbox(t, map[string]string{agency: "github:msitarzewski/agency-agents", p: p + "@1.0.0"}, `{"plugins":[{"spec":"`+p+`@1.0.0","off":true}]}`)
l := Leftovers()
if len(l) != 1 || l[0].Name != agency || l[0].Spec != "github:msitarzewski/agency-agents" || l[0].Missing {
t.Fatalf("leftovers = %+v", l)
}
if err := RemoveLeftover(ctxMinute(t), p); err == nil {
t.Fatal("removed a plugin's dependency (one turned off) as a leftover")
}
// Bun can't copy it again either: the remove goes on by hand
t.Setenv("MAGPIE_FAKE_BUN_BUSY", agency)
t.Setenv("MAGPIE_FAKE_BUN_NOREMOVE", "1")
if err := RemoveLeftover(ctxMinute(t), agency); err != nil {
t.Fatal(err)
}
d := depsOf(t)
if _, in := d[agency]; in || d[p] == "" {
t.Fatalf("dependencies after = %v", d)
}
if l := Leftovers(); len(l) != 0 {
t.Fatalf("leftovers after = %+v", l)
}
}
// plugins.json there and unreadable: which dependencies are plugins is
// unknown, and none is a leftover (none taken out, none offered).
func TestNoLeftoversWhenListUnreadable(t *testing.T) {
depsSandbox(t, map[string]string{"opencode-kept-auth": "opencode-kept-auth@1.0.0"}, `{"plugins":[`)
if l := Leftovers(); len(l) != 0 {
t.Fatalf("leftovers with plugins.json unreadable = %+v", l)
}
if err := RemoveLeftover(ctxMinute(t), "opencode-kept-auth"); err == nil {
t.Fatal("removed a dependency with plugins.json unreadable")
}
if gone := dropBroken(&bunRunError{err: os.ErrInvalid, out: "EBUSY: failed copying files from cache to destination for package opencode-kept-auth"}); len(gone) != 0 {
t.Fatalf("dropped %v with plugins.json unreadable", gone)
}
}
// Bun's words for a package it couldn't install, as 1.4.2 prints them
// (colour codes and its <b>/<r> tags too).
func TestBunFailed(t *testing.T) {
for out, want := range map[string][]string{
"EBUSY: failed copying files from cache to destination for package agency-agents": {agency},
"\x1b[31merror\x1b[0m: failed copying files from cache to destination for package <b>@scope/pkg<r>\n": {"@scope/pkg"},
"EACCES: Permission denied while installing opencode-x-auth\nEBUSY: failed copying files from cache to destination for package agency-agents": {"opencode-x-auth", agency},
"error: GET https://registry.npmjs.org/x - 404": nil,
} {
if got := bunFailed(out); !slices.Equal(got, want) {
t.Errorf("bunFailed(%q) = %v, want %v", out, got, want)
}
}
}