Files
magpie/internal/plugin/bunload_test.go
T
yetone 5b32b7ea80 plugins: a Bun Windows can't load (0xc0000139, before 10 1809 / Server 2019) says so and what to do (zgcwkj on Discord)
zgcwkj runs magpie-cli-windows-amd64.exe as a Windows service (nssm) and
every plugin install failed with "bun add …: exit status 0xc0000139: (and
from the community repo's dist branch: bun add ./dist/….tgz: exit status
0xc0000139: )".

Why: 0xc0000139 is STATUS_ENTRYPOINT_NOT_FOUND, the loader refusing the
exe before it runs. Reading the PE imports (debug/pe) of Bun's
windows-x64(-baseline) zips: 1.3.14 and later, so magpie's floor 1.4.2
and the latest 1.4.4, import kernel32's CreatePseudoConsole,
ClosePseudoConsole and ResizePseudoConsole, which Windows has from 10
1809 (build 17763) / Server 2019 on. 1.3.13 doesn't. Bun's docs say
"Windows 10 version 1809 or later" (bun.com/docs/installation). So
Windows Server 2016 and Windows 10 1507 to 1803 (LTSB 2015/2016) can't
run any Bun magpie uses. 1.4.2's pinned SHA-256 matches the asset.

What changes:
- wontLoad (internal/plugin/bun.go) reads an exit of 0xc0000139 as
  "this Windows" and 0xc000001d or SIGILL as "this processor" (Bun's x64
  build targets SSE4.2), as a *BunUnstartableError whose message says
  which, the versions Bun needs, and MAGPIE_BUN.
- bun add, bun update, cantStart and whyDied (the host's start) wrap it.
- An install no longer tries the dist branch after it: that bun add
  can't start either, and its error read as the dist branch's.
- The GUI's fail() answers it with code bun_unstartable, bunWhy and
  said, an i18n.js key (bunSaid in api.go), which api() in app.js puts
  through t(): en, zh, zh-TW, ja, de, in the window and in magpie web.
  The sentences live in Go so app.js grows one line: with both in
  app.js, TestCoreScriptBudget was 128 bytes over app.js's budget and
  487 over the core's, and no budget is raised here.
  TestBunSaidIsTranslated fails when a language lacks one. The CLI
  prints the English message.
- No fallback to an older Bun: 1.3.13 is below BunVersion and Bun
  doesn't support that Windows, and the -baseline assets are now the
  same binary.
- docs/subsystems/provider-plugins.md says all this.

Run:
- New tests: TestWontLoadSaysWindowsOrCPU (injected NTSTATUS exits, a
  real kill -ILL), TestInstallOnUnstartableBunSaysWhyAndSkipsDist (0
  dist requests), TestHostOnUnstartableBunSaysWhy, TestFailSaysBun
  Unstartable, TestBunSaidIsTranslated, plugin-bun-unstartable.test.cjs (chromium and webkit,
  every language, window and web).
- Real Windows (10.0.26200 box, a sandbox dir of its own): a
  windows/amd64 nogui magpie-cli.exe with a fake magpie-bun.exe that
  exits 0xc0000139, `plugin add @magpie-community/opencode-raccoon-auth`
  printed the new message, and no dist folder was made.
- gofmt -l; go vet darwin, linux -tags nogui, windows; builds of all three.
- Full suite (go test -tags nogui -timeout 45m ./..., through
  suite-lock, at a6399dd49 plus this change with both sentences in app.js):
  every package ok except internal/gui's TestCoreScriptBudget (128
  bytes over app.js's budget, 487 over the core's). After the sentences
  moved to bunSaid: internal/gui and internal/plugin ok, budget
  included (core 1,528,331 of 1,528,525 before scoping the line to the
  code, ~20 bytes more after).
- GUI (chromium and webkit, node --test): i18n-keys, every plugin-*
  file and signin-paste-plugin, 660 of 660 before the move to bunSaid;
  after it 656 of 660 at load ~140: plugin-card-update webkit (#3079)
  and plugin-quota-error chromium ja, whose Chromium launch hit
  Playwright's 180s timeout before the test ran. Both files alone: 16
  of 16, 14 of 16 (which one not kept), 16 of 16, 16 of 16.
- Rebased on ff6090354: TestCoreScriptBudget, the new Go tests,
  plugin-bun-unstartable and i18n-keys again ok; gofmt; windows and
  linux vet of internal/plugin and internal/gui.

Broken by hand (each restored):
- wontLoad always nil: the three plugin tests fail, the install one
  with the reporter's exact "(and from the community repo's dist
  branch: …)" shape.
- the dist skip removed: 1 dist request.
- the whyDied check removed: the host test fails.
- fail()'s block removed: the gui Go test fails.
- app.js's line turned off: the GUI test fails, 16 of 16 (chromium).
- one language's key renamed: TestBunSaidIsTranslated fails (3 of 4).

