CHENGJIAHEGOXIS on X, on v0.1.4241 (Windows): installing GoAnyAPI from
the Plugins page still failed with "Unexpected: failed copying files
from cache to destination for package @magpie-community/tools-goanyapi"
and "(LICENSE is missing)", at both of addDist's tries.
Cause, from Bun 1.4.4's source (src/install/PackageInstall.rs): on
Windows Bun copies each cached file by CreateHardLinkW, then CopyFileW,
with no other fallback; a Win32 error it has no errno for prints as
"Unexpected". On a machine where that copy fails (agency-agents failed
the same way there before) dropping the cache entry and trying again
can't help, which is all v0.1.4205 to v0.1.4241 did.
Now, when Bun's copy is still not whole after the second try, magpie
makes it from the tarball installDist verified against the index's
sha512 (unpackDist): the files go into a temp folder beside
node_modules/<name> with .bun-tag = the tarball's integrity, as Bun
writes it, and the folder is renamed into place. Bun takes a tarball
package whose .bun-tag matches as installed and never copies over it,
so bun add runs once more to write the dependency and bun.lock. Only a
package with no dependencies (optional, peer, bundled) whose
package.json names it is made this way; a path leaving the package is
refused. When magpie can't make it either, the add is errDistPartial as
before.
Run at c00c491bc + this change:
- mote: go test -count=1 ./internal/plugin/... ok (153s); go vet,
GOOS=windows go vet, GOOS=linux go vet -tags nogui ./internal/plugin/
clean; gofmt -l clean.
- xiaomi-gpu (Windows 11): go test -run
'TestDistAdd|TestUnpackDist|Leftover|Unnamed|TestDist' ./internal/plugin/ ok.
- Red on the old code: TestDistAddBunCantCopy against origin/main's
distcopy.go failed in both subtests with the reporter's error shape
("Unexpected: failed copying files … (package.json is missing)",
"(index.js is missing)").
- Real Bun 1.4.4, throwaway tests (not committed): on mote, a copy
unpacked with .bun-tag plus a MARK file survived bun add of another
package, bun install, bun update, bun remove of another, and bun
install with bun.lock deleted. On Windows, unpackDist then real
`bun add ./dist/<tgz>` wrote the dependency and lock, kept MARK, and
`bun install` said "no changes".
Not tried: the reporter's machine and its exact Win32 code; I could not
make real Bun's cache copy fail on Windows (a denied read is got past by
the hardlink, and across volumes Bun re-extracted its entry), so the
fallback itself ran with the fake bun only. A dist package with
dependencies is not unpacked, and the npm add is not covered.
199 lines
7.2 KiB
Go
199 lines
7.2 KiB
Go
package plugin
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"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)
|
|
}
|
|
}
|
|
|
|
// The reporter's case on v0.1.4241 (CHENGJIAHEGOXIS on X): Bun can't
|
|
// copy GoAnyAPI from its cache at either try ("Unexpected: failed
|
|
// copying files from cache to destination for package …", "LICENSE is
|
|
// missing"), or copies it short and exits 0. magpie makes the copy from
|
|
// the tarball it verified, tagged as Bun tags it, and Bun takes it as
|
|
// installed: the plugin is in, and a later bun command leaves its copy
|
|
// as it is.
|
|
func TestDistAddBunCantCopy(t *testing.T) {
|
|
for _, c := range []struct{ name, env, val string }{
|
|
{"Bun can't copy it", "MAGPIE_FAKE_BUN_HELD", "1"},
|
|
{"exit 0, a file missing", "MAGPIE_FAKE_BUN_LOSE", "index.js"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
goanyapiSandbox(t, nil, `{"plugins":[]}`, "")
|
|
t.Setenv(c.env, c.val)
|
|
noDistPause(t)
|
|
if _, err := AddExact(ctxMinute(t), goanyapi+"@latest"); err != nil {
|
|
t.Fatalf("add from dist failed: %v", err)
|
|
}
|
|
tgz := npmTarball(t, goanyapi, "0.1.1")
|
|
if bad := distCopyDiffers(tgz, goanyapi); bad != "" {
|
|
t.Fatalf("GoAnyAPI's copy isn't the tarball's: %s", bad)
|
|
}
|
|
sum := sha512.Sum512(tgz)
|
|
if tag, _ := os.ReadFile(filepath.Join(goanyapiAt(), ".bun-tag")); string(tag) != "sha512-"+base64.StdEncoding.EncodeToString(sum[:]) {
|
|
t.Fatalf(".bun-tag = %q, not the tarball's integrity", tag)
|
|
}
|
|
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)
|
|
}
|
|
// Bun's next command takes magpie's copy as installed
|
|
if err := os.WriteFile(filepath.Join(goanyapiAt(), "MARK"), nil, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bun, err := Bun(ctxMinute(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
installMu.Lock()
|
|
err = bunAdd(ctxMinute(t), bun, "./dist/magpie-community-tools-goanyapi-0.1.1.tgz", goanyapi)
|
|
installMu.Unlock()
|
|
if err != nil {
|
|
t.Fatalf("bun add over magpie's copy: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(goanyapiAt(), "MARK")); err != nil {
|
|
t.Fatalf("Bun copied over magpie's copy: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A copy neither Bun nor magpie can make whole 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)
|
|
was := distUnpack
|
|
distUnpack = func(string, []byte) error { return errors.New("no room") }
|
|
t.Cleanup(func() { distUnpack = was })
|
|
_, 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// magpie makes a copy only of a package Bun would install nothing more
|
|
// for, and only inside its folder.
|
|
func TestUnpackDistRefuses(t *testing.T) {
|
|
depsSandbox(t, nil, `{"plugins":[]}`)
|
|
tarball := func(files map[string]string) []byte {
|
|
var buf bytes.Buffer
|
|
gz := gzip.NewWriter(&buf)
|
|
tw := tar.NewWriter(gz)
|
|
for n, body := range files {
|
|
tw.WriteHeader(&tar.Header{Name: n, Mode: 0o644, Size: int64(len(body))})
|
|
tw.Write([]byte(body))
|
|
}
|
|
tw.Close()
|
|
gz.Close()
|
|
return buf.Bytes()
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
files map[string]string
|
|
}{
|
|
{"dependencies", map[string]string{"package/package.json": `{"name":"` + goanyapi + `","dependencies":{"zod":"^3"}}`}},
|
|
{"peer dependencies", map[string]string{"package/package.json": `{"name":"` + goanyapi + `","peerDependencies":{"zod":"^3"}}`}},
|
|
{"another package", map[string]string{"package/package.json": `{"name":"left-pad"}`}},
|
|
{"no package.json", map[string]string{"package/index.js": "x"}},
|
|
{"a path out", map[string]string{"package/package.json": `{"name":"` + goanyapi + `"}`, "package/../../x.js": "x"}},
|
|
} {
|
|
if err := unpackDist(goanyapi, tarball(c.files)); err == nil {
|
|
t.Errorf("%s: unpacked", c.name)
|
|
}
|
|
if _, err := os.Stat(goanyapiAt()); err == nil {
|
|
t.Errorf("%s: a copy was left", c.name)
|
|
}
|
|
}
|
|
}
|