diff --git a/internal/proc/appmain.go b/internal/proc/appmain.go new file mode 100644 index 000000000..16d564367 --- /dev/null +++ b/internal/proc/appmain.go @@ -0,0 +1,71 @@ +package proc + +import ( + "errors" + "os" + "path/filepath" + "regexp" + "strings" +) + +// errAppMain is what a probe of an app's own program ends with, unrun. +var errAppMain = errors.New("not run: it is a macOS app's own program, which opens the app's window") + +// appMain reports whether the program at path is the main executable of +// an app a user opens (…/X.app/Contents/MacOS/, X.app +// in no other bundle), after links. Run, that opens the app: its Dock icon +// and window, whatever the arguments, and a GUI app given --version never +// answers it. An `opencode` on PATH that resolves to OpenCode.app's own +// program (a folder of the bundle on PATH, where a Mac's case-insensitive +// disk finds OpenCode as opencode, or a link to it) put OpenCode's window +// up each time magpie asked its version, at every start and after each +// update (saint carol on Discord). The bundle's other programs (OpenCode's +// opencode-cli, Kiro CLI.app's kiro-cli beside its kiro_cli_desktop) are +// CLIs and are run, and so is a bundle inside an app's own: ChatGPT.app's +// codex is CodexCLI.app's main executable. When Info.plist can't be read +// (a binary one, as Kiro CLI.app's is), the program named as its bundle +// is taken for the main one. +func appMain(path string) bool { + if path == "" { + return false + } + if real, err := filepath.EvalSymlinks(path); err == nil { + path = real + } + if abs, err := filepath.Abs(path); err == nil { + path = abs + } + macos := filepath.Dir(path) + contents := filepath.Dir(macos) + bundle := filepath.Dir(contents) + if !strings.EqualFold(filepath.Base(macos), "MacOS") || !strings.EqualFold(filepath.Base(contents), "Contents") || + !strings.HasSuffix(strings.ToLower(filepath.Base(bundle)), ".app") { + return false + } + for up := filepath.Dir(bundle); up != filepath.Dir(up); up = filepath.Dir(up) { + if strings.HasSuffix(strings.ToLower(filepath.Base(up)), ".app") { + return false // a helper or a CLI another app ships + } + } + exe := bundleExecutable(filepath.Join(contents, "Info.plist")) + if exe == "" { + exe = filepath.Base(bundle) + exe = exe[:len(exe)-len(".app")] + } + return strings.EqualFold(exe, filepath.Base(path)) +} + +var bundleExeRe = regexp.MustCompile(`CFBundleExecutable\s*([^<]+)`) + +// bundleExecutable is the CFBundleExecutable an XML Info.plist names, "" +// when it can't be read (a binary plist too) or names none. +func bundleExecutable(plist string) string { + b, err := os.ReadFile(plist) + if err != nil { + return "" + } + if m := bundleExeRe.FindSubmatch(b); m != nil { + return strings.TrimSpace(string(m[1])) + } + return "" +} diff --git a/internal/proc/appmain_test.go b/internal/proc/appmain_test.go new file mode 100644 index 000000000..e30ac7b74 --- /dev/null +++ b/internal/proc/appmain_test.go @@ -0,0 +1,163 @@ +//go:build !windows + +package proc + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/testenv" +) + +// openCodeBundle lays out OpenCode.app as its desktop app ships on a Mac: +// its own program OpenCode (the window), named by Info.plist's +// CFBundleExecutable, and a CLI beside it. Each program writes its name to +// ran when it is started. +func openCodeBundle(t *testing.T, plist string) (macos, ran string) { + dir := t.TempDir() + ran = filepath.Join(dir, "ran") + contents := filepath.Join(dir, "Applications", "OpenCode.app", "Contents") + macos = filepath.Join(contents, "MacOS") + if err := os.MkdirAll(macos, 0o755); err != nil { + t.Fatal(err) + } + if plist != "" { + if err := os.WriteFile(filepath.Join(contents, "Info.plist"), []byte(plist), 0o644); err != nil { + t.Fatal(err) + } + } + testenv.Program(t, filepath.Join(macos, "OpenCode"), "#!/bin/sh\necho window >> '"+ran+"'\n") + testenv.Program(t, filepath.Join(macos, "opencode-cli"), "#!/bin/sh\necho cli >> '"+ran+"'\necho 2.2.5\n") + return macos, ran +} + +const openCodePlist = ` + + CFBundleExecutable + OpenCode + CFBundleIdentifier + ai.opencode.desktop +` + +func probeVersion(t *testing.T, bin string) (string, error) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + out, err := ProbeContext(ctx, bin, "--version").Output() + return strings.TrimSpace(string(out)), err +} + +func ranOf(t *testing.T, ran string) string { + b, err := os.ReadFile(ran) + if err != nil && !os.IsNotExist(err) { + t.Fatal(err) + } + return strings.TrimSpace(string(b)) +} + +// An opencode that is OpenCode.app's own program, reached through a link +// or a folder of the bundle on PATH, is never run by a version probe: run, +// it opens OpenCode's window, which magpie's version check did at each +// start and after each update (saint carol on Discord). The CLI beside it +// in the bundle is still asked. +func TestProbeNeverOpensAnApp(t *testing.T) { + macos, ran := openCodeBundle(t, openCodePlist) + bin := filepath.Join(t.TempDir(), "opencode") + if err := os.Symlink(filepath.Join(macos, "OpenCode"), bin); err != nil { + t.Fatal(err) + } + if _, err := probeVersion(t, bin); err == nil { + t.Fatal("a link to the app's own program was run") + } + if v := Version(bin); v != "" { + t.Fatalf("the app's own program said version %q", v) + } + // PATH holding the bundle's folder: on a Mac's case-insensitive disk + // "opencode" finds OpenCode itself + if _, err := os.Stat(filepath.Join(macos, "opencode")); err == nil { + t.Setenv("PATH", macos) + if _, err := probeVersion(t, "opencode"); err == nil { + t.Fatal("opencode found in the bundle's folder was run") + } + } + if got := ranOf(t, ran); got != "" { + t.Fatalf("a probe started %q", got) + } + if v, err := probeVersion(t, filepath.Join(macos, "opencode-cli")); err != nil || v != "2.2.5" { + t.Fatalf("the bundle's CLI answered %q, %v", v, err) + } + if got := ranOf(t, ran); got != "cli" { + t.Fatalf("ran %q, want only the CLI", got) + } +} + +// A bundle whose Info.plist can't be read (none, or a binary one, as Kiro +// CLI.app's is): the program named as the bundle is its main one and is +// not run, the CLI beside it is. +func TestProbeUnreadPlistByName(t *testing.T) { + for name, plist := range map[string]string{"none": "", "binary": "bplist00\x01\x02"} { + t.Run(name, func(t *testing.T) { + macos, ran := openCodeBundle(t, plist) + if _, err := probeVersion(t, filepath.Join(macos, "OpenCode")); err == nil { + t.Error("OpenCode was run") + } + if v, err := probeVersion(t, filepath.Join(macos, "opencode-cli")); err != nil || v != "2.2.5" { + t.Errorf("the bundle's CLI answered %q, %v", v, err) + } + if got := ranOf(t, ran); got != "cli" { + t.Fatalf("ran %q, want only the CLI", got) + } + }) + } +} + +// ChatGPT.app ships its codex as CodexCLI.app's main executable, inside +// its own bundle: a CLI, and asked its version (Info.plist as 26.1027's). +func TestProbeRunsANestedAppsCLI(t *testing.T) { + dir := t.TempDir() + contents := filepath.Join(dir, "ChatGPT.app", "Contents", "Resources", "codex-cli", "CodexCLI.app", "Contents") + if err := os.MkdirAll(filepath.Join(contents, "MacOS"), 0o755); err != nil { + t.Fatal(err) + } + plist := ` + + CFBundleDisplayNameChatGPT + CFBundleExecutablecodex + CFBundleIdentifiercom.openai.codex.cli + CFBundleNameChatGPT + CFBundlePackageTypeAPPL + CFBundleVersion1 +` + if err := os.WriteFile(filepath.Join(contents, "Info.plist"), []byte(plist), 0o644); err != nil { + t.Fatal(err) + } + codex := filepath.Join(contents, "MacOS", "codex") + testenv.Program(t, codex, "#!/bin/sh\necho codex-cli 0.166.0\n") + if v, err := probeVersion(t, codex); err != nil || v != "codex-cli 0.166.0" { + t.Fatalf("ChatGPT's codex answered %q, %v", v, err) + } +} + +// A program outside any bundle, or in a folder only named like one, runs. +func TestAppMainOnlyBundles(t *testing.T) { + dir := t.TempDir() + for _, p := range []string{ + filepath.Join(dir, "bin", "opencode"), + filepath.Join(dir, "MacOS", "opencode"), + filepath.Join(dir, "Contents", "MacOS", "opencode"), + filepath.Join(dir, "x.apps", "Contents", "MacOS", "opencode"), + filepath.Join(dir, "X.app", "Contents", "MacOS", "x-cli"), + filepath.Join(dir, "A.app", "Contents", "Frameworks", "X.app", "Contents", "MacOS", "X"), + } { + if appMain(p) { + t.Errorf("%s taken for an app's own program", p) + } + } + if !appMain(filepath.Join(dir, "X.app", "Contents", "MacOS", "X")) { + t.Error("a bundle with no Info.plist: the program named as it was taken for a CLI") + } +} diff --git a/internal/proc/probe.go b/internal/proc/probe.go index 1a23c7f04..6e3f3abdd 100644 --- a/internal/proc/probe.go +++ b/internal/proc/probe.go @@ -20,6 +20,11 @@ func ProbeContext(ctx context.Context, name string, args ...string) *exec.Cmd { ctx, cancel := context.WithCancel(ctx) p := &probe{cancel: cancel, killing: make(chan struct{}), ended: make(chan struct{})} cmd := CommandContext(ctx, name, args...) + // a probe never opens an app: asked its version, an app's own program + // puts its window up instead (appMain) + if cmd.Err == nil && appMain(cmd.Path) { + cmd.Err = errAppMain + } probeGroup(cmd, p) probes.Lock() probes.m[p] = struct{}{}