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.
284 lines
9.2 KiB
Go
284 lines
9.2 KiB
Go
package plugin
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"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 made by magpie from tgz
|
|
// (unpackDist) and Bun run over it; one not whole even then 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
|
|
}
|
|
// Bun can't make the copy at all: magpie makes it from the tarball
|
|
// it verified, and Bun takes it as installed from then on
|
|
if uerr := distUnpack(name, tgz); uerr != nil {
|
|
log.Printf("plugins: %s: Bun's copy %s, and magpie's own can't be made: %v", name, bad2, uerr)
|
|
} else {
|
|
// Bun again, over magpie's copy: it writes the dependency and
|
|
// bun.lock, leaves the copy be, and says what else failed
|
|
err = bunAdd(ctx, bun, "./dist/"+file, name)
|
|
if bad2 = distCopyDiffers(tgz, name); bad2 == "" {
|
|
log.Printf("plugins: %s: Bun couldn't copy it twice; installed from the verified tarball", name)
|
|
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)
|
|
}
|
|
|
|
// distUnpack is unpackDist; tests make it fail.
|
|
var distUnpack = unpackDist
|
|
|
|
// unpackDist puts the package in tgz, a dist tarball checked against the
|
|
// index's sha512, at node_modules/<name> itself, when Bun can't copy it
|
|
// there from its cache: CHENGJIAHEGOXIS on X (v0.1.4241) got "Unexpected:
|
|
// failed copying files from cache to destination" adding GoAnyAPI at
|
|
// both tries, which on Windows is CopyFileW failing with a Win32 code
|
|
// Bun has no errno for (Bun 1.4.4's hardlink backend falls back to it,
|
|
// and never to anything else on Windows). The copy's .bun-tag is the
|
|
// tarball's integrity, as Bun writes it: Bun checks an installed
|
|
// tarball by that alone and leaves the copy as it is at every later
|
|
// command (Bun 1.4.4, with bun.lock and without). Only a package with no
|
|
// dependencies of its own: Bun would have to install those.
|
|
func unpackDist(name string, tgz []byte) error {
|
|
files, err := tarballFiles(tgz)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var pj struct {
|
|
Name string
|
|
Dependencies map[string]any
|
|
OptionalDependencies map[string]any
|
|
PeerDependencies map[string]any
|
|
BundleDependencies any
|
|
BundledDependencies any
|
|
}
|
|
b, ok := files["package.json"]
|
|
if !ok {
|
|
return errors.New("the tarball has no package.json")
|
|
}
|
|
if err := json.Unmarshal(b.body, &pj); err != nil {
|
|
return fmt.Errorf("the tarball's package.json: %w", err)
|
|
}
|
|
if pj.Name != name {
|
|
return fmt.Errorf("the tarball is %q's, not %s's", pj.Name, name)
|
|
}
|
|
if len(pj.Dependencies)+len(pj.OptionalDependencies)+len(pj.PeerDependencies) > 0 || pj.BundleDependencies != nil || pj.BundledDependencies != nil {
|
|
return errors.New("the package has dependencies of its own, which only Bun installs")
|
|
}
|
|
at := filepath.Join(Dir(), "node_modules", filepath.FromSlash(name))
|
|
if err := os.MkdirAll(filepath.Dir(at), 0o755); err != nil {
|
|
return err
|
|
}
|
|
tmp, err := os.MkdirTemp(filepath.Dir(at), "."+path.Base(name)+"-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer removeTree(tmp)
|
|
for rel, f := range files {
|
|
to := filepath.Join(tmp, filepath.FromSlash(rel))
|
|
if err := os.MkdirAll(filepath.Dir(to), 0o755); err != nil {
|
|
return err
|
|
}
|
|
if err := os.WriteFile(to, f.body, f.mode); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
sum := sha512.Sum512(tgz)
|
|
if err := os.WriteFile(filepath.Join(tmp, ".bun-tag"), []byte("sha512-"+base64.StdEncoding.EncodeToString(sum[:])), 0o644); err != nil {
|
|
return err
|
|
}
|
|
removeTree(at)
|
|
return os.Rename(tmp, at)
|
|
}
|
|
|
|
// tarFile is a regular file of a tarball: its bytes and the mode it is
|
|
// written with.
|
|
type tarFile struct {
|
|
body []byte
|
|
mode fs.FileMode
|
|
}
|
|
|
|
// tarballFiles is every regular file of an npm pack tarball, by its path
|
|
// under the package's folder (the first part of each name, whatever it
|
|
// is called, as Bun takes it). A name leaving that folder is refused.
|
|
func tarballFiles(tgz []byte) (map[string]tarFile, error) {
|
|
gz, err := gzip.NewReader(bytes.NewReader(tgz))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tr := tar.NewReader(gz)
|
|
files := map[string]tarFile{}
|
|
for {
|
|
h, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if h.Typeflag != tar.TypeReg {
|
|
continue
|
|
}
|
|
clean := path.Clean(strings.TrimPrefix(h.Name, "./"))
|
|
if !filepath.IsLocal(filepath.FromSlash(clean)) {
|
|
return nil, fmt.Errorf("the tarball names %q, outside its package", h.Name)
|
|
}
|
|
_, rel, ok := strings.Cut(clean, "/")
|
|
if !ok || rel == "" {
|
|
continue
|
|
}
|
|
body, err := io.ReadAll(tr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mode := fs.FileMode(0o644)
|
|
if h.Mode&0o111 != 0 {
|
|
mode = 0o755
|
|
}
|
|
files[rel] = tarFile{body, mode}
|
|
}
|
|
if len(files) == 0 {
|
|
return nil, errors.New("the tarball has no files")
|
|
}
|
|
return files, nil
|
|
}
|