plugin: a community plugin npm doesn't have installs from the plugins repo's dist branch, so a reviewed plugin no longer waits unmerged for an owner's npm 2FA (magpie-community/plugins#84)

npm's Trusted Publishing can't create a package, so a new plugin's first
version needs an owner's 2FA, and reviewed PRs sat unmerged for it. A
trust made after 2026-09-03 also allows stage publish only, which is why
npm refused zed 0.1.7 and zen-free 0.1.7.

The plugins repo now packs every merged version onto its `dist` branch
(scripts/dist.mjs, magpie-community/plugins f5308cd): tgz/<file> and an
index.json with each version's sha512, manifest and README, with no
credentials. magpie still asks npm first. Only when `bun add` fails for a
@magpie-community package at latest or an exact version, and not because
of the user's minimumReleaseAge (that hold is kept), does installDist
fetch the tarball. It refuses the tarball unless its sha512 is the index's,
keeps it in plugins/dist and runs `bun add ./dist/<file>`. A later npm
install replaces it, and pruneDist removes tarballs package.json no longer
names. npmAsk (market version, Update, the hourly check) takes dist's
latest when npm errors or is behind, and Readme falls back to dist's. The
branch is read through jsDelivr when the China mirror switch is on, else
raw.githubusercontent.com. installMu is now held for the whole install,
fallback included.

Run at this head (on origin/main 8d2a4a7da):
- go test ./... -count=1: all ok but internal/gateway, which hit the
  600s default timeout inside a file rename. The disk was at 5.3 GB free
  and the load around 22. Rerun alone with -timeout 30m: ok in 559.7s.
- gofmt -l clean. GOOS=windows go vet ./... ok. GOOS=linux go vet ok on
  every magpie package that doesn't import Wails (its Linux files need
  cgo, which can't be cross-compiled from macOS).
- New tests, with real Bun 1.4.2 against a fake npm and a fake dist:
  TestDistInstallsAPackageNPMDoesntHave, TestDistServesAVersionNPMRefused,
  TestDistRefusesATarballNotItsIndexs and TestDistWanted.
  TestUpdateBunHoldsBackSaysWhy now has dist holding the held version too.
- Broken by hand, each went red: no fallback, no sha512 check, no prune,
  no dist version in npmAsk, a loose file regexp, and no minAge guard.
- The real dist branch serves index.json and zed's 0.1.7 tarball from
  raw (200). jsDelivr's @dist answered 200, cold in ~20s.

- Real use, sandbox HOME, live npm and live dist: Info said zed 0.1.7
  (npm has 0.1.6), and Add@0.1.7 installed it from dist, package.json
  naming ./dist/magpie-community-opencode-zed-auth-0.1.7.tgz.

