-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improve shell PATH hydration and fallback detection #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,77 @@ | ||
| import { | ||
| listLoginShellCandidates, | ||
| mergePathEntries, | ||
| readPathFromLaunchctl, | ||
| readEnvironmentFromLoginShell, | ||
| resolveLoginShell, | ||
| ShellEnvironmentReader, | ||
| } from "@t3tools/shared/shell"; | ||
|
|
||
| const LOGIN_SHELL_ENV_NAMES = [ | ||
| "PATH", | ||
| "SSH_AUTH_SOCK", | ||
| "HOMEBREW_PREFIX", | ||
| "HOMEBREW_CELLAR", | ||
| "HOMEBREW_REPOSITORY", | ||
| "XDG_CONFIG_HOME", | ||
| "XDG_DATA_HOME", | ||
| ] as const; | ||
|
|
||
| function logShellEnvironmentWarning(message: string, error?: unknown): void { | ||
| console.warn(`[desktop] ${message}`, error instanceof Error ? error.message : (error ?? "")); | ||
| } | ||
|
|
||
| export function syncShellEnvironment( | ||
| env: NodeJS.ProcessEnv = process.env, | ||
| options: { | ||
| platform?: NodeJS.Platform; | ||
| readEnvironment?: ShellEnvironmentReader; | ||
| readLaunchctlPath?: typeof readPathFromLaunchctl; | ||
| userShell?: string; | ||
| logWarning?: (message: string, error?: unknown) => void; | ||
| } = {}, | ||
| ): void { | ||
| const platform = options.platform ?? process.platform; | ||
| if (platform !== "darwin" && platform !== "linux") return; | ||
|
|
||
| try { | ||
| const shell = resolveLoginShell(platform, env.SHELL); | ||
| if (!shell) return; | ||
| const logWarning = options.logWarning ?? logShellEnvironmentWarning; | ||
| const readEnvironment = options.readEnvironment ?? readEnvironmentFromLoginShell; | ||
| const shellEnvironment: Partial<Record<string, string>> = {}; | ||
|
|
||
| const shellEnvironment = (options.readEnvironment ?? readEnvironmentFromLoginShell)(shell, [ | ||
| "PATH", | ||
| "SSH_AUTH_SOCK", | ||
| ]); | ||
| try { | ||
| for (const shell of listLoginShellCandidates(platform, env.SHELL, options.userShell)) { | ||
| try { | ||
| Object.assign(shellEnvironment, readEnvironment(shell, LOGIN_SHELL_ENV_NAMES)); | ||
| if (shellEnvironment.PATH) { | ||
| break; | ||
| } | ||
| } catch (error) { | ||
| logWarning(`Failed to read login shell environment from ${shell}.`, error); | ||
| } | ||
| } | ||
|
|
||
| if (shellEnvironment.PATH) { | ||
| env.PATH = shellEnvironment.PATH; | ||
| const launchctlPath = | ||
| platform === "darwin" ? (options.readLaunchctlPath ?? readPathFromLaunchctl)() : undefined; | ||
| const mergedPath = mergePathEntries(shellEnvironment.PATH ?? launchctlPath, env.PATH, platform); | ||
|
Comment on lines
+52
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium On Darwin, - const launchctlPath =
- platform === "darwin" ? (options.readLaunchctlPath ?? readPathFromLaunchctl)() : undefined;
+ const launchctlPath =
+ platform === "darwin" && !shellEnvironment.PATH ? (options.readLaunchctlPath ?? readPathFromLaunchctl)() : undefined;🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| if (mergedPath) { | ||
| env.PATH = mergedPath; | ||
| } | ||
|
|
||
| if (!env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) { | ||
| env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK; | ||
| } | ||
|
|
||
| for (const name of [ | ||
| "HOMEBREW_PREFIX", | ||
| "HOMEBREW_CELLAR", | ||
| "HOMEBREW_REPOSITORY", | ||
| "XDG_CONFIG_HOME", | ||
| "XDG_DATA_HOME", | ||
| ] as const) { | ||
| if (!env[name] && shellEnvironment[name]) { | ||
| env[name] = shellEnvironment[name]; | ||
| } | ||
| } | ||
| } catch { | ||
| // Keep inherited environment if shell lookup fails. | ||
| logWarning("Failed to synchronize the desktop shell environment."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outer catch discards error details unlike server counterpartLow Severity The outer Additional Locations (1)Reviewed by Cursor Bugbot for commit 0d649e0. Configure here. |
||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary synchronous subprocess spawned on macOS every time
Medium Severity
readPathFromLaunchctl()is called unconditionally on macOS, spawning a synchronous subprocess viaexecFileSynceven when the login shell already provided aPATH. The result is only used as a fallback (shellEnvironment.PATH ?? launchctlPath), but the child process is always spawned. This blocks the main thread unnecessarily on every startup in the common happy-path case.Additional Locations (1)
apps/server/src/os-jank.ts#L44-L46Reviewed by Cursor Bugbot for commit 0d649e0. Configure here.