Skip to content

The agent that grows with you

fix(desktop): probe non-Windows git and gh candidates for executability

Konstantin Khlopkov57f2da675aa3
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index fd83994d2e3..2333038e0ab 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -389,6 +389,7 @@ import {
type SecretStoragePolicy,
writeSecretStoragePolicy
} from './secret-storage-policy'
+import { selectRunnableBinary } from './select-runnable-binary'
import {
buildInstanceWindowUrl,
buildSessionWindowUrl,
@@ -2948,13 +2949,59 @@ function makeDashboardReadyFile() {
// standard Git-for-Windows locations, then PATH. Cached after first probe.
let _gitBinaryCache = null
+// A binary can exist on disk and still be unlaunchable — on macOS an
+// Intel-only build ahead on PATH (e.g. a pre-Rosetta-removal Homebrew)
+// fails at spawn time with errno -86 (EBADARCH), which callers then report
+// as an update-server/network problem. Probing `git --version` before
+// committing to a candidate skips such entries; the existence-only
+// fallback keeps behaviour unchanged where the probe itself cannot run.
+function binaryRuns(candidate) {
+ try {
+ execFileSync(candidate, ['--version'], {
+ stdio: 'ignore',
+ timeout: 5000,
+ windowsHide: true
+ })
+
+ return true
+ } catch {
+ return false
+ }
+}
+
+function findPathCandidates(command) {
+ const pathEntries = String(process.env.PATH || '')
+ .split(path.delimiter)
+ .filter(Boolean)
+
+ const candidates = []
+
+ for (const entry of pathEntries) {
+ const candidate = path.join(entry, command)
+
+ if (fileExists(candidate)) {
+ candidates.push(candidate)
+ }
+ }
+
+ return candidates
+}
+
function resolveGitBinary() {
if (_gitBinaryCache) {
return _gitBinaryCache
}
if (!IS_WINDOWS) {
- _gitBinaryCache = findOnPath('git') || 'git'
+ // Every PATH hit, probed — the first entry that merely exists can be
+ // unlaunchable while a working system git sits later on the same PATH.
+ const selected = selectRunnableBinary({
+ candidates: findPathCandidates('git'),
+ fileExists,
+ binaryRuns
+ })
+
+ _gitBinaryCache = selected || 'git'
return _gitBinaryCache
}
@@ -3003,7 +3050,17 @@ function resolveGhBinary() {
candidates.push('/opt/homebrew/bin/gh', '/usr/local/bin/gh', '/usr/bin/gh', path.join(home, '.local', 'bin', 'gh'))
}
- _ghBinaryCache = candidates.find(fileExists) || findOnPath('gh') || 'gh'
+ // Same selection rule as git: an existing-but-unlaunchable candidate (e.g.
+ // an Intel-only build from a stale Homebrew) must not shadow a working one
+ // further down the list, and PATH is only consulted when none of the
+ // explicit candidates is usable.
+ const selected = selectRunnableBinary({
+ candidates,
+ fileExists,
+ binaryRuns
+ })
+
+ _ghBinaryCache = selected || findOnPath('gh') || 'gh'
return _ghBinaryCache
}
diff --git a/apps/desktop/electron/select-runnable-binary.test.ts b/apps/desktop/electron/select-runnable-binary.test.ts
new file mode 100644
index 00000000000..6552cd03d9c
--- /dev/null
+++ b/apps/desktop/electron/select-runnable-binary.test.ts
@@ -0,0 +1,58 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { selectRunnableBinary } from './select-runnable-binary'
+
+const yes = () => true
+const no = () => false
+
+test.each([
+ {
+ // The reported machine: Intel-only /usr/local/bin/git ahead on PATH of a
+ // working /usr/bin/git — it exists, so existence-only selection commits to
+ // it, and it fails at spawn with errno -86 (Bad CPU type in executable).
+ name: 'an existing but unlaunchable earlier candidate is skipped for a later one that runs',
+ candidates: ['/usr/local/bin/git', '/usr/bin/git'],
+ binaryRuns: (p: string) => p === '/usr/bin/git',
+ expected: '/usr/bin/git'
+ },
+ {
+ name: 'the first candidate wins when it both exists and runs',
+ candidates: ['/opt/homebrew/bin/gh', '/usr/local/bin/gh'],
+ binaryRuns: yes,
+ expected: '/opt/homebrew/bin/gh'
+ },
+ {
+ // Preserves pre-probe behaviour where the probe itself cannot run
+ // (locked-down execution policy, AV interposing on spawn) instead of
+ // skipping a binary that would have worked.
+ name: 'when no candidate runs, fall back to the first that exists',
+ candidates: ['/usr/local/bin/git', '/usr/bin/git'],
+ binaryRuns: no,
+ expected: '/usr/local/bin/git'
+ }
+])('$name', ({ candidates, binaryRuns, expected }) => {
+ assert.equal(selectRunnableBinary({ candidates, fileExists: yes, binaryRuns }), expected)
+})
+
+test('missing candidates are never probed, and nothing existing yields null for the caller fallback', () => {
+ // Probing a non-existent path would cost a failed spawn per candidate.
+ const probed: string[] = []
+
+ const result = selectRunnableBinary({
+ candidates: ['/opt/homebrew/bin/gh', '/usr/local/bin/gh', '/usr/bin/gh'],
+ fileExists: (p: string) => p === '/usr/bin/gh',
+ binaryRuns: (p: string) => {
+ probed.push(p)
+
+ return true
+ }
+ })
+
+ assert.equal(result, '/usr/bin/gh')
+ assert.deepEqual(probed, ['/usr/bin/gh'])
+
+ assert.equal(selectRunnableBinary({ candidates: ['/opt/homebrew/bin/gh'], fileExists: no, binaryRuns: no }), null)
+ assert.equal(selectRunnableBinary({ candidates: [], fileExists: yes, binaryRuns: yes }), null)
+})
diff --git a/apps/desktop/electron/select-runnable-binary.ts b/apps/desktop/electron/select-runnable-binary.ts
new file mode 100644
index 00000000000..ba526d3d9f8
--- /dev/null
+++ b/apps/desktop/electron/select-runnable-binary.ts
@@ -0,0 +1,34 @@
+export interface RunnableBinaryOptions {
+ /** Ordered candidate absolute paths, highest preference first. */
+ candidates: string[]
+ fileExists: (filePath: string) => boolean
+ /** Probe: does this candidate actually execute (`<binary> --version`)? */
+ binaryRuns: (filePath: string) => boolean
+}
+
+/**
+ * Pick the first candidate that both exists on disk and runs, falling back to
+ * the first that merely exists, then to null.
+ *
+ * A file can exist and still be unlaunchable: on macOS an Intel-only binary
+ * ahead on PATH (Homebrew under Rosetta gone after an OS update) fails at
+ * spawn time with errno -86 (EBADARCH, "Bad CPU type in executable"), which
+ * callers then misreport as a network/update-server problem because the
+ * failure only surfaces when the child is spawned. Existence-only selection
+ * (`candidates.find(fileExists)`, first PATH hit) commits to the broken
+ * candidate by construction and never looks at the working one right after it.
+ *
+ * The existence-only fallback keeps behaviour unchanged where the probe itself
+ * cannot run (locked-down execution policy, AV interposing on spawn) rather
+ * than skipping a binary that would have worked.
+ *
+ * Resolution order (first match wins):
+ * 1. a candidate that exists and runs
+ * 2. a candidate that merely exists
+ * 3. null — caller falls back to its own PATH/bare-name resolution
+ */
+export function selectRunnableBinary(opts: RunnableBinaryOptions): string | null {
+ const existing = opts.candidates.filter(opts.fileExists)
+
+ return existing.find(opts.binaryRuns) || existing[0] || null
+}