Not tried: the GUI's Add on the live branch, or the China mirror path.
This commit is contained in:
yetone
2026-10-15 05:18:56 +08:00
parent 7a3fed6bb5
commit 118f4f28bf
6 changed files with 476 additions and 9 deletions
+2
View File
@@ -145,6 +145,8 @@ New users do not see an unused deprecated built-in in the Add sheet. `unusedSub`
The editor's *Update* button and *Update all* install npm's newest version; a pin the user set is kept until they update it themselves, and is then unpinned. `keepMovedCurrent` is the one path that acts without the user pressing anything, so `installPlugin` leaves what the user chose: a pinned npm version below the move's `min` is not reinstalled, and the move says it is pinned and which version it needs; a git install is reinstalled from its own spec; an unpinned npm one goes through `plugin.Upgrade` (npm's newest, which bun says why it won't install under a `minimumReleaseAge`, where `Add`'s `@latest` would quietly give an older one); a plugin the user turned off is left alone.
A `@magpie-community` package npm doesn't give is installed from the community repo's `dist` branch ([`dist.go`](../../internal/plugin/dist.go)). npm can't take a new package's first version without an owner's 2FA (Trusted Publishing sets up only a package that exists), so reviewed plugins sat unmerged waiting for someone to publish them (magpie-community/plugins#84); a Trusted Publisher made after 2026-09-03 allows stage publish only, and zed 0.1.7 and zen-free 0.1.7 were refused that way. The repo's `publish.yml` now packs every merged version onto `dist` (`scripts/dist.mjs`: `tgz/<file>` and `index.json` with each version's sha512, manifest and the README) with nobody's credentials, and npm is published to beside it. `install` asks npm first, as before; only when `bun add` fails for a community package at `latest` or an exact version (`distWanted`), and not because of the user's `minimumReleaseAge` (that hold is kept), does `installDist` fetch the tarball, refuse it unless its sha512 is the index's, keep it in `plugins/dist` and `bun add ./dist/<file>`. A later install from npm takes its place and `pruneDist` removes tarballs `package.json` no longer names. `npmAsk` (the market's version, *Update*, the hourly check) takes `dist`'s latest when npm errors or is behind, and `Readme` falls back to its README. The branch is read through the 「国内镜像」 switch's jsDelivr copy when that is on (`fetchJSONFaithful`), else raw.githubusercontent.com. `TestDistInstallsAPackageNPMDoesntHave`, `TestDistServesAVersionNPMRefused`, `TestDistRefusesATarballNotItsIndexs` and `TestUpdateBunHoldsBackSaysWhy` (with `dist` holding the held version) in [`dist_test.go`](../../internal/plugin/dist_test.go) and [`update_held_test.go`](../../internal/plugin/update_held_test.go) cover these.
A plugin on the list whose package isn't in `plugins/` is installed again from its own spec, switched off or not, by `plugin.InstallMissing`. `KeepUpdated` runs it 30 seconds after start and then hourly, before `CheckUpdates`. Nothing else did this. An update skips a plugin with no installed version, so the host said `no such plugin: …\node_modules\…` at every start until the user removed the plugin and added it again (#2002, Windows, after an update). Every bun command that changes `plugins/` runs one at a time under `installMu`: `install` (with the first `package.json` it writes), `reinstall`'s `bun update`, and `Remove`'s and `add`'s `bun remove`. At start, `KeepUpdated` and `keepMovedCurrent` both run `bun add`. Two at once each read `package.json` before either writes it, so one change was lost (reproduced with Bun 1.4.3 on Windows), and a git plugin whose dependency was dropped can't be found. `TestMissingPluginsInstalledAgain` and `TestBunCommandsOneAtATime` in [`plugin_missing_test.go`](../../internal/plugin/plugin_missing_test.go) cover both.
A plugin the user turned off stays off through every install path: `add` keeps `Off` when it replaces an existing entry, so Add, Update, Update all and Upgrade all leave it as the user left it.
+220
View File
@@ -0,0 +1,220 @@
package plugin
import (
"context"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// The community repo's dist branch: every version of each
// @magpie-community package, packed by the repo's publish workflow
// (scripts/dist.mjs there) as soon as it is merged. npm can't take a new
// package's first version without an owner's 2FA (trusted publishing sets
// up only a package that exists), so a reviewed plugin used to wait,
// unmerged, for someone to publish it by hand. magpie asks npm first, as
// before, and turns here only for what npm doesn't give: a package or
// version npm doesn't have, or npm not answering. A tarball is installed
// only when its sha512 is the one index.json names.
// distBase is where the branch is read; moved by tests. Read through the
// 「国内镜像」 switch's jsDelivr copy first when it is on (source.DoFaithful).
var distBase = "https://raw.githubusercontent.com/magpie-community/plugins/dist"
// distScope is the scope of the packages the branch has.
const distScope = "@magpie-community/"
type distVersion struct {
File string `json:"file"` // tgz/<npm pack's file name>
Integrity string `json:"integrity"` // sha512-<base64>, as npm writes it
Manifest json.RawMessage `json:"manifest"` // the version's package.json
}
type distPackage struct {
Latest string `json:"latest"`
Readme string `json:"readme"`
Versions map[string]distVersion `json:"versions"`
}
type distIndex struct {
Packages map[string]distPackage `json:"packages"`
}
var (
distMu sync.Mutex
distIdx *distIndex
distAt time.Time // when distIdx was read, or last failed to be
distOK bool // whether the last read was distIdx's
distKeep = 10 * time.Minute
distWait = time.Minute // after a read failed, before asking again
)
// distFile is a file the index may name: one tarball under tgz/, nothing
// that leaves the branch or plugins/dist.
var distFile = regexp.MustCompile(`^tgz/[A-Za-z0-9._-]+\.tgz$`)
// distPkg is what the dist branch has of the package, nil when it has
// none, the package isn't one of its scope, or the branch can't be read.
func distPkg(ctx context.Context, name string) *distPackage {
if !strings.HasPrefix(name, distScope) || distBase == "" {
return nil
}
distMu.Lock()
defer distMu.Unlock()
stale := distIdx == nil || time.Since(distAt) > distKeep
if stale && (distOK || distAt.IsZero() || time.Since(distAt) > distWait) {
b, err := fetchJSONFaithful(ctx, distBase+"/index.json", 32<<20)
var idx distIndex
if err == nil && json.Unmarshal(b, &idx) == nil {
distIdx, distOK = &idx, true
} else {
// what was read last stays, until a read works
distOK = false
}
distAt = time.Now()
}
if distIdx == nil {
return nil
}
p, ok := distIdx.Packages[name]
if !ok {
return nil
}
return &p
}
// distInfo is the package's newest version on the dist branch, as npm's
// /latest would say it, and whether there is one.
func distInfo(ctx context.Context, name string) (NPM, bool) {
d := distPkg(ctx, name)
if d == nil {
return NPM{}, false
}
v, ok := d.Versions[d.Latest]
if !ok {
return NPM{}, false
}
var l npmLatest
if json.Unmarshal(v.Manifest, &l) != nil {
return NPM{}, false
}
info := npmOf(l)
info.Version = d.Latest
return info, true
}
// distWanted is the package and version an npm spec asks for, when the
// dist branch could serve it: a @magpie-community package, at a version
// or "latest" (not a range or another tag).
func distWanted(spec string) (name, version string, ok bool) {
if IsPath(spec) || IsGit(spec) {
return "", "", false
}
name = Name(spec)
if !strings.HasPrefix(name, distScope) || !pkgName.MatchString(name) {
return "", "", false
}
version = strings.TrimPrefix(strings.TrimPrefix(spec, name), "@")
if version == "" {
version = "latest"
}
if version != "latest" && !exactVersion.MatchString(version) {
return "", "", false
}
return name, version, true
}
var exactVersion = regexp.MustCompile(`^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$`)
// installDist installs the package's version from the dist branch: its
// tarball fetched, checked against the index's sha512, kept in
// plugins/dist (bun.lock names it there, and a later bun add installs it
// again from it), and added with bun. installMu held.
func installDist(ctx context.Context, bun, name, version string) error {
d := distPkg(ctx, name)
if d == nil {
return fmt.Errorf("the community repo's dist branch has no %s, or can't be read", name)
}
if version == "latest" {
version = d.Latest
}
v, ok := d.Versions[version]
if !ok {
return fmt.Errorf("the community repo's dist branch has no %s@%s", name, version)
}
if !distFile.MatchString(v.File) {
return fmt.Errorf("the community repo's dist branch names %q for %s@%s, not a tarball of its own", v.File, name, version)
}
b, err := fetchJSONFaithful(ctx, distBase+"/"+v.File, 64<<20)
if err != nil {
return err
}
sum := sha512.Sum512(b)
if got := "sha512-" + base64.StdEncoding.EncodeToString(sum[:]); got != v.Integrity {
return fmt.Errorf("%s from the community repo's dist branch isn't the tarball its index names (sha512 %s, not %s)", v.File, got, v.Integrity)
}
dir := filepath.Join(Dir(), "dist")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
file := path.Base(v.File)
tmp, err := os.CreateTemp(dir, file+".*")
if err != nil {
return err
}
_, werr := tmp.Write(b)
if cerr := tmp.Close(); werr == nil {
werr = cerr
}
if werr == nil {
werr = os.Rename(tmp.Name(), filepath.Join(dir, file))
}
if werr != nil {
os.Remove(tmp.Name())
return werr
}
return bunAdd(ctx, bun, "./dist/"+file)
}
// pruneDist takes out the tarballs in plugins/dist that plugins/package.json
// no longer names: one an update from npm, or a newer one, took the place
// of. installMu held. One it can't tell about (package.json unreadable)
// stays.
func pruneDist() {
b, err := os.ReadFile(filepath.Join(Dir(), "package.json"))
if err != nil {
return
}
var pj struct {
Dependencies map[string]string `json:"dependencies"`
}
if json.Unmarshal(b, &pj) != nil {
return
}
named := map[string]bool{}
for _, s := range pj.Dependencies {
s = strings.TrimPrefix(s, "file:")
if rest, ok := strings.CutPrefix(filepath.ToSlash(s), "./dist/"); ok {
named[rest] = true
} else if rest, ok := strings.CutPrefix(filepath.ToSlash(s), "dist/"); ok {
named[rest] = true
}
}
ents, err := os.ReadDir(filepath.Join(Dir(), "dist"))
if err != nil {
return
}
for _, e := range ents {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".tgz") && !named[e.Name()] {
os.Remove(filepath.Join(Dir(), "dist", e.Name()))
}
}
}
+205
View File
@@ -0,0 +1,205 @@
package plugin
import (
"context"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// fakeDist serves the community repo's dist branch, as scripts/dist.mjs
// there writes it, with the versions given of each package (the last one
// named its latest); tamper serves every tarball other than the one its
// index names.
func fakeDist(t *testing.T, pkgs map[string][]string, tamper bool) {
t.Helper()
files := map[string][]byte{}
idx := map[string]any{}
for name, versions := range pkgs {
vs := map[string]any{}
for _, v := range versions {
tgz := npmTarball(t, name, v)
file := strings.ReplaceAll(strings.TrimPrefix(name, "@"), "/", "-") + "-" + v + ".tgz"
sum := sha512.Sum512(tgz)
if tamper {
tgz = npmTarball(t, name, v+"-not")
}
files["/tgz/"+file] = tgz
pj, _ := json.Marshal(map[string]string{"name": name, "version": v, "description": "from dist", "author": "dbydd"})
vs[v] = map[string]any{"file": "tgz/" + file, "integrity": "sha512-" + base64.StdEncoding.EncodeToString(sum[:]), "manifest": json.RawMessage(pj)}
}
idx[name] = map[string]any{"latest": versions[len(versions)-1], "readme": "# " + name + " from dist", "versions": vs}
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/index.json" {
json.NewEncoder(w).Encode(map[string]any{"version": 1, "packages": idx})
return
}
if b, ok := files[r.URL.Path]; ok {
w.Write(b)
return
}
http.NotFound(w, r)
}))
t.Cleanup(srv.Close)
forgetDist := func() {
distMu.Lock()
distIdx, distAt, distOK = nil, time.Time{}, false
distMu.Unlock()
}
base := distBase
distBase = srv.URL
forgetDist()
t.Cleanup(func() { distBase = base; forgetDist() })
}
// npmHasNothing is an npm registry that has no package at all: a new
// plugin, merged before anyone could publish it there.
func npmHasNothing(t *testing.T) {
fakeNPM(t, func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/downloads/") {
w.Write([]byte(`{"downloads":0}`))
return
}
http.NotFound(w, r)
})
t.Setenv("BUN_CONFIG_REGISTRY", npmRegistry+"/")
}
// A plugin merged into the community repo reaches magpie before npm has
// it: its first npm version waits for an owner's 2FA, which kept reviewed
// plugins unmerged (magpie-community/plugins#84, Open Design). The market
// shows its version and README, and adding it installs the dist branch's
// tarball with real bun.
func TestDistInstallsAPackageNPMDoesntHave(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/opencode-opendesign-auth"
npmHasNothing(t)
fakeDist(t, map[string][]string{pkg: {"0.1.3", "0.1.4"}}, false)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if info := Info(ctx, []string{pkg})[pkg]; info.Version != "0.1.4" || info.Description != "from dist" || info.Publisher != "dbydd" {
t.Fatalf("the market says %+v, want the dist branch's 0.1.4", info)
}
if p, err := Readme(ctx, pkg); err != nil || !strings.Contains(p.Readme, "from dist") {
t.Fatalf("README: %+v, %v", p, err)
}
if _, err := Add(ctx, pkg); err != nil {
t.Fatalf("Add: %v", err)
}
if v := Installed(pkg); v != "0.1.4" {
t.Fatalf("installed %q, want 0.1.4", v)
}
if _, err := os.Stat(filepath.Join(Dir(), "dist", "magpie-community-opencode-opendesign-auth-0.1.4.tgz")); err != nil {
t.Fatalf("the tarball bun.lock names isn't kept: %v", err)
}
// a pinned, older version too, and the newer one's tarball goes
if err := installAt(ctx, pkg, "0.1.3"); err != nil {
t.Fatalf("installAt 0.1.3: %v", err)
}
if v := Installed(pkg); v != "0.1.3" {
t.Fatalf("installed %q, want 0.1.3", v)
}
if left, _ := filepath.Glob(filepath.Join(Dir(), "dist", "*.tgz")); len(left) != 1 || !strings.HasSuffix(left[0], "-0.1.3.tgz") {
t.Fatalf("plugins/dist holds %v, want 0.1.3's tarball alone", left)
}
}
// A version npm refused (its Trusted Publisher allowing stage publish
// only, as zed 0.1.7 and zen-free 0.1.7 were) comes from the dist branch,
// and once npm has a newer one, from npm again, the tarball taken out.
func TestDistServesAVersionNPMRefused(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/opencode-zed-auth"
old := time.Now().Add(-72 * time.Hour)
heldRegistry(t, pkg, map[string]time.Time{"0.1.6": old}, "0.1.6")
fakeDist(t, map[string][]string{pkg: {"0.1.6", "0.1.7"}}, false)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if _, err := Add(ctx, pkg+"@0.1.6"); err != nil {
t.Fatal(err)
}
if err := save(List{Plugins: []Entry{{Spec: pkg + "@latest"}}}); err != nil {
t.Fatal(err)
}
if err := Upgrade(ctx, pkg); err != nil {
t.Fatalf("Upgrade to the dist branch's 0.1.7: %v", err)
}
if v := Installed(pkg); v != "0.1.7" {
t.Fatalf("installed %q, want 0.1.7", v)
}
heldRegistry(t, pkg, map[string]time.Time{"0.1.6": old, "0.1.8": old}, "0.1.8")
fakeDist(t, map[string][]string{pkg: {"0.1.6", "0.1.7", "0.1.8"}}, false)
if err := Upgrade(ctx, pkg); err != nil {
t.Fatalf("Upgrade to npm's 0.1.8: %v", err)
}
if v := Installed(pkg); v != "0.1.8" {
t.Fatalf("installed %q, want 0.1.8", v)
}
b, _ := os.ReadFile(filepath.Join(Dir(), "package.json"))
if !strings.Contains(string(b), `"`+pkg+`": "0.1.8"`) {
t.Fatalf("plugins/package.json doesn't name npm's 0.1.8:\n%s", b)
}
if left, _ := filepath.Glob(filepath.Join(Dir(), "dist", "*.tgz")); len(left) != 0 {
t.Fatalf("plugins/dist still holds %v", left)
}
}
// A tarball that isn't the one the index names is never installed.
func TestDistRefusesATarballNotItsIndexs(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/opencode-opendesign-auth"
npmHasNothing(t)
fakeDist(t, map[string][]string{pkg: {"0.1.4"}}, true)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
_, err := Add(ctx, pkg)
if err == nil || !strings.Contains(err.Error(), "isn't the tarball its index names") {
t.Fatalf("Add: %v, want the sha512 refused", err)
}
if v := Installed(pkg); v != "" {
t.Fatalf("installed %q", v)
}
if left, _ := filepath.Glob(filepath.Join(Dir(), "dist", "*")); len(left) != 0 {
t.Fatalf("plugins/dist holds %v", left)
}
}
func TestDistWanted(t *testing.T) {
for spec, want := range map[string]string{
"@magpie-community/opencode-zed-auth": "@magpie-community/opencode-zed-auth latest",
"@magpie-community/opencode-zed-auth@latest": "@magpie-community/opencode-zed-auth latest",
"@magpie-community/opencode-zed-auth@0.1.7": "@magpie-community/opencode-zed-auth 0.1.7",
"@magpie-community/opencode-zed-auth@^0.1.7": "",
"@magpie-community/opencode-zed-auth@next": "",
"opencode-zed-auth@0.1.7": "",
"@someone/opencode-zed-auth@0.1.7": "",
"github:magpie-community/plugins": "",
"./dist/magpie-community-opencode-zed-auth.tgz": "",
} {
name, version, ok := distWanted(spec)
got := ""
if ok {
got = name + " " + version
}
if got != want {
t.Errorf("distWanted(%q) = %q, want %q", spec, got, want)
}
}
for file, ok := range map[string]bool{"tgz/a-0.1.0.tgz": true, "tgz/../a.tgz": false, "../a.tgz": false, "tgz/a/b.tgz": false, "tgz/a.zip": false} {
if distFile.MatchString(file) != ok {
t.Errorf("distFile %q: %v, want %v", file, !ok, ok)
}
}
}
+26 -8
View File
@@ -19,6 +19,7 @@ import (
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/ghtoken"
"github.com/yetone/magpie/internal/source"
"github.com/yetone/magpie/internal/update"
)
// The plugin market: the plugins magpie suggests, from the community
@@ -434,14 +435,7 @@ func npmAsk(ctx context.Context, name string) (NPM, error) {
latestErr = err
return
}
info.Version, info.Description, info.Homepage = l.Version, l.Description, l.Homepage
info.License, _ = l.License.(string)
info.Repository = repoURL(l.Repository)
info.Publisher = person(l.Author)
info.Icon = ownIcon(l.Magpie.Icon)
if info.Publisher == "" {
info.Publisher = l.NPMUser.Name
}
info = npmOf(l)
}()
var weekly int
go func() {
@@ -456,6 +450,12 @@ func npmAsk(ctx context.Context, name string) (NPM, error) {
}
}()
wg.Wait()
// a @magpie-community package npm doesn't have yet, or has an older
// version of, is as the community repo's dist branch has it, which
// magpie installs it from (dist.go)
if d, ok := distInfo(ctx, name); ok && (latestErr != nil || update.Newer(d.Version, info.Version)) {
info, latestErr = d, nil
}
info.Weekly = weekly
if latestErr != nil {
return NPM{}, latestErr
@@ -463,6 +463,19 @@ func npmAsk(ctx context.Context, name string) (NPM, error) {
return info, nil
}
// npmOf is what a version's package.json says, as NPM has it.
func npmOf(l npmLatest) NPM {
info := NPM{Version: l.Version, Description: l.Description, Homepage: l.Homepage}
info.License, _ = l.License.(string)
info.Repository = repoURL(l.Repository)
info.Publisher = person(l.Author)
info.Icon = ownIcon(l.Magpie.Icon)
if info.Publisher == "" {
info.Publisher = l.NPMUser.Name
}
return info
}
// Hit is a package npm's search found.
type Hit struct {
Package string `json:"package"`
@@ -590,6 +603,11 @@ func Readme(ctx context.Context, name string) (Page, error) {
defer cancel()
b, err := fetchJSON(ctx, npmRegistry+"/"+npmPath(name), 32<<20)
if err != nil {
// one npm doesn't have (yet): the README the community repo's
// dist branch packed with it
if d := distPkg(ctx, name); d != nil && d.Readme != "" {
return Page{Readme: d.Readme}, nil
}
return Page{}, err
}
var d struct {
+21 -1
View File
@@ -677,8 +677,28 @@ func install(ctx context.Context, spec string) error {
return err
}
}
defer installMu.Unlock()
err = bunAdd(ctx, bun, spec)
// a @magpie-community package or version npm doesn't have (a new
// one waits for an owner to publish it there) comes from the
// community repo's dist branch; one Bun's minimumReleaseAge holds
// back stays held, as the user's bunfig asks
if name, version, ok := distWanted(spec); ok && err != nil && !minAge.MatchString(err.Error()) {
if derr := installDist(ctx, bun, name, version); derr != nil {
return fmt.Errorf("%w (and from the community repo's dist branch: %v)", err, derr)
}
err = nil
}
if err == nil {
pruneDist()
}
return err
}
// bunAdd adds spec to plugins/ with bun, its scripts left unrun.
// installMu held.
func bunAdd(ctx context.Context, bun, spec string) error {
out, err := bunCommand(ctx, bun, Dir(), "add", "--ignore-scripts", spec).CombinedOutput()
installMu.Unlock()
if err != nil {
if hint := missingLibs(string(out)); hint != "" {
return fmt.Errorf("bun add %s: %s: %w", spec, hint, err)
+2
View File
@@ -97,6 +97,8 @@ func TestUpdateBunHoldsBackSaysWhy(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/opencode-factory-auth"
heldRegistry(t, pkg, map[string]time.Time{"0.1.18": time.Now().Add(-72 * time.Hour), "0.1.21": time.Now().Add(-time.Hour)}, "0.1.21")
// the dist branch has 0.1.21 too: the user's hold is kept, not stepped round
fakeDist(t, map[string][]string{pkg: {"0.1.18", "0.1.21"}}, false)
home, _ := os.UserHomeDir()
bunfig := []byte("[install]\nminimumReleaseAge = 172800\n")
for _, p := range []string{filepath.Join(home, ".bunfig.toml"), filepath.Join(os.Getenv("XDG_CONFIG_HOME"), ".bunfig.toml")} {