Skip to content

Commit 7217838

Browse files
fix(desktop): add existing CLI dirs to PATH when shell probes return none
When neither the login shell nor launchctl yields a PATH, append the existing Homebrew bin, /usr/local/bin and ~/.local/bin after the inherited PATH, so a Finder launch can still find CLIs without shadowing a caller-chosen nvm/mise/fnm binary. The desktop and server share one helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 982dccf commit 7217838

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
buildEnvironmentCaptureCommand,
3+
listExistingPosixCliDirs,
34
listLoginShellCandidates,
45
setWindowsPath,
56
} from "@t3tools/shared/shell";
@@ -415,9 +416,13 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
415416
config.platform === "darwin" && !shellEnvironment.PATH
416417
? yield* readLaunchctlPath
417418
: Option.none<string>();
419+
const probedPath = trimNonEmpty(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath));
418420
const mergedPath = mergePaths(config.platform, [
419-
trimNonEmpty(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath)),
421+
probedPath,
420422
readEnvPath(config.env),
423+
Option.isNone(probedPath)
424+
? trimNonEmpty(listExistingPosixCliDirs(config.env, config.platform).join(":"))
425+
: Option.none(),
421426
]);
422427

423428
if (Option.isSome(mergedPath)) {

‎apps/server/src/os-jank.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as HostProcess from "@t3tools/shared/HostProcess";
22
import {
3+
listExistingPosixCliDirs,
34
listLoginShellCandidates,
45
mergePathEntries,
56
readEnvironmentFromLoginShell,
@@ -35,7 +36,10 @@ function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): vo
3536

3637
const shellPath = shellEnvironment.PATH;
3738
const launchctlPath = platform === "darwin" && !shellPath ? readPathFromLaunchctl() : undefined;
38-
const mergedPath = mergePathEntries(shellPath ?? launchctlPath, env.PATH, platform);
39+
const probedPath = shellPath ?? launchctlPath;
40+
const mergedPath = probedPath
41+
? mergePathEntries(probedPath, env.PATH, platform)
42+
: mergePathEntries(env.PATH, listExistingPosixCliDirs(env, platform).join(":"), platform);
3943
if (mergedPath) {
4044
env.PATH = mergedPath;
4145
}

‎packages/shared/src/shell.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,30 @@ export function mergePathEntries(
198198
return merged.length > 0 ? merged.join(delimiter) : undefined;
199199
}
200200

201+
function isDirectory(dirPath: string): boolean {
202+
try {
203+
return NodeFS.statSync(dirPath).isDirectory();
204+
} catch {
205+
return false;
206+
}
207+
}
208+
209+
// Last resort when no PATH probe answers: a slow profile trips the login-shell
210+
// timeout, and `launchctl getenv PATH` is unset on recent macOS, which leaves a
211+
// Finder launch on the bare `/usr/bin:/bin:/usr/sbin:/sbin` PATH. Callers append
212+
// these after the inherited PATH so a caller-chosen nvm/mise/fnm binary still wins.
213+
export function listExistingPosixCliDirs(
214+
env: NodeJS.ProcessEnv,
215+
platform: NodeJS.Platform,
216+
): ReadonlyArray<string> {
217+
const home = trimNonEmpty(env.HOME);
218+
return [
219+
platform === "darwin" ? "/opt/homebrew/bin" : "/home/linuxbrew/.linuxbrew/bin",
220+
"/usr/local/bin",
221+
...(home ? [`${home}/.local/bin`] : []),
222+
].filter(isDirectory);
223+
}
224+
201225
function envCaptureStart(name: string): string {
202226
return `__T3CODE_ENV_${name}_START__`;
203227
}

0 commit comments

Comments
 (0)