Skip to content

Commit 9986640

Browse files
committed
fix(desktop): resolve full shell PATH on macOS before spawning sidecar
On macOS, Electron apps launched from Finder/Dock inherit a minimal launchd PATH (/usr/bin:/bin:/usr/sbin:/sbin). Most users configure PATH in ~/.zshrc, which is only sourced by interactive shells. The sidecar was spawned with `zsh -l -c` (login, non-interactive), which skips .zshrc entirely — causing tools like bun, mise, Homebrew binaries, etc. to be missing from bash tool execution. Add shell-env.ts that spawns an interactive login shell (`-ilc`) at startup to capture the real PATH via marker-delimited printenv, then applies it to process.env before the sidecar is spawned. Closes anomalyco#15565 Closes anomalyco#7519
1 parent 14f9e21 commit 9986640

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎packages/desktop-electron/src/main/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { InitStep, ServerReadyData, SqliteMigrationProgress, WslConfig } fr
2626
import { checkAppExists, resolveAppPath, wslPath } from "./apps"
2727
import type { CommandChild } from "./cli"
2828
import { installCli, syncCli } from "./cli"
29+
import { fixPath } from "./shell-env"
2930
import { CHANNEL, UPDATER_ENABLED } from "./constants"
3031
import { registerIpcHandlers, sendDeepLinks, sendMenuCommand, sendSqliteMigrationProgress } from "./ipc"
3132
import { initLogging } from "./logging"
@@ -94,6 +95,7 @@ function setupApp() {
9495

9596
void app.whenReady().then(async () => {
9697
// migrate()
98+
fixPath()
9799
app.setAsDefaultProtocolClient("opencode")
98100
setDockIcon()
99101
setupAutoUpdater()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { execFileSync } from "node:child_process"
2+
3+
const ENV_NAME_PATTERN = /^[A-Z0-9_]+$/
4+
5+
function envCaptureStart(name: string): string {
6+
return `__OPENCODE_ENV_${name}_START__`
7+
}
8+
9+
function envCaptureEnd(name: string): string {
10+
return `__OPENCODE_ENV_${name}_END__`
11+
}
12+
13+
function buildCaptureCommand(names: ReadonlyArray<string>): string {
14+
return names
15+
.map((name) => {
16+
if (!ENV_NAME_PATTERN.test(name)) {
17+
throw new Error(`Unsupported environment variable name: ${name}`)
18+
}
19+
20+
return [
21+
`printf '%s\\n' '${envCaptureStart(name)}'`,
22+
`printenv ${name} || true`,
23+
`printf '%s\\n' '${envCaptureEnd(name)}'`,
24+
].join("; ")
25+
})
26+
.join("; ")
27+
}
28+
29+
function extractValue(output: string, name: string): string | undefined {
30+
const startMarker = envCaptureStart(name)
31+
const endMarker = envCaptureEnd(name)
32+
const startIdx = output.indexOf(startMarker)
33+
if (startIdx === -1) return undefined
34+
35+
const valueStart = startIdx + startMarker.length
36+
const endIdx = output.indexOf(endMarker, valueStart)
37+
if (endIdx === -1) return undefined
38+
39+
let value = output.slice(valueStart, endIdx)
40+
if (value.startsWith("\n")) value = value.slice(1)
41+
if (value.endsWith("\n")) value = value.slice(0, -1)
42+
43+
return value.length > 0 ? value : undefined
44+
}
45+
46+
export function readEnvironmentFromLoginShell(
47+
shell: string,
48+
names: ReadonlyArray<string>,
49+
): Partial<Record<string, string>> {
50+
if (names.length === 0) return {}
51+
52+
const output = execFileSync(shell, ["-ilc", buildCaptureCommand(names)], {
53+
encoding: "utf8",
54+
timeout: 5000,
55+
})
56+
57+
const environment: Partial<Record<string, string>> = {}
58+
for (const name of names) {
59+
const value = extractValue(output, name)
60+
if (value !== undefined) {
61+
environment[name] = value
62+
}
63+
}
64+
65+
return environment
66+
}
67+
68+
/**
69+
* On macOS, Electron apps launched from Finder/Dock inherit a minimal launchd
70+
* PATH (/usr/bin:/bin:/usr/sbin:/sbin). Most users configure PATH in ~/.zshrc
71+
* which is only sourced by interactive shells. This resolves the full
72+
* interactive login shell PATH and applies it to process.env so the sidecar
73+
* and all child processes see the user's installed tools.
74+
*/
75+
export function fixPath() {
76+
if (process.platform !== "darwin") return
77+
78+
const shell = process.env.SHELL || "/bin/zsh"
79+
try {
80+
const result = readEnvironmentFromLoginShell(shell, ["PATH"])
81+
if (result.PATH) {
82+
console.log(`[shell-env] fixPath: resolved PATH with ${result.PATH.split(":").length} entries`)
83+
process.env.PATH = result.PATH
84+
}
85+
} catch (error) {
86+
console.warn("[shell-env] fixPath: failed to resolve shell PATH", error)
87+
}
88+
}

0 commit comments

Comments
 (0)