diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 87ddc371b..71218656f 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -125,6 +125,8 @@ Keep the migration, host, and upstream behavior separate when investigating fail The host gets magpie's proxy only as `MAGPIE_*_PROXY` and applies it to each fetch itself (`proxyFor`), and to each HTTP/2 session a plugin opens (`http2.connect`, `tunnel`, #1035). A host on the user's own network goes around that proxy as it does for magpie's own requests (`onLAN` in `host.js`, a port of `internal/netproxy/lan.go`'s: a private, link-local, loopback or CGNAT address, a `.local` name, or a name every address of which is one of those, looked up for 400 ms at most and kept a minute; #1572). A provider's own proxy is still taken as it is. The two rules are kept the same: a change to one is made in the other. A program a plugin starts, such as the Grok plugin's `grok login`, gets it back as `HTTPS_PROXY`/`HTTP_PROXY` from the spawn wrapper in `host.js`, unless the plugin set one. A sign-in that works in the built-in and fails through the plugin may be a host difference like this one, not a plugin bug. +magpie downloads the Bun build for this machine (`bunTarget` in [`bun.go`](../../internal/plugin/bun.go)): the baseline build on x64 Linux and Windows, and on a musl Linux such as Alpine (`libc.Musl`, the check Claude Code's download uses too) the musl one, `bun-linux-x64-musl-baseline` or `bun-linux-aarch64-musl`. A glibc build names glibc's loader, which Alpine lacks, so it couldn't start there and exec said "no such file or directory" (#1877). A musl Linux keeps its Buns and their `state.json` under `bun/musl`, so a machine whose C library changed never runs the other one's build or takes its set-aside versions; everywhere else they stay in `bun`, where a Bun downloaded before is. A bun that is on disk but can't be started says it is a build for another C library and names `MAGPIE_BUN` (`cantStart`), and it isn't fallen back from or set aside: the version before is the same build. Bun's musl build needs libstdc++ and libgcc; on an Alpine without them, `bun add` and the host's start say which library is missing and `apk add libstdc++ libgcc` (`missingLibs`). `TestBunTargetForMusl`, `TestMuslBunKeptApartFromGlibc`, `TestCantStartSaysCLibrary`, `TestHostOnUnstartableBunSaysCLibrary`, `TestMissingLibsNamesThem` and `TestHostOnBunLackingLibsSaysWhich` in [`bunmusl_test.go`](../../internal/plugin/bunmusl_test.go) cover these. + Bun takes only http:// and https:// proxies. A SOCKS5 one, such as a Mac's system SOCKS proxy, is given to every bun magpie starts as a loopback HTTP proxy in front of it (`netproxy.Bridge`): to the host through `hostEnv`, and to `bun add`, `bun update` and `bun remove` through `bunCommand` (`netproxy.EnvForBun`). Before, an install with only a SOCKS system proxy failed as `UnsupportedProxyProtocol` (#1409). An agent CLI that bun installed is updated through the bridge too (`updateEnv` in `internal/agent/cliupdate.go`). The host reads its proxy once, when it starts. When the proxy magpie would give a new host differs from the one the running host got (the system proxy set after magpie started, as at login before Clash is up, or Settings changed), the next call to the host replaces it (`proxyMoved` in [`host.go`](../../internal/plugin/host.go), looked at no more than every 15s, or every 2s while the host has no proxy: `proxyLookNone`, #1518). A call made for a read nobody asked for, while allowances are read only when asked (`netproxy.Held`), never reaches the host. Before this, a host started without a proxy kept none, so the Grok plugin's `grok models` couldn't renew its token and the account read as signed out after each restart (#1363). diff --git a/internal/claudecode/claudecode.go b/internal/claudecode/claudecode.go index 1c990a641..e41a0c507 100644 --- a/internal/claudecode/claudecode.go +++ b/internal/claudecode/claudecode.go @@ -26,6 +26,7 @@ import ( "strings" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/libc" "github.com/yetone/magpie/internal/proc" ) @@ -118,16 +119,6 @@ func Platform(goos, goarch string, musl bool) (string, error) { return "", fmt.Errorf("Claude Code has no build for %s", goos) } -// isMusl tells a musl Linux as Anthropic's installer does: by musl's loader. -func isMusl() bool { - for _, p := range []string{"/lib/libc.musl-x86_64.so.1", "/lib/libc.musl-aarch64.so.1"} { - if _, err := os.Stat(p); err == nil { - return true - } - } - return false -} - // Release is a build of Claude Code for this machine, as the manifest // names it. type Release struct { @@ -141,7 +132,7 @@ type Release struct { // Latest asks for the newest release (the "latest" channel, Claude Code's // own default) and its build for this machine. func Latest(ctx context.Context) (Release, error) { - return latest(ctx, runtime.GOOS, runtime.GOARCH, runtime.GOOS == "linux" && isMusl()) + return latest(ctx, runtime.GOOS, runtime.GOARCH, libc.Musl()) } func latest(ctx context.Context, goos, goarch string, musl bool) (Release, error) { diff --git a/internal/cli/claude_code_cli_test.go b/internal/cli/claude_code_cli_test.go index cd3ab62e6..941ef7bb6 100644 --- a/internal/cli/claude_code_cli_test.go +++ b/internal/cli/claude_code_cli_test.go @@ -13,6 +13,7 @@ import ( "testing" "github.com/yetone/magpie/internal/claudecode" + "github.com/yetone/magpie/internal/libc" ) // claudeCodeOut runs `magpie claude-code args…` and gives what it printed. @@ -47,10 +48,8 @@ func TestClaudeCodeInstallDownloadsTheRelease(t *testing.T) { if err != nil { t.Skip(err) } - if runtime.GOOS == "linux" { - if _, err := os.Stat("/lib/libc.musl-x86_64.so.1"); err == nil { - t.Skip("a musl Linux asks for the -musl build") - } + if libc.Musl() { + t.Skip("a musl Linux asks for the -musl build") } const body = "#!/bin/sh\necho '2.1.296 (Claude Code)'\n" sum := sha256.Sum256([]byte(body)) diff --git a/internal/libc/libc.go b/internal/libc/libc.go new file mode 100644 index 000000000..ea8aeeea8 --- /dev/null +++ b/internal/libc/libc.go @@ -0,0 +1,31 @@ +// Package libc tells which C library this Linux runs on, for the +// downloads that come in a glibc build and a musl one: Claude Code's and +// Bun's. A glibc build names glibc's loader (/lib64/ld-linux-x86-64.so.2) +// as its interpreter, which a musl Linux such as Alpine doesn't have, so +// the kernel can't start it and exec says "no such file or directory" +// (#1877). +package libc + +import ( + "os" + "runtime" +) + +// muslLoaders are the files that tell a musl Linux, as Anthropic's +// installer tells one: musl's libc, under the name Alpine gives it. +var muslLoaders = []string{"/lib/libc.musl-x86_64.so.1", "/lib/libc.musl-aarch64.so.1"} + +// Musl is whether this is a Linux whose C library is musl (Alpine). It is +// false on every other system. +func Musl() bool { + return runtime.GOOS == "linux" && muslAt(muslLoaders) +} + +func muslAt(paths []string) bool { + for _, p := range paths { + if _, err := os.Stat(p); err == nil { + return true + } + } + return false +} diff --git a/internal/libc/libc_test.go b/internal/libc/libc_test.go new file mode 100644 index 000000000..6b3cc66b2 --- /dev/null +++ b/internal/libc/libc_test.go @@ -0,0 +1,32 @@ +package libc + +import ( + "os" + "path/filepath" + "runtime" + "testing" +) + +// musl's libc at either name tells a musl Linux; neither there tells a +// glibc one. +func TestMuslAt(t *testing.T) { + dir := t.TempDir() + x64 := filepath.Join(dir, "libc.musl-x86_64.so.1") + arm := filepath.Join(dir, "libc.musl-aarch64.so.1") + if muslAt([]string{x64, arm}) { + t.Fatal("musl with neither file there") + } + if err := os.WriteFile(arm, nil, 0o644); err != nil { + t.Fatal(err) + } + if !muslAt([]string{x64, arm}) { + t.Fatal("not musl with musl's aarch64 libc there") + } +} + +// Only a Linux is ever musl. +func TestMuslOnlyOnLinux(t *testing.T) { + if runtime.GOOS != "linux" && Musl() { + t.Fatalf("Musl() on %s", runtime.GOOS) + } +} diff --git a/internal/plugin/bun.go b/internal/plugin/bun.go index d1a0b89eb..f3e8757a9 100644 --- a/internal/plugin/bun.go +++ b/internal/plugin/bun.go @@ -10,15 +10,20 @@ import ( "errors" "fmt" "io" + "io/fs" "net/http" "os" "os/exec" "path/filepath" + "regexp" "runtime" + "slices" "strings" "sync" + "time" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/libc" "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/source" ) @@ -37,6 +42,8 @@ var bunSums = map[string]string{ "bun-darwin-x64.zip": "80520d7e17526308c9185d261679ac6d27798d3803a0e9f7ff9121ab8affb012", "bun-linux-aarch64.zip": "54328bbc2d9c8e0c9f892c544d66c57a83b84139e34909e5ee81758f1ac8fda7", "bun-linux-x64-baseline.zip": "c678040f14fe0440eb839d37cbd0ce4c051a32da72806ac97de6a6aab6bf728f", + "bun-linux-aarch64-musl.zip": "71760b6c8ea30623b81a4907cb815d48e2ea266f2e73e751534a44a0607950df", + "bun-linux-x64-musl-baseline.zip": "76e1db84e98f22f78de0a87e309bfbbf297732847f9720db36750646c85c8c18", "bun-windows-x64-baseline.zip": "78c221c2376f79731ccf4e4af0b3bb46d81fefa3296c5abee09ad8a1b21e68c6", "bun-linux-aarch64-android.zip": "a1c7e2983f1bb65146beb256a4d72449f23042412bc2cf278aa6397ba27e0274", "bun-linux-x64-android-baseline.zip": "fe36d8d4795e0eadc22fb6696d44d168491c2e5b9b7cbb12b8c96b0c0c40a4f9", @@ -54,16 +61,36 @@ var bunRelease = "https://github.com/oven-sh/bun/releases/download" var bunMu sync.Mutex +// bunPlatform is this machine as Bun's builds tell them apart: its OS, its +// architecture, and whether it is a Linux on musl (Alpine), where Bun's +// glibc builds can't start (#1877). A var for tests. +var bunPlatform = func() (goos, goarch string, musl bool) { + return runtime.GOOS, runtime.GOARCH, libc.Musl() +} + // bunTarget is the name of Bun's build for this machine: bun-darwin-aarch64 // and the like. x64 Linux and Windows get the baseline build, which runs -// on CPUs without AVX2 as well. +// on CPUs without AVX2 as well; a musl Linux gets the musl build. func bunTarget() (string, error) { - arch := map[string]string{"arm64": "aarch64", "amd64": "x64"}[runtime.GOARCH] + return bunTargetFor(bunPlatform()) +} + +func bunTargetFor(goos, goarch string, musl bool) (string, error) { + arch := map[string]string{"arm64": "aarch64", "amd64": "x64"}[goarch] if arch == "" { - return "", fmt.Errorf("Bun has no build for %s/%s", runtime.GOOS, runtime.GOARCH) + return "", fmt.Errorf("Bun has no build for %s/%s", goos, goarch) } - switch runtime.GOOS { - case "darwin", "linux": + switch goos { + case "darwin": + case "linux": + if musl { + // arm64 has no baseline build, musl or not + t := "bun-linux-" + arch + "-musl" + if arch == "x64" { + t += "-baseline" + } + return t, nil + } case "android": t := "bun-linux-" + arch + "-android" if arch == "x64" { @@ -72,13 +99,13 @@ func bunTarget() (string, error) { return t, nil case "windows": if arch != "x64" { - return "", fmt.Errorf("Bun has no build for windows/%s", runtime.GOARCH) + return "", fmt.Errorf("Bun has no build for windows/%s", goarch) } default: - return "", fmt.Errorf("Bun has no build for %s", runtime.GOOS) + return "", fmt.Errorf("Bun has no build for %s", goos) } - t := "bun-" + runtime.GOOS + "-" + arch - if arch == "x64" && runtime.GOOS != "darwin" { + t := "bun-" + goos + "-" + arch + if arch == "x64" && goos != "darwin" { t += "-baseline" } return t, nil @@ -91,6 +118,63 @@ func bunExe() string { return "bun" } +// missingLibRe finds what a musl Linux's loader says when a program needs +// a library the system lacks: "Error loading shared library +// libstdc++.so.6: No such file or directory (needed by …)". +var missingLibRe = regexp.MustCompile(`Error loading shared library ([^:\s]+)`) + +// missingLibs says which libraries a bun that started couldn't be linked +// with lacks, from what it printed, "" when that isn't why it failed. Bun's +// musl build needs libstdc++ and libgcc, which a bare Alpine doesn't have +// (#1877); the loader's first line names them, under a screenful of +// "Error relocating" lines. +func missingLibs(out string) string { + var libs []string + for _, m := range missingLibRe.FindAllStringSubmatch(out, -1) { + if !slices.Contains(libs, m[1]) { + libs = append(libs, m[1]) + } + } + if len(libs) == 0 { + return "" + } + return fmt.Sprintf("this system lacks %s, which Bun needs (on Alpine: apk add libstdc++ libgcc), or set MAGPIE_BUN to a bun that runs here", strings.Join(libs, ", ")) +} + +// whyDied adds to the error of a host that died starting what the bun it +// ran on says when asked its version, when that is a library it lacks. +func whyDied(ctx context.Context, bun string, err error) error { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + out, verr := command(ctx, bun, "--version").CombinedOutput() + if verr == nil { + return err + } + if hint := missingLibs(string(out)); hint != "" { + return fmt.Errorf("%s: %w", hint, err) + } + return err +} + +// cantStart says what it means when a bun that is on disk can't be +// started with "no such file or directory": not that the file is missing, +// but that the interpreter it names is. A glibc build on a musl Linux +// (Alpine) names glibc's loader, which isn't there (#1877). Any other +// error is given back as it is. +func cantStart(bun string, err error) error { + if err == nil || !errors.Is(err, fs.ErrNotExist) { + return err + } + if fi, serr := os.Stat(bun); serr != nil || fi.IsDir() { + return err + } + if b := os.Getenv("MAGPIE_BUN"); b != "" && b == bun { + return fmt.Errorf("this system can't start $MAGPIE_BUN (%s), though it is there: it is likely a build for another C library than this system's (glibc or musl), whose loader isn't here: %w", bun, err) + } + target, _ := bunTarget() + return fmt.Errorf("this system can't start the Bun magpie downloaded (%s, %s), though it is there: the build doesn't match this system's C library (glibc or musl). Set MAGPIE_BUN to a bun built for this system to run plugins on it: %w", target, bun, err) +} + // Bun is the bun to run plugins with: $MAGPIE_BUN when set, else the one // magpie downloaded and keeps up to date, downloading it now when there // is none yet. diff --git a/internal/plugin/bunmusl_test.go b/internal/plugin/bunmusl_test.go new file mode 100644 index 000000000..b7c4d503a --- /dev/null +++ b/internal/plugin/bunmusl_test.go @@ -0,0 +1,241 @@ +package plugin + +import ( + "context" + "errors" + "io/fs" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + "time" +) + +// stubPlatform has bunTarget and bunRoot see the machine given. +func stubPlatform(t *testing.T, goos, goarch string, musl bool) { + t.Helper() + old := bunPlatform + bunPlatform = func() (string, string, bool) { return goos, goarch, musl } + t.Cleanup(func() { bunPlatform = old }) +} + +// A musl Linux (Alpine) gets Bun's musl build: a glibc one names a loader +// Alpine doesn't have and can't start there (#1877). x64 keeps the +// baseline build, arm64 has none; musl changes nothing off Linux. +func TestBunTargetForMusl(t *testing.T) { + for _, c := range []struct { + goos, goarch string + musl bool + want string + }{ + {"linux", "amd64", true, "bun-linux-x64-musl-baseline"}, + {"linux", "arm64", true, "bun-linux-aarch64-musl"}, + {"linux", "amd64", false, "bun-linux-x64-baseline"}, + {"linux", "arm64", false, "bun-linux-aarch64"}, + {"darwin", "arm64", true, "bun-darwin-aarch64"}, + {"windows", "amd64", true, "bun-windows-x64-baseline"}, + {"android", "arm64", true, "bun-linux-aarch64-android"}, + } { + got, err := bunTargetFor(c.goos, c.goarch, c.musl) + if err != nil || got != c.want { + t.Errorf("bunTargetFor(%s, %s, musl %v) = %q, %v; want %q", c.goos, c.goarch, c.musl, got, err, c.want) + } + if c.goos == "linux" && bunChecksum(BunVersion, got) == "" { + t.Errorf("no checksum for %s", got) + } + } +} + +// One version is a different build on glibc and on musl, so each keeps +// its own: a machine whose C library changed (a container moved from +// Debian to Alpine) downloads the musl build rather than run the glibc one +// it has, nor reads the glibc one's state. A glibc machine keeps the Bun +// it downloaded before, where it was, with no new download. +func TestMuslBunKeptApartFromGlibc(t *testing.T) { + bunHome(t, BunVersion, time.Now()) + stubPlatform(t, "linux", "amd64", false) + glibc := bunExeOf(BunVersion) + if err := os.MkdirAll(filepath.Dir(glibc), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(glibc, []byte("glibc build"), 0o755); err != nil { + t.Fatal(err) + } + if err := writeBunState(bunState{Bad: []string{"9.9.9"}}); err != nil { + t.Fatal(err) + } + if rel, _ := filepath.Rel(filepath.Dir(bunRoot()), glibc); rel != filepath.Join("bun", BunVersion, magpieBun()) { + t.Fatalf("a glibc Bun is kept at %s, not where it was before", glibc) + } + + stubPlatform(t, "linux", "amd64", true) + if HasBun() { + t.Fatal("HasBun on musl with only the glibc build on disk") + } + if s := readBunState(); len(s.Bad) != 0 { + t.Fatalf("musl reads the glibc build's state: %+v", s) + } + asked := bunReleases(t, []byte("musl build")) + exe, err := Bun(context.Background()) + if err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(exe); string(b) != "musl build" || asked.Load() != 1 || exe == glibc { + t.Fatalf("musl Bun = %s holding %q after %d downloads", exe, b, asked.Load()) + } + + stubPlatform(t, "linux", "amd64", false) + exe, err = Bun(context.Background()) + if err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(exe); exe != glibc || string(b) != "glibc build" || asked.Load() != 1 { + t.Fatalf("glibc Bun = %s holding %q after %d downloads", exe, b, asked.Load()) + } + if s := readBunState(); len(s.Bad) != 1 { + t.Fatalf("glibc state = %+v", s) + } +} + +// unstartable is a bun on disk that exec can't start with "no such file +// or directory": the interpreter it names isn't there, as glibc's loader +// isn't on Alpine. +func unstartable(t *testing.T, path string) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("Windows has no interpreter line") + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte("#!/lib64/no-such-ld-linux-x86-64.so.2\n"), 0o755); err != nil { + t.Fatal(err) + } +} + +// A bun that is there but can't be started is said to be a build for +// another C library, with MAGPIE_BUN as the way round, not left reading +// as a missing file. A bun that really is missing, and any other error, +// stay as they are. +func TestCantStartSaysCLibrary(t *testing.T) { + bunHome(t, BunVersion, time.Now()) + bun := filepath.Join(t.TempDir(), "magpie-bun") + unstartable(t, bun) + err := exec.Command(bun).Start() + if !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("starting it: %v, want ENOENT", err) + } + got := cantStart(bun, err) + if msg := got.Error(); !strings.Contains(msg, "C library") || !strings.Contains(msg, "MAGPIE_BUN") || !strings.Contains(msg, bun) || !errors.Is(got, fs.ErrNotExist) { + t.Fatalf("cantStart = %v", got) + } + + t.Setenv("MAGPIE_BUN", bun) + if msg := cantStart(bun, err).Error(); !strings.Contains(msg, "$MAGPIE_BUN") || !strings.Contains(msg, "C library") { + t.Fatalf("cantStart of $MAGPIE_BUN = %s", msg) + } + + missing := filepath.Join(t.TempDir(), "bun") + merr := exec.Command(missing).Start() + if got := cantStart(missing, merr); got != merr { + t.Fatalf("cantStart of a missing bun = %v, want %v", got, merr) + } + other := errors.New("exit status 3") + if got := cantStart(bun, other); got != other { + t.Fatalf("cantStart of another error = %v", got) + } +} + +// The plugin host on a downloaded Bun that can't be started says why, and +// the Bun isn't set aside: the one before is the same build for this +// system, so falling back can't help and would mark a good version bad. +func TestHostOnUnstartableBunSaysCLibrary(t *testing.T) { + bunHome(t, "9.9.9", time.Now().Add(-72*time.Hour)) + t.Cleanup(Settle) + unstartable(t, bunExeOf("9.9.9")) + unstartable(t, bunExeOf(BunVersion)) + if err := writeBunState(bunState{Current: "9.9.9", Previous: BunVersion}); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + abs, _ := filepath.Abs("testdata/fake/index.js") + if _, err := Add(ctx, abs); err != nil && !strings.Contains(err.Error(), "C library") { + t.Fatal(err) + } + _, err := Providers(ctx) + if err == nil || !strings.Contains(err.Error(), "C library") || !strings.Contains(err.Error(), "MAGPIE_BUN") { + t.Fatalf("Providers on an unstartable Bun: %v", err) + } + if s := readBunState(); s.Current != "9.9.9" || len(s.Bad) != 0 { + t.Fatalf("state %+v after an unstartable Bun", s) + } +} + +// alpineLoader is what musl's loader printed on a bare Alpine 3.20 when +// asked to run Bun 1.4.2's musl build (bun --version), cut after a few of +// its 40 "Error relocating" lines. +const alpineLoader = `Error loading shared library libstdc++.so.6: No such file or directory (needed by /sbx/.cache/magpie/bun/musl/1.4.2/magpie-bun) +Error relocating /sbx/.cache/magpie/bun/musl/1.4.2/magpie-bun: _ZSt15get_new_handlerv: symbol not found +Error relocating /sbx/.cache/magpie/bun/musl/1.4.2/magpie-bun: _ZSt24__throw_out_of_range_fmtPKcz: symbol not found +Error relocating /sbx/.cache/magpie/bun/musl/1.4.2/magpie-bun: __once_proxy: symbol not found +` + +// lacksLibstdcxx is a bun as Bun's musl build is on a bare Alpine: the +// loader says what it lacks and exits 127. +func lacksLibstdcxx(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) + } + script := "#!/bin/sh\ncat >&2 <<'OUT'\n" + alpineLoader + "OUT\nexit 127\n" + if err := os.WriteFile(path, []byte(script), 0o755); err != nil { + t.Fatal(err) + } +} + +// Bun's musl build on an Alpine without libstdc++ says which library is +// missing and how to add it, where the loader's first line was cut by its +// screenful of relocation errors; other output says nothing. +func TestMissingLibsNamesThem(t *testing.T) { + got := missingLibs(alpineLoader) + if !strings.Contains(got, "libstdc++.so.6") || !strings.Contains(got, "apk add libstdc++") || !strings.Contains(got, "MAGPIE_BUN") { + t.Fatalf("missingLibs = %q", got) + } + if got := missingLibs("error: GET https://registry.npmjs.org/x - 404\n"); got != "" { + t.Fatalf("missingLibs of a 404 = %q", got) + } + + bunHome(t, BunVersion, time.Now()) + lacksLibstdcxx(t, bunExeOf(BunVersion)) + err := install(context.Background(), "@magpie-community/opencode-cursor-auth@latest") + if err == nil || !strings.Contains(err.Error(), "libstdc++.so.6") || !strings.Contains(err.Error(), "apk add") { + t.Fatalf("bun add on a bun lacking libstdc++: %v", err) + } +} + +// The host on a bun lacking a library says which, and the Bun isn't set +// aside for it. +func TestHostOnBunLackingLibsSaysWhich(t *testing.T) { + bunHome(t, BunVersion, time.Now()) + t.Cleanup(Settle) + lacksLibstdcxx(t, bunExeOf(BunVersion)) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + abs, _ := filepath.Abs("testdata/fake/index.js") + if _, err := Add(ctx, abs); err != nil && !strings.Contains(err.Error(), "libstdc++") { + t.Fatal(err) + } + _, err := Providers(ctx) + if err == nil || !strings.Contains(err.Error(), "libstdc++.so.6") || !strings.Contains(err.Error(), "apk add") { + t.Fatalf("Providers on a bun lacking libstdc++: %v", err) + } + if s := readBunState(); len(s.Bad) != 0 { + t.Fatalf("state %+v", s) + } +} diff --git a/internal/plugin/bunupdate.go b/internal/plugin/bunupdate.go index 62190b774..2740c94cb 100644 --- a/internal/plugin/bunupdate.go +++ b/internal/plugin/bunupdate.go @@ -42,7 +42,20 @@ type bunState struct { Checked time.Time `json:"checked,omitzero"` } -func bunRoot() string { return filepath.Join(filepath.Dir(catalog.CachePath()), "bun") } +// bunRoot is where magpie keeps its Buns and their state.json. A musl +// Linux keeps them under bun/musl: one version is a different build there, +// so a machine whose C library changed under it (a container's image +// moved from Debian to Alpine, or back) never runs the other one's build, +// nor takes its state, whose Bad may list versions only the wrong build +// failed (#1877). Everywhere else they stay in bun itself, where a Bun +// downloaded before is. +func bunRoot() string { + root := filepath.Join(filepath.Dir(catalog.CachePath()), "bun") + if t, err := bunTarget(); err == nil && strings.Contains(t, "-musl") { + root = filepath.Join(root, "musl") + } + return root +} func bunDirOf(v string) string { return filepath.Join(bunRoot(), v) } diff --git a/internal/plugin/bunupdate_test.go b/internal/plugin/bunupdate_test.go index d9546183c..d581c01bf 100644 --- a/internal/plugin/bunupdate_test.go +++ b/internal/plugin/bunupdate_test.go @@ -96,6 +96,8 @@ func TestBunDefaultSums(t *testing.T) { "bun-darwin-x64", "bun-linux-aarch64", "bun-linux-x64-baseline", + "bun-linux-aarch64-musl", + "bun-linux-x64-musl-baseline", "bun-windows-x64-baseline", "bun-linux-aarch64-android", "bun-linux-x64-android-baseline", diff --git a/internal/plugin/host.go b/internal/plugin/host.go index 93fcb9ae0..55378a78b 100644 --- a/internal/plugin/host.go +++ b/internal/plugin/host.go @@ -748,8 +748,14 @@ func start(ctx context.Context) (*host, error) { return nil, err } h, crashed, err := startOn(ctx, bun) + if crashed { + err = whyDied(ctx, bun, err) + } if err == nil || !crashed || os.Getenv("MAGPIE_BUN") != "" { - return h, err + // a Bun that can't be started at all isn't fallen back from: the + // one before is the same build for this system, and setting this + // one aside would mark a good version bad (#1877) + return h, cantStart(bun, err) } // the Bun magpie last took died starting the host: the one before it, // and the new one set aside when that one starts it diff --git a/internal/plugin/store.go b/internal/plugin/store.go index 5704b6a3e..11c45a081 100644 --- a/internal/plugin/store.go +++ b/internal/plugin/store.go @@ -652,7 +652,10 @@ func install(ctx context.Context, spec string) error { } out, err := bunCommand(ctx, bun, Dir(), "add", "--ignore-scripts", spec).CombinedOutput() if err != nil { - return fmt.Errorf("bun add %s: %v: %s", spec, err, lastLines(string(out), 6)) + if hint := missingLibs(string(out)); hint != "" { + return fmt.Errorf("bun add %s: %s: %w", spec, hint, err) + } + return fmt.Errorf("bun add %s: %v: %s", spec, cantStart(bun, err), lastLines(string(out), 6)) } return nil } @@ -818,7 +821,10 @@ func reinstall(ctx context.Context, spec string) error { } out, err := bunCommand(ctx, bun, Dir(), "update", "--ignore-scripts", name).CombinedOutput() if err != nil { - return fmt.Errorf("bun update %s: %v: %s", name, err, lastLines(string(out), 6)) + if hint := missingLibs(string(out)); hint != "" { + return fmt.Errorf("bun update %s: %s: %w", name, hint, err) + } + return fmt.Errorf("bun update %s: %v: %s", name, cantStart(bun, err), lastLines(string(out), 6)) } return nil }