Part 1 of 2, pruneDist (coffemon's first commit): pruneDist took out every tarball in plugins/dist that plugins/package.json doesn't name, as encoding/json reads it. Bun installs a package from the tarball bun.lock's packages has it at, and that can be one package.json has moved on from: gkd2323c's lock (#3622, Windows, Bun 1.4.4) had autoclaw, codearts and cloudflare-workers-ai at the tarballs before the ones package.json names, and those were gone from plugins/dist, where nothing but pruneDist takes a file out. With Bun's own copy of such a tarball, every bun command that changes plugins/ puts the older version back; with none, every add and removal, of any plugin, fails with "ENOENT extracting tarball from <that package>" (part 2 has the likely why there was none). Now a tarball the lock's packages has a package at stays, beside the ones package.json names. Kept, it is what Bun installs: a package whose lock is behind package.json stays at the older version without a word, where4e5b2ad8, once Bun had no copy, failed every command with ENOENT. part 2 sends such a lock back to package.json before each bun command when package.json has the package at a dist tarball too; with package.json at npm it stays behind (#3720). package.json is read as text, every dist/<file> in it counting, where encoding/json keeps one value of a key named twice: Bun 1.4.0 adds a tarball's package a second time instead of replacing it, the new tarball first, which Bun reads, and encoding/json reads the second, so4e5b2ad8took out the tarball Bun installs from. No magpie runs plugins on that Bun unless MAGPIE_BUN names it (BunVersion has been 1.4.2 since 10-03, before plugins/dist was), so that is how a decoded package.json loses it, not a path a user is known to have taken. A tarball only the lock's workspaces names goes: Bun 1.4.2 and 1.4.3 leave the one an add replaced there until their next command, and nothing installs from it. With no bun.lock, package.json alone decides, as before; with one that can't be read, nothing goes, as with a package.json that can't be read. This keeps the tarballs a lock has from going, and mends no lock: one behind package.json's dist tarball, the reporter's or one that falls behind later, is sent back by part 2, its tarball there or gone. What was run (tree 8f285445, on origin/main4e5b2ad8, go1.26.8, linux/amd64): - TestDistTarballTheLockNamesStays (new): the reporter's package.json and lock, built around the lines #3622 quotes; the package.json real Bun 1.4.2 writes for `bun add ./dist/<0.2.6>` over 0.2.1, and the one Bun 1.4.0 writes (the package named twice), each with the lock both write, and the Bun 1.4.0 one with no lock; no lock; a lock that can't be read (a folder in its place). The tarballs the lock's packages or package.json name stay, the one only the lock's workspaces name goes, and one nothing names goes; with no lock package.json alone decides, and with an unreadable one nothing goes. With the change the TestDist tests are ok on each Bun. Broken by hand, each put back after, with Bun 1.4.2, 1.4.3 and 1.4.4 (outside graftcp, the proxy wrapper this box runs commands in, umask 077, logs kept): - 4e5b2ad8's pruneDist: the reporter's, both named-twice and the unreadable-lock cases red on each ("<tarball> taken out"); - package.json decoded as4e5b2ad8did, the lock still read: the named-twice case with no lock red on each ("<0.2.6> taken out"); - the whole lock read for tarballs instead of its packages: the workspaces case red on each ("0.2.1 kept"), and TestDistInstallsAPackageNPMDoesntHave red on 1.4.2 and 1.4.3 (0.1.4's tarball stays after 0.1.3 is pinned: those two leave it in the workspaces). - internal/plugin, -tags nogui, with Bun 1.4.4, the one magpie runs now: ok but TestPluginSOCKSProxy, red the same at origin/main7a60e78c(#2207, open, filed for 1.4.3); TestFlowBigReplyProd and TestRenewOtherHost skip by their own condition. - gofmt -l lists nothing; GOOS=linux, windows and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build without the tag on linux: exit 1 (no GTK, WebKitGTK or fontconfig headers on this box). - Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 64 ok, internal/plugin among them, and 8 red, none in what this changes: - agent TestCodexDriftReached and TestCodexBehindReverseProxy: #3398; TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git, #3641. - davsync TestSyncTeraCloudStillSeesOtherWrites: #3420. - edit TestWriteAtomicReadOnlyHardLink: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: this box's millions of open fds, #3649. - provider: "fatal error: close of synctest channel from outside bubble" in TestUnreadLoginsWriteErrorsKeepCredentials, main's CI red, #3632; the package's tests after it didn't run. - sessions TestAgyTitleLater: a same-size write in the same mtime tick, 2 of 200 on main alone, #3708. - testenv TestProgram: this box's umask 077, #3640. Not tried: Windows, where the reporter is (CI's Windows job runs none of these tests: it compiles them and runs a named few). package.json and the lock are read for the forward-slash form magpie hands Bun ("./dist/<file>"), which the reporter's Bun 1.4.4 on Windows kept in both. A tarball they name only with backslashes goes, where4e5b2ad8on Windows kept one package.json named so (it read package.json through filepath.ToSlash). Part 2 of 2, mendDist (coffemon's second commit): The commit before keeps the tarballs bun.lock has packages at, but a lock behind package.json stays behind: gkd2323c's (#3622, Windows, Bun 1.4.4) had autoclaw, codearts and cloudflare-workers-ai at tarballs pruneDist had taken, while package.json and the lock's workspaces had moved on. Bun installs what the lock has, at every command that changes plugins/. From that tarball, kept now, or from its own copy of it, it put the older version back without a word; with neither, every install, update and removal, of any plugin, failed with "ENOENT extracting tarball from <that package>". Bun keeps its copy of a local tarball in its install cache as @T@<16hex>@@@1 on 1.4.2 and 1.4.3, and as @T@sha512-<32hex>@@@1, the start of the tarball's sha512, on 1.4.4, so a Bun moved to 1.4.4 has no copy of one an older Bun made (magpie takes a Bun release two days after it is out, bunSettle: 1.4.4, out on 10-22, from 10-24). pruneInstallCache leaves these copies alone (no package name before their first @). On Linux, such a lock and copies made by 1.4.2 or by 1.4.3 give an add on 1.4.4 the reporter's error, for both packages locked at gone tarballs; the same Bun's add succeeds and puts both back at their older versions. Now mendDist runs before each bun command that changes plugins/ (in runBun and install, under installMu): the packages the lock has at a dist tarball package.json doesn't name, while package.json has them at one plugins/dist holds, get one `bun update --ignore-scripts` of them all, which resolves them from package.json again. It comes after a9242eb6's mendPackageJSON, which cuts what follows package.json's object (plugins#405): mendDist decodes package.json, and with that tail there it would return without a mend, leaving the lock behind for the command. A package.json that names the lock's tarball too, as Bun 1.4.0's names a package twice, isn't behind. Nothing is asked of the registry: no package on dist has a dependency (its index.json, 42 packages, on 10-26). `bun add` of them doesn't do it on Bun 1.4.2 and 1.4.3, which keep what the lock has, and with no copy of them, an update of one package alone fails on the others still locked at gone tarballs. Each mend is logged with the packages it sent back, so a lock that falls behind again, which nothing here explains, leaves a line saying when. What it can't mend is left to the command it comes before, as before: package.json's tarball gone too, or a package.json or bun.lock it can't read. A lock behind package.json across npm and plugins/dist it leaves alone, and Bun installs the lock's version (#3720). A lock with nothing stale costs reading the two files, decoding package.json and a scan of the lock; a tarball is looked at only where the lock and package.json name different ones. A lock already behind, the reporter's among them, is sent back at the next bun command magpie runs that changes plugins/, as package.json's tarball is in plugins/dist: pruneDist never takes one package.json names. What was run (tree 5ae0b4a1, on origin/main4e5b2ad8, go1.26.8, linux/amd64): - TestDistLockBehindPackageJSONIsMended (new), with real Bun: autoclaw and codearts installed from plugins/dist at 0.2.1 and 0.2.0, then at 0.2.6 and 0.2.1 (the reporter's), and the lock's packages given back the entries Bun wrote for the first ones; with Bun's copies kept, with them gone, with the older tarballs put back in plugins/dist, as the commit before keeps them, and with the copies gone and plugins#405's tail after package.json's object. From each state an unrelated plugin's add (zcode) and then its removal succeed and leave both packages at package.json's versions, in node_modules and alone in the lock, and package.json's specs for both as they were; the add's prune takes the older tarballs. With the change the TestDist tests are ok on each Bun, and the two new ones at -count=10 on 1.4.2 and 1.4.4. Broken by hand, each put back after, with Bun 1.4.2, 1.4.3 and 1.4.4 (outside graftcp, the proxy wrapper this box runs commands in, umask 077, logs kept): - no mendDist: every case red on each, the older versions back with the copies or the older tarballs kept (`is at "0.2.1", want 0.2.6`), the add failing with "ENOENT extracting tarball from" both packages with the copies gone; - mendDist before install's add only: every case red at the removal on each, the older versions back, or with the copies gone package.json still naming zcode after Remove; - one `bun update` a package: both copies-gone cases red on each, the add failing with "ENOENT extracting tarball from" both packages, as with no mendDist (each update alone fails on the other's gone tarball); - `bun add` of package.json's tarballs instead of `bun update`: every case red on 1.4.2 and 1.4.3, ok on 1.4.4; - the trigger as first written (the lock's tarball gone): the tarballs-kept case red on each, the older versions back (`is at "0.2.1", want 0.2.6`); - mendDist before package.json's tail is cut: in install, the tail case red on each at the add, "ENOENT extracting tarball from" both packages; in runBun, red on each at the removal, package.json still naming zcode after Remove. - internal/plugin, -tags nogui, with Bun 1.4.4, the one magpie runs now: ok but TestPluginSOCKSProxy, red the same at origin/main7a60e78c(#2207, open, filed for 1.4.3); TestFlowBigReplyProd and TestRenewOtherHost skip by their own condition. - gofmt -l lists nothing; GOOS=linux, windows and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build without the tag on linux: exit 1 (no GTK, WebKitGTK or fontconfig headers on this box). - Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 65 ok, internal/plugin among them, and 7 red, none in what this changes: - agent TestCodexDriftReached: #3398; TestInstalledVersion, TestHowInstalled, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git, #3641. - davsync TestSync and TestSyncTeraCloudStillSeesOtherWrites: #3420. - edit TestWriteAtomicReadOnlyHardLink: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: this box's millions of open fds, #3649. - provider: "fatal error: close of synctest channel from outside bubble" in TestUnreadLoginsWriteErrorsKeepCredentials, main's CI red, #3632; the package's tests after it didn't run. - testenv TestProgram: this box's umask 077, #3640. Not tried: Windows, where the reporter is (CI's Windows job runs none of these tests: it compiles them and runs a named few); the reporter's own plugins/ folder; the step that left their lock's packages at the tarballs before while package.json and the workspaces moved on. On Linux each Bun from 1.4.2 to 1.4.4 writes the packages at the tarball an add names, so the test makes that state by hand. Review (macOS arm64, go1.26.8, squashed into one commit per issue, on origin/main1cbdda25b), plus the class plugins#405 belongs to, a file here written over a longer one without truncating: - bun.lock: real Bun 1.4.4 reads the lock's first object and goes on past bytes after it (a lock with an older lock's packages appended: `bun install` checked 1 install, no changes, tail left as it was). So a tail doesn't stop Bun, but pruneDist's and mendDist's regexp read the whole file: the tail's older tarball was kept, and sent to bun update before every command. Both now read lockHead, the first object, braces and quotes inside strings skipped. TestDistLockTailIsntRead (bun update run for a lock whose object is behind, not for one whose tail is, nor one with "}\"{]" in a string) and a TestDistTarballTheLockNamesStays case (the tail's tarball goes). Broken by hand: lockHead returning the whole file turned the tail case and both tail subtests red ("0.2.1 kept", "bun update run: true"); escapes not skipped turned the braces-in-strings subtest red. - package.json: every magpie write of it is temp+rename (mendPackageJSON sincea9242eb68) but the first one install makes, an os.WriteFile; that goes through edit.WriteAtomic now. bun's own writes are bun's. - TestDist* ok on Bun 1.4.2, 1.4.3 and 1.4.4; broken by hand with 1.4.4: mendDist's calls removed turned TestDistLockBehindPackageJSONIsMended red, and the old pruneDist turned TestDistTarballTheLockNamesStays red ("taken out"). - internal/plugin whole package ok (144s); gofmt clean; vet ok for linux nogui, windows, darwin nogui. Not tried: Windows, where both reports come from.
588 lines
22 KiB
Go
588 lines
22 KiB
Go
package plugin
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"maps"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"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)
|
|
}
|
|
}
|
|
|
|
// pruneDist keeps a tarball plugins/package.json names or bun.lock's
|
|
// packages has a package at, read as Bun writes them. gkd2323c's lock
|
|
// (#3622) had three packages at the tarballs before the ones package.json
|
|
// names, and c158b043 took those out; a package.json Bun 1.4.0 wrote names
|
|
// autoclaw twice, the new tarball first, which Bun reads, and c158b043 kept
|
|
// only the second. The tarball Bun 1.4.2 leaves in the lock's workspaces
|
|
// alone goes, as does one nothing names; with no bun.lock, package.json
|
|
// alone decides; with one that can't be read, nothing goes.
|
|
func TestDistTarballTheLockNamesStays(t *testing.T) {
|
|
const (
|
|
autoclaw = "magpie-community-opencode-autoclaw-auth-0.2.6.tgz"
|
|
autoclawWas = "magpie-community-opencode-autoclaw-auth-0.2.1.tgz"
|
|
cloudflare = "magpie-community-opencode-cloudflare-workers-ai-auth-0.0.3.tgz"
|
|
cfWas = "magpie-community-opencode-cloudflare-workers-ai-auth-0.0.2.tgz"
|
|
codearts = "magpie-community-opencode-codearts-auth-0.2.1.tgz"
|
|
codeartsWas = "magpie-community-opencode-codearts-auth-0.2.0.tgz"
|
|
unnamed = "magpie-community-opencode-autoclaw-auth-0.2.5.tgz"
|
|
)
|
|
// gkd2323c's package.json and lock: the lines #3622 quotes, in the
|
|
// files Bun writes, the lock's workspaces as package.json (the issue
|
|
// says they were)
|
|
reported := `{
|
|
"name": "magpie-plugins",
|
|
"private": true,
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz",
|
|
"@magpie-community/opencode-cloudflare-workers-ai-auth": "./dist/magpie-community-opencode-cloudflare-workers-ai-auth-0.0.3.tgz",
|
|
"@magpie-community/opencode-codearts-auth": "./dist/magpie-community-opencode-codearts-auth-0.2.1.tgz"
|
|
}
|
|
}
|
|
`
|
|
reportedLock := `{
|
|
"lockfileVersion": 2,
|
|
"configVersion": 1,
|
|
"workspaces": {
|
|
"": {
|
|
"name": "magpie-plugins",
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz",
|
|
"@magpie-community/opencode-cloudflare-workers-ai-auth": "./dist/magpie-community-opencode-cloudflare-workers-ai-auth-0.0.3.tgz",
|
|
"@magpie-community/opencode-codearts-auth": "./dist/magpie-community-opencode-codearts-auth-0.2.1.tgz",
|
|
},
|
|
},
|
|
},
|
|
"packages": {
|
|
"@magpie-community/opencode-autoclaw-auth": ["@magpie-community/opencode-autoclaw-auth@./dist/magpie-community-opencode-autoclaw-auth-0.2.1.tgz", {}, "sha512-..."],
|
|
|
|
"@magpie-community/opencode-cloudflare-workers-ai-auth": ["@magpie-community/opencode-cloudflare-workers-ai-auth@./dist/magpie-community-opencode-cloudflare-workers-ai-auth-0.0.2.tgz", {}, "sha512-..."],
|
|
|
|
"@magpie-community/opencode-codearts-auth": ["@magpie-community/opencode-codearts-auth@./dist/magpie-community-opencode-codearts-auth-0.2.0.tgz", {}, "sha512-..."],
|
|
}
|
|
}
|
|
`
|
|
// `bun add ./dist/<0.2.6>` over 0.2.1: Bun 1.4.2 writes package.json
|
|
// so, Bun 1.4.0 with the package twice, and both write the lock below
|
|
once := `{
|
|
"name": "magpie-plugins",
|
|
"private": true,
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz"
|
|
}
|
|
}
|
|
`
|
|
twice := `{
|
|
"name": "magpie-plugins",
|
|
"private": true,
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz",
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.1.tgz"
|
|
}
|
|
}
|
|
`
|
|
updatedLock := `{
|
|
"lockfileVersion": 2,
|
|
"configVersion": 1,
|
|
"workspaces": {
|
|
"": {
|
|
"name": "magpie-plugins",
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz",
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.1.tgz",
|
|
},
|
|
},
|
|
},
|
|
"packages": {
|
|
"@magpie-community/opencode-autoclaw-auth": ["@magpie-community/opencode-autoclaw-auth@./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz", {}, "sha512-63rE5gosmPjiHJlJZU3SImX8OBOk8pAKQHX5OAa5Ke2vaSlhVgDzmyPiiiXOJ2fCHMs8ty5cYoMhcOzeaDLhvA=="],
|
|
}
|
|
}
|
|
`
|
|
for _, c := range []struct {
|
|
name, pj, lock string // lock: "" for none, "unreadable" for a folder in its place
|
|
stay, gone []string
|
|
}{
|
|
{"the lock at the tarballs before", reported, reportedLock, []string{autoclaw, autoclawWas, cloudflare, cfWas, codearts, codeartsWas}, []string{unnamed}},
|
|
{"package.json naming a package twice", twice, updatedLock, []string{autoclaw, autoclawWas}, []string{unnamed}},
|
|
{"package.json naming a package twice, no lock", twice, "", []string{autoclaw, autoclawWas}, []string{unnamed}},
|
|
{"the lock's workspaces alone naming one", once, updatedLock, []string{autoclaw}, []string{autoclawWas, unnamed}},
|
|
// a shorter lock written over a longer one without truncating
|
|
// (plugins#405's package.json): Bun reads the first object alone
|
|
{"the lock with an older lock's tail past it", once, updatedLock + reportedLock[strings.Index(reportedLock, ` "@magpie-community/opencode-autoclaw-auth": [`):], []string{autoclaw}, []string{autoclawWas, unnamed}},
|
|
{"no lock", reported, "", []string{autoclaw, cloudflare, codearts}, []string{autoclawWas, cfWas, codeartsWas, unnamed}},
|
|
{"a lock that can't be read", reported, "unreadable", []string{autoclaw, autoclawWas, cloudflare, cfWas, codearts, codeartsWas, unnamed}, nil},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("HOME", dir)
|
|
t.Setenv("USERPROFILE", dir)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
|
|
if err := os.MkdirAll(filepath.Join(Dir(), "dist"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(Dir(), "package.json"), []byte(c.pj), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
switch c.lock {
|
|
case "":
|
|
case "unreadable":
|
|
if err := os.Mkdir(filepath.Join(Dir(), "bun.lock"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
default:
|
|
if err := os.WriteFile(filepath.Join(Dir(), "bun.lock"), []byte(c.lock), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, f := range append(slices.Clone(c.stay), c.gone...) {
|
|
if err := os.WriteFile(filepath.Join(Dir(), "dist", f), []byte("tgz"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
pruneDist()
|
|
for _, f := range c.stay {
|
|
if _, err := os.Stat(filepath.Join(Dir(), "dist", f)); err != nil {
|
|
t.Errorf("%s taken out: %v", f, err)
|
|
}
|
|
}
|
|
for _, f := range c.gone {
|
|
if _, err := os.Stat(filepath.Join(Dir(), "dist", f)); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Errorf("%s kept (%v)", f, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A bun.lock that has packages at tarballs package.json has moved on from,
|
|
// as gkd2323c's had autoclaw, codearts and cloudflare-workers-ai (#3622),
|
|
// is sent back to package.json before Bun runs. Bun installs what the lock
|
|
// has, at every add and removal of any plugin: from the older tarball,
|
|
// which pruneDist keeps now, or from its copy of one gone from
|
|
// plugins/dist, it put the older version back; with neither it failed each
|
|
// with "ENOENT extracting tarball". A Bun moved to 1.4.4 has no copy of
|
|
// those 1.4.2 and 1.4.3 made, as 1.4.4 keys its copies by the tarball's
|
|
// sha512 and they didn't. The state is made with real bun: the lock's
|
|
// packages given back the entries Bun wrote for the tarballs before, which
|
|
// pruneDist took when the newer were added, or which are put back. The
|
|
// packages stay at the versions package.json names, and package.json as it
|
|
// was. A package.json with bytes after its object (plugins#405) is cut
|
|
// first, as mendDist reads it.
|
|
func TestDistLockBehindPackageJSONIsMended(t *testing.T) {
|
|
const (
|
|
autoclaw = "@magpie-community/opencode-autoclaw-auth"
|
|
codearts = "@magpie-community/opencode-codearts-auth"
|
|
zcode = "@magpie-community/opencode-zcode-auth"
|
|
)
|
|
tarball := func(pkg, v string) string {
|
|
return strings.ReplaceAll(strings.TrimPrefix(pkg, "@"), "/", "-") + "-" + v + ".tgz"
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
copiesGone, tarballsKept, tail bool
|
|
}{
|
|
{"Bun's copies of the tarballs kept", false, false, false},
|
|
{"Bun's copies of the tarballs gone", true, false, false},
|
|
{"the tarballs kept", false, true, false},
|
|
{"Bun's copies gone, package.json with a tail", true, false, true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
sandbox(t)
|
|
npmHasNothing(t)
|
|
fakeDist(t, map[string][]string{autoclaw: {"0.2.1", "0.2.6"}, codearts: {"0.2.0", "0.2.1"}, zcode: {"0.1.30"}}, false)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
lockPath := filepath.Join(Dir(), "bun.lock")
|
|
readLock := func() []byte {
|
|
t.Helper()
|
|
b, err := os.ReadFile(lockPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
// the package's entry in the lock's packages, the line Bun writes
|
|
entry := func(lock []byte, pkg string) []byte {
|
|
t.Helper()
|
|
m := regexp.MustCompile(`(?m)^[ \t]*"` + regexp.QuoteMeta(pkg) + `": \[.*$`).Find(lock)
|
|
if m == nil {
|
|
t.Fatalf("bun.lock's packages have no %s:\n%s", pkg, lock)
|
|
}
|
|
return m
|
|
}
|
|
specs := func() map[string]string {
|
|
t.Helper()
|
|
b, err := os.ReadFile(filepath.Join(Dir(), "package.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var pj struct{ Dependencies map[string]string }
|
|
if err := json.Unmarshal(b, &pj); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return map[string]string{autoclaw: pj.Dependencies[autoclaw], codearts: pj.Dependencies[codearts]}
|
|
}
|
|
before := map[string]string{autoclaw: "0.2.1", codearts: "0.2.0"}
|
|
want := map[string]string{autoclaw: "0.2.6", codearts: "0.2.1"}
|
|
was, wasTgz := map[string][]byte{}, map[string][]byte{}
|
|
for _, at := range []map[string]string{before, want} {
|
|
for _, pkg := range []string{autoclaw, codearts} {
|
|
if err := installAt(ctx, pkg, at[pkg]); err != nil {
|
|
t.Fatalf("installAt %s %s: %v", pkg, at[pkg], err)
|
|
}
|
|
}
|
|
if len(was) == 0 {
|
|
for _, pkg := range []string{autoclaw, codearts} {
|
|
was[pkg] = entry(readLock(), pkg)
|
|
b, err := os.ReadFile(filepath.Join(Dir(), "dist", tarball(pkg, before[pkg])))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wasTgz[pkg] = b
|
|
}
|
|
}
|
|
}
|
|
pj := specs()
|
|
breakLock := func() {
|
|
t.Helper()
|
|
b := readLock()
|
|
for pkg, old := range was {
|
|
f := filepath.Join(Dir(), "dist", tarball(pkg, before[pkg]))
|
|
if _, err := os.Stat(f); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Fatalf("%s is still in plugins/dist (%v)", tarball(pkg, before[pkg]), err)
|
|
}
|
|
if c.tarballsKept {
|
|
if err := os.WriteFile(f, wasTgz[pkg], 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
b = bytes.Replace(b, entry(b, pkg), old, 1)
|
|
}
|
|
if err := os.WriteFile(lockPath, b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.tail {
|
|
// what plugins#405's bun remove left after the object
|
|
f, err := os.OpenFile(filepath.Join(Dir(), "package.json"), os.O_APPEND|os.O_WRONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = f.WriteString("@magpie-community/opencode-zcode-auth\": \"^0.1.27\"\n }\n}\n")
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if c.copiesGone {
|
|
if err := os.RemoveAll(installCacheDir()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
mended := func(after string) {
|
|
t.Helper()
|
|
b := readLock()
|
|
for pkg, v := range want {
|
|
if got := Installed(pkg); got != v {
|
|
t.Errorf("after %s, %s is at %q, want %s", after, pkg, got, v)
|
|
}
|
|
if !bytes.Contains(entry(b, pkg), []byte(pkg+"@./dist/"+tarball(pkg, v)+`"`)) || bytes.Contains(b, []byte(tarball(pkg, before[pkg]))) {
|
|
t.Errorf("after %s, bun.lock doesn't have %s at %s alone:\n%s", after, pkg, v, b)
|
|
}
|
|
}
|
|
if got := specs(); !maps.Equal(got, pj) {
|
|
t.Errorf("after %s, package.json has %v, was %v", after, got, pj)
|
|
}
|
|
}
|
|
|
|
breakLock()
|
|
if _, err := Add(ctx, zcode); err != nil {
|
|
t.Fatalf("Add with the lock behind package.json: %v", err)
|
|
}
|
|
if v := Installed(zcode); v != "0.1.30" {
|
|
t.Fatalf("zcode installed %q", v)
|
|
}
|
|
mended("the add")
|
|
for pkg := range was {
|
|
if _, err := os.Stat(filepath.Join(Dir(), "dist", tarball(pkg, before[pkg]))); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Errorf("after the add, %s is still in plugins/dist (%v)", tarball(pkg, before[pkg]), err)
|
|
}
|
|
}
|
|
|
|
breakLock()
|
|
if err := Remove(ctx, zcode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(Dir(), "package.json")); bytes.Contains(b, []byte(zcode)) {
|
|
t.Fatalf("plugins/package.json still names zcode after Remove:\n%s", b)
|
|
}
|
|
mended("the removal")
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A lock with an older lock's bytes past its object (a shorter one written
|
|
// over a longer one without truncating, as plugins#405's package.json) is
|
|
// read as Bun reads it, its first object: the tail's older tarball isn't
|
|
// sent to bun update at every command, while the same tarball in the
|
|
// object still is.
|
|
func TestDistLockTailIsntRead(t *testing.T) {
|
|
const pj = `{
|
|
"name": "magpie-plugins",
|
|
"private": true,
|
|
"dependencies": {
|
|
"@magpie-community/opencode-autoclaw-auth": "./dist/magpie-community-opencode-autoclaw-auth-0.2.6.tgz"
|
|
}
|
|
}
|
|
`
|
|
at := func(file string) string {
|
|
return `{
|
|
"lockfileVersion": 2,
|
|
"packages": {
|
|
"@magpie-community/opencode-autoclaw-auth": ["@magpie-community/opencode-autoclaw-auth@./dist/` + file + `", {}, "sha512-..."],
|
|
}
|
|
}
|
|
`
|
|
}
|
|
const now, was = "magpie-community-opencode-autoclaw-auth-0.2.6.tgz", "magpie-community-opencode-autoclaw-auth-0.2.1.tgz"
|
|
old := at(was)
|
|
for _, c := range []struct {
|
|
name, lock string
|
|
update bool
|
|
}{
|
|
{"an older lock's tail past it", at(now) + old[strings.Index(old, ` "@magpie-community/`):], false},
|
|
{"the object behind", at(was), true},
|
|
{"a tail with braces and quotes in its strings", strings.Replace(at(now), `"sha512-..."`, `"}\"{]"`, 1) + old[strings.Index(old, ` "@magpie-community/`):], false},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("HOME", dir)
|
|
t.Setenv("USERPROFILE", dir)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
|
|
if err := os.MkdirAll(filepath.Join(Dir(), "dist"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, body := range map[string]string{"package.json": pj, "bun.lock": c.lock, filepath.Join("dist", now): "tgz"} {
|
|
if err := os.WriteFile(filepath.Join(Dir(), name), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var ran [][]string
|
|
was := bunCommand
|
|
t.Cleanup(func() { bunCommand = was })
|
|
bunCommand = func(ctx context.Context, bun, dir string, args ...string) *exec.Cmd {
|
|
ran = append(ran, args)
|
|
return exec.CommandContext(ctx, filepath.Join(dir, "no-such-bun"))
|
|
}
|
|
mendDist(context.Background(), "bun")
|
|
if got := len(ran) > 0; got != c.update {
|
|
t.Fatalf("bun update run: %v, want %v (%q)", got, c.update, ran)
|
|
}
|
|
})
|
|
}
|
|
}
|