Files
magpie/internal/plugin/distcopy.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

149 lines
4.8 KiB
Go

package plugin
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// A dist tarball's add is judged by the copy Bun leaves in node_modules,
// not by what Bun says. Bun 1.4.4 installs a local tarball from its
// install cache (<cache>/@T@sha512-<32 hex>@@@1), trusting that entry
// whole once it is there: an entry missing a file (cut short, or a file
// taken out of it since) gives a copy missing that file, Bun saying "1
// package installed" and exiting 0. And a copy it can't make (a file of
// the old one held open on Windows: "EBUSY: failed copying files from
// cache to destination") still has it print "installed …", write the
// dependency and exit 1 with "Failed to install 1 package"
// (CHENGJIAHEGOXIS on X: "Unexpected: failed copying files …" adding
// GoAnyAPI on v0.1.4229). The tarball magpie checked against the index's
// sha512 is in hand, so the copy is checked against it, file by file.
// errDistPartial is a dist add whose copy in node_modules isn't the
// tarball's, after the add was tried again: the plugin isn't installed,
// whatever Bun said.
var errDistPartial = errors.New("the plugin's files didn't all reach plugins/node_modules")
// distAgainAfter is the pause before a dist add is tried again over a
// copy that wasn't whole: what held a file of it (an antivirus' scan, a
// host still letting go) has a moment to finish. Moved by tests.
var distAgainAfter = 2 * time.Second
// addDist adds the dist tarball file (plugins/dist/<file>, its bytes tgz)
// as name's, and checks the copy Bun made. A copy that isn't the
// tarball's is taken out with Bun's cached copy of the tarball, and the
// add tried once more; one still not whole is errDistPartial, with what
// Bun said. A whole copy is name installed: an error then is Bun's over
// another package, as before.
func addDist(ctx context.Context, bun, name, file string, tgz []byte) error {
err := bunAdd(ctx, bun, "./dist/"+file, name)
bad := distCopyDiffers(tgz, name)
if bad == "" {
return err
}
dropDistCopy(name, tgz)
select {
case <-ctx.Done():
return fmt.Errorf("%w (%s): %w", errDistPartial, bad, ctx.Err())
case <-time.After(distAgainAfter):
}
err = bunAdd(ctx, bun, "./dist/"+file, name)
bad2 := distCopyDiffers(tgz, name)
if bad2 == "" {
return err
}
if err == nil {
return fmt.Errorf("%w (%s)", errDistPartial, bad2)
}
return fmt.Errorf("%w (%s): %w", errDistPartial, bad2, err)
}
// distCopyDiffers says how node_modules/<name> isn't the package in tgz,
// or "" when every file of tgz is there with its bytes. Files the copy has
// besides (Bun's .bun-tag) don't count.
func distCopyDiffers(tgz []byte, name string) string {
at := filepath.Join(Dir(), "node_modules", filepath.FromSlash(name))
gz, err := gzip.NewReader(bytes.NewReader(tgz))
if err != nil {
return "the tarball can't be read: " + err.Error()
}
tr := tar.NewReader(gz)
files := 0
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "the tarball can't be read: " + err.Error()
}
if h.Typeflag != tar.TypeReg {
continue
}
// npm pack's files are under package/; the first part is the
// package's folder whatever it is named, as Bun takes it
_, rel, ok := strings.Cut(path.Clean(strings.TrimPrefix(h.Name, "./")), "/")
if !ok || rel == "" || rel == ".." || strings.HasPrefix(rel, "../") {
continue
}
want, err := io.ReadAll(tr)
if err != nil {
return "the tarball can't be read: " + err.Error()
}
files++
got, err := os.ReadFile(filepath.Join(at, filepath.FromSlash(rel)))
if errors.Is(err, fs.ErrNotExist) {
return rel + " is missing"
}
if err != nil {
return rel + " can't be read: " + err.Error()
}
if !bytes.Equal(got, want) {
return rel + " isn't the tarball's"
}
}
if files == 0 {
return "the tarball has no files"
}
return ""
}
// dropDistCopy takes out node_modules/<name> and Bun's cached copy of
// tgz: the next add extracts the tarball afresh. Best-effort: a file
// still held stays, and the next add says so.
func dropDistCopy(name string, tgz []byte) {
removeTree(filepath.Join(Dir(), "node_modules", filepath.FromSlash(name)))
sum := sha512.Sum512(tgz)
entry := "@T@sha512-" + hex.EncodeToString(sum[:])[:32] + "@@@1"
root := installCacheDir()
removeTree(filepath.Join(root, entry))
os.Remove(filepath.Join(root, filepath.FromSlash(name), entry))
}
// removeTree takes out dir, making its folders writable where that is
// what stops it (a read-only folder's files can't be unlinked on unix).
func removeTree(dir string) {
if os.RemoveAll(dir) == nil {
return
}
filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
if err == nil && d.IsDir() {
os.Chmod(p, 0o755)
}
return nil
})
os.RemoveAll(dir)
}