-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d649e0
Improve shell PATH hydration on desktop and server
juliusmarminge 526ff40
fix: defer launchctl subprocess, remove dead resolveLoginShell, captu…
cursoragent 02f0753
Merge branch 'main' into t3code/brew-provider-detection
juliusmarminge 99b6d62
fix: defer launchctl subprocess, remove dead resolveLoginShell, captu…
cursor[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,79 @@ | ||
| 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" && !shellEnvironment.PATH | ||
| ? (options.readLaunchctlPath ?? readPathFromLaunchctl)() | ||
| : undefined; | ||
| const mergedPath = mergePathEntries(shellEnvironment.PATH ?? launchctlPath, env.PATH, platform); | ||
| if (mergedPath) { | ||
| env.PATH = mergedPath; | ||
| } | ||
|
|
||
| if (!env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) { | ||
| env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK; | ||
| } | ||
| } catch { | ||
| // Keep inherited environment if shell lookup fails. | ||
|
|
||
| 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 (error) { | ||
| logWarning("Failed to synchronize the desktop shell environment.", error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.