Files
magpie/internal/plugin/distcopy_test.go
T
yetone 96d1b93b23 plugin: a dist add is judged by the copy Bun leaves, tried again over a broken one
CHENGJIAHEGOXIS on X (Windows, Bun 1.4.4, after v0.1.4229) still couldn't
install @magpie-community/tools-goanyapi from the dist branch: "Unexpected:
failed copying files from cache to destination for package
@magpie-community/tools-goanyapi ... installed ... Failed to install 1
package". 38a90a34c only let a failure over *another* package pass; this one
names GoAnyAPI itself, so the add was refused, and nothing magpie did made
the next try different.

What Bun 1.4.4 does (reproduced, sandbox HOME, its source read in
src/install/PackageInstall.rs):
- A local tarball is installed from its install-cache entry
  <cache>/@T@sha512-<32 hex>@@@1, trusted whole once there. An entry
  missing a file gives a copy missing it, "1 package installed", exit 0
  (macOS on mote and Windows on xiaomi-gpu). Before this, magpie recorded
  that plugin as installed with no index.mjs.
- A copy it can't make still prints "installed", writes the dependency and
  exits 1: a file of the old copy held open without delete sharing on
  Windows gives the reporter's lines with EBUSY; a read-only folder on
  macOS gives EACCES. The reporter's "Unexpected" is a Win32 code Bun has
  no errno for; which one wasn't reproduced (antivirus, a cloud
  placeholder or a transient hold are the candidates).

Now addDist checks node_modules/<name> against the tarball installDist
verified, file by file (.bun-tag and other extras ignored). A copy that
differs is taken out together with Bun's cache entry and its scope link,
and after 2s (distAgainAfter) the add is tried once more. A whole copy is
the package in. One still not whole is errDistPartial, which install never
hands to othersFailed, so the dependency is put back and the plugin isn't
listed. The npm add has no tarball in hand and isn't judged this way.

The fake bun now copies a dist tarball through a modelled cache entry
(.held / MAGPIE_FAKE_BUN_HELD for a copy it can't make, MAGPIE_FAKE_BUN_LOSE
for a file that never arrives); both sandboxes pass BUN_INSTALL_CACHE_DIR.

Run:
- New tests red on the old dist.go/store.go (mote): TestDistAddOverABroken-
  CacheEntry (no index.js), TestDistAddOverAHeldCopy (the reporter's
  error), TestDistAddNeverRecordsAPartialCopy (exit 0 one returned nil).
- go test ./internal/plugin/... on mote at the pushed head (rebased on
  8bf9cb99d): ok 155s. gofmt -l clean; GOOS=windows go vet and GOOS=linux
  go vet -tags nogui of internal/plugin: ok.
