|
| 1 | +//! Shell environment resolution for GUI applications |
| 2 | +//! |
| 3 | +//! On macOS (and Linux), GUI applications launched from Finder/Dock/Spotlight |
| 4 | +//! inherit a minimal PATH that typically only includes `/usr/bin:/bin:/usr/sbin:/sbin`. |
| 5 | +//! This means tools installed via Homebrew (`/opt/homebrew/bin`), nvm, Volta, fnm, |
| 6 | +//! or standard `/usr/local/bin` are invisible to the app. |
| 7 | +//! |
| 8 | +//! This module resolves the user's full login shell PATH by spawning their default |
| 9 | +//! shell with login flags and reading back `$PATH`. The result is cached for the |
| 10 | +//! lifetime of the process. |
| 11 | +
|
| 12 | +use std::ffi::OsString; |
| 13 | +use std::sync::OnceLock; |
| 14 | +use tracing::{debug, info, warn}; |
| 15 | + |
| 16 | +/// Cached shell PATH, resolved once on first access. |
| 17 | +static SHELL_PATH: OnceLock<Option<OsString>> = OnceLock::new(); |
| 18 | + |
| 19 | +/// Get the user's full shell PATH. |
| 20 | +/// |
| 21 | +/// On Unix (macOS / Linux), this spawns the user's login shell to read the |
| 22 | +/// fully-initialized `$PATH`, including entries added by `.zshrc`, `.bashrc`, |
| 23 | +/// `.profile`, nvm, Volta, Homebrew, etc. |
| 24 | +/// |
| 25 | +/// On Windows, this returns `None` because Windows GUI apps inherit the full |
| 26 | +/// system + user PATH from the registry (no shell sourcing needed). |
| 27 | +/// |
| 28 | +/// The result is cached after the first call. |
| 29 | +pub fn get_shell_path() -> Option<&'static OsString> { |
| 30 | + SHELL_PATH |
| 31 | + .get_or_init(|| { |
| 32 | + #[cfg(unix)] |
| 33 | + { |
| 34 | + resolve_unix_shell_path() |
| 35 | + } |
| 36 | + #[cfg(not(unix))] |
| 37 | + { |
| 38 | + None |
| 39 | + } |
| 40 | + }) |
| 41 | + .as_ref() |
| 42 | +} |
| 43 | + |
| 44 | +/// Resolve the full PATH from the user's login shell on Unix. |
| 45 | +/// |
| 46 | +/// Strategy: |
| 47 | +/// 1. Read `$SHELL` to find the user's default shell (falls back to `/bin/sh`) |
| 48 | +/// 2. Spawn `$SHELL -l -i -c 'printf "%s" "$PATH"'` to get the fully-initialized PATH |
| 49 | +/// - `-l` (login): sources `/etc/profile`, `~/.zprofile` / `~/.bash_profile` |
| 50 | +/// - `-i` (interactive): sources `~/.zshrc` / `~/.bashrc` (where nvm/Volta/fnm init lives) |
| 51 | +/// - `printf` avoids trailing newlines that `echo` might add |
| 52 | +/// 3. If `-i` fails (some shells reject it in non-terminal contexts), retry with just `-l` |
| 53 | +/// 4. Merge the resolved PATH with the current process PATH to avoid losing any entries |
| 54 | +#[cfg(unix)] |
| 55 | +fn resolve_unix_shell_path() -> Option<OsString> { |
| 56 | + let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()); |
| 57 | + info!("[ShellEnv] Resolving PATH from login shell: {}", shell); |
| 58 | + |
| 59 | + // Try interactive login shell first (gets nvm/Volta/fnm paths from .zshrc/.bashrc) |
| 60 | + let shell_path = try_resolve_path_from_shell(&shell, &["-l", "-i", "-c"]).or_else(|| { |
| 61 | + debug!("[ShellEnv] Interactive shell failed, trying login-only"); |
| 62 | + try_resolve_path_from_shell(&shell, &["-l", "-c"]) |
| 63 | + }); |
| 64 | + |
| 65 | + let shell_path = match shell_path { |
| 66 | + Some(p) if !p.is_empty() => p, |
| 67 | + _ => { |
| 68 | + warn!("[ShellEnv] Could not resolve PATH from shell, using process PATH"); |
| 69 | + return None; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + // Merge: shell PATH + current process PATH (to keep any paths the app already has) |
| 74 | + let current_path = std::env::var("PATH").unwrap_or_default(); |
| 75 | + let merged = merge_paths(&shell_path, ¤t_path); |
| 76 | + |
| 77 | + info!( |
| 78 | + "[ShellEnv] Resolved PATH ({} entries, shell had {} entries)", |
| 79 | + merged.split(':').count(), |
| 80 | + shell_path.split(':').count() |
| 81 | + ); |
| 82 | + debug!("[ShellEnv] PATH = {}", merged); |
| 83 | + |
| 84 | + Some(OsString::from(merged)) |
| 85 | +} |
| 86 | + |
| 87 | +/// Try to resolve PATH by running the user's shell with the given flags. |
| 88 | +/// |
| 89 | +/// Uses `printf "%s" "$PATH"` instead of `echo $PATH` to avoid: |
| 90 | +/// - Trailing newlines from echo |
| 91 | +/// - Shell-specific echo behavior differences |
| 92 | +#[cfg(unix)] |
| 93 | +fn try_resolve_path_from_shell(shell: &str, flags: &[&str]) -> Option<String> { |
| 94 | + use std::process::{Command, Stdio}; |
| 95 | + |
| 96 | + // Build command: $SHELL <flags> 'printf "%s" "$PATH"' |
| 97 | + let mut cmd = Command::new(shell); |
| 98 | + for flag in flags { |
| 99 | + cmd.arg(flag); |
| 100 | + } |
| 101 | + cmd.arg(r#"printf "%s" "$PATH""#); |
| 102 | + |
| 103 | + // Prevent the child from inheriting stdin (avoids tty issues) |
| 104 | + cmd.stdin(Stdio::null()); |
| 105 | + cmd.stdout(Stdio::piped()); |
| 106 | + cmd.stderr(Stdio::null()); // Suppress shell startup warnings |
| 107 | + |
| 108 | + match cmd.output() { |
| 109 | + Ok(output) if output.status.success() => { |
| 110 | + let path = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 111 | + if path.is_empty() { |
| 112 | + debug!("[ShellEnv] Shell returned empty PATH"); |
| 113 | + None |
| 114 | + } else { |
| 115 | + Some(path) |
| 116 | + } |
| 117 | + } |
| 118 | + Ok(output) => { |
| 119 | + debug!( |
| 120 | + "[ShellEnv] Shell exited with status {} (flags: {:?})", |
| 121 | + output.status, flags |
| 122 | + ); |
| 123 | + None |
| 124 | + } |
| 125 | + Err(e) => { |
| 126 | + debug!("[ShellEnv] Failed to spawn shell '{}': {}", shell, e); |
| 127 | + None |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// Merge two PATH strings, preserving order and deduplicating. |
| 133 | +/// |
| 134 | +/// The `primary` PATH takes precedence (its entries appear first). |
| 135 | +/// Entries from `secondary` are appended only if not already present. |
| 136 | +#[cfg(unix)] |
| 137 | +fn merge_paths(primary: &str, secondary: &str) -> String { |
| 138 | + use std::collections::HashSet; |
| 139 | + |
| 140 | + let mut seen = HashSet::new(); |
| 141 | + let mut merged = Vec::new(); |
| 142 | + |
| 143 | + for entry in primary.split(':').chain(secondary.split(':')) { |
| 144 | + if !entry.is_empty() && seen.insert(entry.to_string()) { |
| 145 | + merged.push(entry.to_string()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + merged.join(":") |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(test)] |
| 153 | +mod tests { |
| 154 | + use super::*; |
| 155 | + |
| 156 | + #[cfg(unix)] |
| 157 | + #[test] |
| 158 | + fn test_merge_paths_deduplicates() { |
| 159 | + let result = merge_paths("/usr/bin:/usr/local/bin", "/usr/bin:/opt/homebrew/bin"); |
| 160 | + assert_eq!(result, "/usr/bin:/usr/local/bin:/opt/homebrew/bin"); |
| 161 | + } |
| 162 | + |
| 163 | + #[cfg(unix)] |
| 164 | + #[test] |
| 165 | + fn test_merge_paths_primary_order_preserved() { |
| 166 | + let result = merge_paths("/a:/b:/c", "/d:/b:/e"); |
| 167 | + assert_eq!(result, "/a:/b:/c:/d:/e"); |
| 168 | + } |
| 169 | + |
| 170 | + #[cfg(unix)] |
| 171 | + #[test] |
| 172 | + fn test_merge_paths_empty_entries_skipped() { |
| 173 | + let result = merge_paths("/a::/b", ":/c:"); |
| 174 | + assert_eq!(result, "/a:/b:/c"); |
| 175 | + } |
| 176 | + |
| 177 | + #[cfg(unix)] |
| 178 | + #[test] |
| 179 | + fn test_merge_paths_empty_secondary() { |
| 180 | + let result = merge_paths("/a:/b", ""); |
| 181 | + assert_eq!(result, "/a:/b"); |
| 182 | + } |
| 183 | + |
| 184 | + #[cfg(unix)] |
| 185 | + #[test] |
| 186 | + fn test_get_shell_path_returns_something() { |
| 187 | + // On any Unix system with a shell, this should succeed |
| 188 | + let path = get_shell_path(); |
| 189 | + assert!(path.is_some(), "Should resolve shell PATH on Unix"); |
| 190 | + let path_str = path.unwrap().to_string_lossy(); |
| 191 | + assert!( |
| 192 | + path_str.contains("/usr/bin") || path_str.contains("/bin"), |
| 193 | + "PATH should contain standard directories: {}", |
| 194 | + path_str |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
0 commit comments