Not tried: a real Windows before 1809 (the box is Windows 11) and a real
processor without SSE4.2; zgcwkj's Windows version is still unknown.
2026-10-25 05:54:07 +08:00

133 lines
4.4 KiB
Go

package plugin
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
)
// exitedWith is a process's exit as Windows reports one: its NTSTATUS as
// the exit code, which a fake bun on another system can't exit with.
type exitedWith uint32
func (e exitedWith) ExitCode() int { return int(uint32(e)) }
func (e exitedWith) Error() string { return fmt.Sprintf("exit status %#x", uint32(e)) }
// A bun that Windows won't load (STATUS_ENTRYPOINT_NOT_FOUND: Bun 1.3.14
// and later import CreatePseudoConsole, which Windows has from 10 1809 on,
// zgcwkj on Discord) or the processor won't run (STATUS_ILLEGAL_
// INSTRUCTION, SIGILL) says which and what to do; any other exit is
// given back as it was.
func TestWontLoadSaysWindowsOrCPU(t *testing.T) {
bun := `C:\magpie\cache\bun\1.4.2\magpie-bun.exe`
for _, c := range []struct {
err error
why string
says []string
}{
{exitedWith(0xc0000139), "windows", []string{"Windows 10 version 1809", "Windows Server 2019", "0xc0000139", "MAGPIE_BUN", bun}},
{exitedWith(0xc000001d), "cpu", []string{"processor", "SSE4.2", "MAGPIE_BUN", bun}},
} {
wrapped := errors.Join(errors.New("starting"), c.err)
ue := wontLoad(bun, wrapped)
if ue == nil || ue.Why != c.why {
t.Fatalf("wontLoad(%v) = %#v, want %s", c.err, ue, c.why)
}
for _, s := range c.says {
if !strings.Contains(ue.Error(), s) {
t.Errorf("%s: %q doesn't say %q", c.why, ue.Error(), s)
}
}
if !errors.Is(ue, c.err) {
t.Errorf("%s: the exit isn't kept under it", c.why)
}
var got *BunUnstartableError
if err := cantStart(bun, c.err); !errors.As(err, &got) || got.Why != c.why {
t.Errorf("cantStart(%v) = %v", c.err, err)
}
}
for _, other := range []error{exitedWith(1), exitedWith(0xc0000005), errors.New("exit status 3"), nil} {
if ue := wontLoad(bun, other); ue != nil {
t.Errorf("wontLoad(%v) = %v, want nil", other, ue)
}
}
if runtime.GOOS != "windows" {
err := exec.Command("/bin/sh", "-c", "kill -ILL $$").Run()
if ue := wontLoad("bun", err); ue == nil || ue.Why != "cpu" {
t.Fatalf("wontLoad(%v) = %v, want cpu", err, ue)
}
err = exec.Command("/bin/sh", "-c", "kill -SEGV $$").Run()
if ue := wontLoad("bun", err); ue != nil {
t.Fatalf("wontLoad(%v) = %v, want nil", err, ue)
}
}
}
// stopsOnSIGILL is a bun as one the processor can't run is: the kernel
// stops it on its first instruction the processor lacks.
func stopsOnSIGILL(t *testing.T, path string) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("the bun is a shell script")
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("#!/bin/sh\nkill -ILL $$\n"), 0o755); err != nil {
t.Fatal(err)
}
}
// An install on a bun this machine can't run says so, as a
// *BunUnstartableError the GUI translates, and isn't tried again from the
// community repo's dist branch: that bun add can't start either, and its
// error read as the dist branch's (bun add ./dist/…: exit status …).
func TestInstallOnUnstartableBunSaysWhyAndSkipsDist(t *testing.T) {
bunHome(t, BunVersion, time.Now())
stopsOnSIGILL(t, bunExeOf(BunVersion))
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
http.NotFound(w, r)
}))
t.Cleanup(srv.Close)
old := distBase
distBase = srv.URL
t.Cleanup(func() { distBase = old })
err := install(context.Background(), "@magpie-community/opencode-raccoon-auth@latest")
var ue *BunUnstartableError
if !errors.As(err, &ue) || ue.Why != "cpu" || ue.Bun != bunExeOf(BunVersion) {
t.Fatalf("install on a bun stopped by SIGILL: %v", err)
}
if strings.Contains(err.Error(), "dist branch") || hits.Load() != 0 {
t.Fatalf("the dist branch was tried (%d requests): %v", hits.Load(), err)
}
}
// The host on a bun this machine can't run says why.
func TestHostOnUnstartableBunSaysWhy(t *testing.T) {
bunHome(t, BunVersion, time.Now())
t.Cleanup(Settle)
stopsOnSIGILL(t, bunExeOf(BunVersion))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
_, _ = Add(ctx, abs)
_, err := Providers(ctx)
var ue *BunUnstartableError
if !errors.As(err, &ue) || ue.Why != "cpu" || !strings.Contains(err.Error(), "SSE4.2") {
t.Fatalf("Providers on a bun stopped by SIGILL: %v", err)
}
}