- win-run go test ./internal/plugin/ (before the rebase): the new tests
  pass; TestStaleMissingIsUnknownWhileDistUnread (#5351, passes alone) and
  TestPiPackage (#4115, red alone on base dd89f1255 too) red.
- Real Bun 1.4.4, real goanyapi 0.1.1 tarball served as the dist branch,
  through AddExact (throwaway test, not committed): clean add, a cache
  entry with index.mjs taken out, and on Windows a file of the old copy
  held open for 3s (EBUSY, then the second try after the pause) each
  installed, and the plugin loaded under bun (its hooks return 37/50
  tools). A hold of 20s: add fails with Bun's EBUSY lines, GoAnyAPI not in
  package.json and not listed.
- All 105 dist packages' latest tarballs added with real Bun 1.4.4 on mote:
  every copy equals its tarball byte for byte (only .bun-tag besides), so
  the check refuses none of them.

Not tried: the reporter's own machine or its "Unexpected" code; the check
on npm adds (#6736).
2026-11-15 13:41:12 +08:00

107 lines
3.6 KiB
Go

package plugin
import (
"crypto/sha512"
"encoding/hex"
"errors"
"os"
"path/filepath"
"slices"
"testing"
)
// goanyapiAt is node_modules' copy of GoAnyAPI.
func goanyapiAt() string {
return filepath.Join(Dir(), "node_modules", filepath.FromSlash(goanyapi))
}
// noDistPause tries a dist add again at once.
func noDistPause(t *testing.T) {
was := distAgainAfter
distAgainAfter = 0
t.Cleanup(func() { distAgainAfter = was })
}
// goanyapiListed says whether plugins.json lists GoAnyAPI.
func goanyapiListed() bool {
return slices.ContainsFunc(Load().Plugins, func(e Entry) bool { return Name(e.Spec) == goanyapi })
}
// Bun's cache entry for the dist tarball missing a file: Bun 1.4.4
// copies the entry as it is, says "1 package installed" and exits 0
// (seen on macOS and Windows). The copy is checked against the tarball,
// the entry taken out and the add tried again: the plugin is whole.
func TestDistAddOverABrokenCacheEntry(t *testing.T) {
goanyapiSandbox(t, nil, `{"plugins":[]}`, "")
sum := sha512.Sum512(npmTarball(t, goanyapi, "0.1.1"))
entry := filepath.Join(installCacheDir(), "@T@sha512-"+hex.EncodeToString(sum[:])[:32]+"@@@1")
if err := os.MkdirAll(entry, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(entry, "package.json"), []byte(`{"name":"`+goanyapi+`","version":"0.1.1","main":"index.js","type":"module"}`), 0o644); err != nil {
t.Fatal(err)
}
noDistPause(t)
if _, err := AddExact(ctxMinute(t), goanyapi+"@latest"); err != nil {
t.Fatalf("add from dist failed: %v", err)
}
if _, err := os.Stat(filepath.Join(goanyapiAt(), "index.js")); err != nil {
t.Fatalf("GoAnyAPI's copy has no index.js: %v", err)
}
if !goanyapiListed() {
t.Fatalf("GoAnyAPI not listed: %+v", Load().Plugins)
}
}
// The reporter's case (CHENGJIAHEGOXIS, v0.1.4229 on Windows): Bun
// couldn't copy GoAnyAPI over its old copy ("Unexpected: failed copying
// files from cache to destination for package …", "installed …",
// "Failed to install 1 package"). The old copy is taken out, and the add
// tried again goes in.
func TestDistAddOverAHeldCopy(t *testing.T) {
goanyapiSandbox(t, nil, `{"plugins":[]}`, "")
if err := os.MkdirAll(goanyapiAt(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(goanyapiAt(), ".held"), nil, 0o644); err != nil {
t.Fatal(err)
}
noDistPause(t)
if _, err := AddExact(ctxMinute(t), goanyapi+"@latest"); err != nil {
t.Fatalf("add from dist failed: %v", err)
}
if _, err := os.Stat(filepath.Join(goanyapiAt(), "index.js")); err != nil {
t.Fatalf("GoAnyAPI's copy has no index.js: %v", err)
}
if d := depsOf(t); d[goanyapi] != "./dist/magpie-community-tools-goanyapi-0.1.1.tgz" || !goanyapiListed() {
t.Fatalf("GoAnyAPI isn't in: deps %v, plugins %+v", d, Load().Plugins)
}
}
// A copy that is still not the tarball's after the second try is no
// plugin installed, whether Bun exited 0 over it or said "installed"
// and failed: not listed, no dependency left, and the error says which
// file.
func TestDistAddNeverRecordsAPartialCopy(t *testing.T) {
for _, c := range []struct{ name, env, val string }{
{"exit 0, a file missing", "MAGPIE_FAKE_BUN_LOSE", "index.js"},
{"held", "MAGPIE_FAKE_BUN_HELD", "1"},
} {
t.Run(c.name, func(t *testing.T) {
goanyapiSandbox(t, nil, `{"plugins":[]}`, "")
t.Setenv(c.env, c.val)
noDistPause(t)
_, err := AddExact(ctxMinute(t), goanyapi+"@latest")
if !errors.Is(err, errDistPartial) {
t.Fatalf("add = %v, want errDistPartial", err)
}
if goanyapiListed() {
t.Fatalf("GoAnyAPI listed: %+v", Load().Plugins)
}
if d := depsOf(t); d[goanyapi] != "" {
t.Fatalf("GoAnyAPI left a dependency: %v", d)
}
})
}
}