Files
magpie/internal/proc/appmain.go
T
yetone b2f7818d2e proc: a probe never runs a Windows GUI program, so asking opencode's version doesn't open OpenCode there either (saint carol)
saint carol, on Windows with OpenCode's desktop app: opening magpie,
often right after an update, brings up OpenCode. af0cd09c9 covered only a
Mac's app bundles.

OpenCode's desktop app installs per user, to
%LOCALAPPDATA%\Programs\ai.opencode.desktop\OpenCode.exe (electron-builder
NSIS; @opencodedesktop before), with its CLI at
resources\opencode-cli.exe. The installer puts nothing on PATH, and
"Install CLI" is Mac-only. With that folder on PATH, `opencode` finds
OpenCode.exe, since Windows looks names up without case and PATHEXT adds
.exe. magpie's `opencode --version` at each start and after a CLI update
(proc.Version, library openCodeVersion, cliupdate runCheck) then started
the Electron app. Nothing else in magpie starts OpenCode on Windows: no
App Paths lookup, no opencode:// link, and scoop's opencode-desktop gives
a shortcut, not a shim.

ProbeContext now also refuses (cmd.Err, nothing started) a program whose
PE header names the GUI subsystem (guiExe: e_lfanew, PE signature,
optional header Subsystem == 2). Console programs run as before: CLIs,
bun's, npm's and WinGet's shims, opencode-cli.exe. Anything that isn't a
PE, or whose header can't be read, also runs as before. On the Windows
box, every program magpie probes that is installed there is console
(claude.exe, codex.exe, git.exe). The GUI ones on PATH are system32
tools, git-gui, gitk and Codex's codex-command-runner, which no probe
asks.

Tests:
- TestGuiPE: GUI PE32 and PE32+ headers are a window's program; console,
  a bad magic, a cut-short header, a script and empty bytes are not.
- TestProbeNeverOpensAWindowsApp (Windows): the install's layout is made
  from copies of the test binary, which notes that it ran (TestMain's
  MAGPIE_PROC_RAN). OpenCode.exe is patched to the GUI subsystem as
  Electron's is. With the folder on PATH, `opencode` resolves to
  OpenCode.exe and isn't run, and Version is "". opencode-cli.exe still
  answers 2.2.5.

Broken by hand on xiaomi-gpu: with guiExe out of ProbeContext,
TestProbeNeverOpensAWindowsApp goes red ("OpenCode.exe was run and said
2.2.5").

Run at 4b407bb79 plus this change (go test -count=1 -timeout 45m):
- mote (macOS): internal/proc, library, agent, provider, wslrun ok;
  GOOS=windows go vet ./... ok, GOOS=linux go vet -tags nogui ./... ok;
  gofmt -l empty.
- xiaomi-gpu (Windows, win-run): internal/proc ok. internal/library red
  on TestGoneAgentListedToTakeOut and TestCheckMixedServerEachSide, the
  same at 4b407bb79 without this change, both listed in #4115.
- magpie-desktop (Linux, wsl-run): internal/proc ok.

Not tried: the real OpenCode desktop app on Windows (not installed on the
test box; the layout is from OpenCode's electron-builder config and NSIS
include), the reporter's PATH, and a .cmd or .ps1 the user wrote that
starts OpenCode.exe (still run).
2026-11-02 16:24:51 +08:00

73 lines
2.6 KiB
Go

package proc
import (
"errors"
"os"
"path/filepath"
"regexp"
"strings"
)
// errAppMain is what a probe of an app's own program (a Mac bundle's, a
// Windows GUI program) ends with, unrun.
var errAppMain = errors.New("not run: it is an 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/<CFBundleExecutable>, 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(`<key>CFBundleExecutable</key>\s*<string>([^<]+)</string>`)
// 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 ""
}