1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
pub mod runnable;
mod shlex;
pub mod windows;
pub use shlex::{escape_posix_for_single_quotes, shlex_posix, shlex_windows};
use std::env::home_dir;
use std::path::{Path, PathBuf};
use uv_fs::Simplified;
use uv_static::EnvVars;
#[cfg(unix)]
use tracing::debug;
/// Shells for which virtualenv activation scripts are available.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[expect(clippy::doc_markdown)]
pub enum Shell {
/// Bourne Again SHell (bash)
Bash,
/// Friendly Interactive SHell (fish)
Fish,
/// PowerShell
Powershell,
/// Cmd (Command Prompt)
Cmd,
/// Z SHell (zsh)
Zsh,
/// Nushell
Nushell,
/// C SHell (csh)
Csh,
/// Korn SHell (ksh)
Ksh,
}
impl Shell {
/// Determine the user's current shell from the environment.
///
/// First checks shell-specific environment variables (`NU_VERSION`, `FISH_VERSION`,
/// `BASH_VERSION`, `ZSH_VERSION`, `KSH_VERSION`, `PSModulePath`) which are set by the
/// respective shells. This takes priority over `SHELL` because on Unix, `SHELL` refers
/// to the user's login shell, not the currently running shell.
///
/// Falls back to parsing the `SHELL` environment variable if no shell-specific variables
/// are found. On Windows, defaults to PowerShell (or Command Prompt if `PROMPT` is set).
///
/// Returns `None` if the shell cannot be determined.
pub fn from_env() -> Option<Self> {
if std::env::var_os(EnvVars::NU_VERSION).is_some() {
Some(Self::Nushell)
} else if std::env::var_os(EnvVars::FISH_VERSION).is_some() {
Some(Self::Fish)
} else if std::env::var_os(EnvVars::BASH_VERSION).is_some() {
Some(Self::Bash)
} else if std::env::var_os(EnvVars::ZSH_VERSION).is_some() {
Some(Self::Zsh)
} else if std::env::var_os(EnvVars::KSH_VERSION).is_some() {
Some(Self::Ksh)
} else if std::env::var_os(EnvVars::PS_MODULE_PATH).is_some() {
Some(Self::Powershell)
} else if let Some(env_shell) = std::env::var_os(EnvVars::SHELL) {
Self::from_shell_path(env_shell)
} else if cfg!(windows) {
// Command Prompt relies on PROMPT for its appearance whereas PowerShell does not.
// See: https://stackoverflow.com/a/66415037.
if std::env::var_os(EnvVars::PROMPT).is_some() {
Some(Self::Cmd)
} else {
// Fallback to PowerShell if the PROMPT environment variable is not set.
Some(Self::Powershell)
}
} else {
// Fallback to detecting the shell from the parent process
Self::from_parent_process()
}
}
/// Attempt to determine the shell from the parent process.
///
/// This is a fallback method for when environment variables don't provide
/// enough information about the current shell. It looks at the parent process
/// to try to identify which shell is running.
///
/// This method currently only works on Unix-like systems. On other platforms,
/// it returns `None`.
fn from_parent_process() -> Option<Self> {
#[cfg(unix)]
{
// Get the parent process ID
let ppid = nix::unistd::getppid();
debug!("Detected parent process ID: {ppid}");
// Try to read the parent process executable path
let proc_exe_path = format!("/proc/{ppid}/exe");
if let Ok(exe_path) = fs_err::read_link(&proc_exe_path) {
debug!("Parent process executable: {}", exe_path.display());
if let Some(shell) = Self::from_shell_path(&exe_path) {
return Some(shell);
}
}
// If reading exe fails, try reading the comm file
let proc_comm_path = format!("/proc/{ppid}/comm");
if let Ok(comm) = fs_err::read_to_string(&proc_comm_path) {
let comm = comm.trim();
debug!("Parent process comm: {comm}");
if let Some(shell) = parse_shell_from_path(Path::new(comm)) {
return Some(shell);
}
}
debug!("Could not determine shell from parent process");
None
}
#[cfg(not(unix))]
{
None
}
}
/// Parse a shell from a path to the executable for the shell.
///
/// # Examples
///
/// ```ignore
/// use crate::shells::Shell;
///
/// assert_eq!(Shell::from_shell_path("/bin/bash"), Some(Shell::Bash));
/// assert_eq!(Shell::from_shell_path("/usr/bin/zsh"), Some(Shell::Zsh));
/// assert_eq!(Shell::from_shell_path("/opt/my_custom_shell"), None);
/// ```
pub fn from_shell_path(path: impl AsRef<Path>) -> Option<Self> {
parse_shell_from_path(path.as_ref())
}
/// Returns `true` if the shell supports a `PATH` update command.
pub fn supports_update(self) -> bool {
match self {
Self::Powershell | Self::Cmd => true,
shell => !shell.configuration_files().is_empty(),
}
}
/// Return the configuration files that should be modified to append to a shell's `PATH`.
///
/// Some of the logic here is based on rustup's rc file detection.
///
/// See: <https://github.com/rust-lang/rustup/blob/fede22fea7b160868cece632bd213e6d72f8912f/src/cli/self_update/shell.rs#L197>
pub fn configuration_files(self) -> Vec<PathBuf> {
let Some(home_dir) = home_dir() else {
return vec![];
};
match self {
Self::Bash => {
// On Bash, we need to update both `.bashrc` and `.bash_profile`. The former is
// sourced for non-login shells, and the latter is sourced for login shells.
//
// In lieu of `.bash_profile`, shells will also respect `.bash_login` and
// `.profile`, if they exist. So we respect those too.
vec![
[".bash_profile", ".bash_login", ".profile"]
.iter()
.map(|rc| home_dir.join(rc))
.find(|rc| rc.is_file())
.unwrap_or_else(|| home_dir.join(".bash_profile")),
home_dir.join(".bashrc"),
]
}
Self::Ksh => {
// On Ksh it's standard POSIX `.profile` for login shells, and `.kshrc` for non-login.
vec![home_dir.join(".profile"), home_dir.join(".kshrc")]
}
Self::Zsh => {
// On Zsh, we only need to update `.zshenv`. This file is sourced for both login and
// non-login shells. However, we match rustup's logic for determining _which_
// `.zshenv` to use.
//
// See: https://github.com/rust-lang/rustup/blob/fede22fea7b160868cece632bd213e6d72f8912f/src/cli/self_update/shell.rs#L197
let zsh_dot_dir = std::env::var(EnvVars::ZDOTDIR)
.ok()
.filter(|dir| !dir.is_empty())
.map(PathBuf::from);
// Attempt to update an existing `.zshenv` file.
if let Some(zsh_dot_dir) = zsh_dot_dir.as_ref() {
// If `ZDOTDIR` is set, and `ZDOTDIR/.zshenv` exists, then we update that file.
let zshenv = zsh_dot_dir.join(".zshenv");
if zshenv.is_file() {
return vec![zshenv];
}
}
// Whether `ZDOTDIR` is set or not, if `~/.zshenv` exists then we update that file.
let zshenv = home_dir.join(".zshenv");
if zshenv.is_file() {
return vec![zshenv];
}
if let Some(zsh_dot_dir) = zsh_dot_dir.as_ref() {
// If `ZDOTDIR` is set, then we create `ZDOTDIR/.zshenv`.
vec![zsh_dot_dir.join(".zshenv")]
} else {
// If `ZDOTDIR` is _not_ set, then we create `~/.zshenv`.
vec![home_dir.join(".zshenv")]
}
}
Self::Fish => {
// On Fish, we only need to update `config.fish`. This file is sourced for both
// login and non-login shells. However, we must respect Fish's logic, which reads
// from `$XDG_CONFIG_HOME/fish/config.fish` if set, and `~/.config/fish/config.fish`
// otherwise.
if let Some(xdg_home_dir) = std::env::var(EnvVars::XDG_CONFIG_HOME)
.ok()
.filter(|dir| !dir.is_empty())
.map(PathBuf::from)
{
vec![xdg_home_dir.join("fish/config.fish")]
} else {
vec![home_dir.join(".config/fish/config.fish")]
}
}
Self::Csh => {
// On Csh, we need to update both `.cshrc` and `.login`, like Bash.
vec![home_dir.join(".cshrc"), home_dir.join(".login")]
}
// TODO(charlie): Add support for Nushell.
Self::Nushell => vec![],
// See: [`crate::windows::prepend_path`].
Self::Powershell => vec![],
// See: [`crate::windows::prepend_path`].
Self::Cmd => vec![],
}
}
/// Returns `true` if the given path is on the `PATH` in this shell.
pub fn contains_path(path: &Path) -> bool {
let home_dir = home_dir();
std::env::var_os(EnvVars::PATH)
.as_ref()
.iter()
.flat_map(std::env::split_paths)
.map(|path| {
// If the first component is `~`, expand to the home directory.
if let Some(home_dir) = home_dir.as_ref() {
if path
.components()
.next()
.map(std::path::Component::as_os_str)
== Some("~".as_ref())
{
return home_dir.join(path.components().skip(1).collect::<PathBuf>());
}
}
path
})
.any(|p| same_file::is_same_file(path, p).unwrap_or(false))
}
/// Returns the command necessary to prepend a directory to the `PATH` in this shell.
pub fn prepend_path(self, path: &Path) -> Option<String> {
match self {
Self::Nushell => None,
Self::Bash | Self::Zsh | Self::Ksh => Some(format!(
"export PATH=\"{}:$PATH\"",
backslash_escape(&path.simplified_display().to_string()),
)),
Self::Fish => Some(format!(
"fish_add_path \"{}\"",
backslash_escape(&path.simplified_display().to_string()),
)),
Self::Csh => Some(format!(
"setenv PATH \"{}:$PATH\"",
backslash_escape(&path.simplified_display().to_string()),
)),
Self::Powershell => Some(format!(
"$env:PATH = \"{};$env:PATH\"",
backtick_escape(&path.simplified_display().to_string()),
)),
Self::Cmd => Some(format!(
"set PATH=\"{};%PATH%\"",
backslash_escape(&path.simplified_display().to_string()),
)),
}
}
}
impl std::fmt::Display for Shell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bash => write!(f, "Bash"),
Self::Fish => write!(f, "Fish"),
Self::Powershell => write!(f, "PowerShell"),
Self::Cmd => write!(f, "Command Prompt"),
Self::Zsh => write!(f, "Zsh"),
Self::Nushell => write!(f, "Nushell"),
Self::Csh => write!(f, "Csh"),
Self::Ksh => write!(f, "Ksh"),
}
}
}
/// Parse the shell from the name of the shell executable.
fn parse_shell_from_path(path: &Path) -> Option<Shell> {
let name = path.file_stem()?.to_str()?;
match name {
"bash" => Some(Shell::Bash),
"zsh" => Some(Shell::Zsh),
"fish" => Some(Shell::Fish),
"csh" => Some(Shell::Csh),
"ksh" => Some(Shell::Ksh),
"powershell" | "powershell_ise" | "pwsh" => Some(Shell::Powershell),
_ => None,
}
}
/// Escape a string for use in a shell command by inserting backslashes.
fn backslash_escape(s: &str) -> String {
let mut escaped = String::with_capacity(s.len());
for c in s.chars() {
match c {
'\\' | '"' => escaped.push('\\'),
_ => {}
}
escaped.push(c);
}
escaped
}
/// Escape a string for use in a `PowerShell` command by inserting backticks.
fn backtick_escape(s: &str) -> String {
let mut escaped = String::with_capacity(s.len());
for c in s.chars() {
match c {
// Need to also escape unicode double quotes that PowerShell treats
// as the ASCII double quote.
'"' | '`' | '\u{201C}' | '\u{201D}' | '\u{201E}' | '$' => escaped.push('`'),
_ => {}
}
escaped.push(c);
}
escaped
}
#[cfg(test)]
mod tests {
use super::*;
use fs_err::File;
use temp_env::with_vars;
use tempfile::tempdir;
// First option used by std::env::home_dir.
const HOME_DIR_ENV_VAR: &str = if cfg!(windows) {
EnvVars::USERPROFILE
} else {
EnvVars::HOME
};
#[test]
fn configuration_files_zsh_no_existing_zshenv() {
let tmp_home_dir = tempdir().unwrap();
let tmp_zdotdir = tempdir().unwrap();
with_vars(
[
(EnvVars::ZDOTDIR, None),
(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_home_dir.path().join(".zshenv")]
);
},
);
with_vars(
[
(EnvVars::ZDOTDIR, tmp_zdotdir.path().to_str()),
(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_zdotdir.path().join(".zshenv")]
);
},
);
}
#[test]
fn configuration_files_zsh_existing_home_zshenv() {
let tmp_home_dir = tempdir().unwrap();
File::create(tmp_home_dir.path().join(".zshenv")).unwrap();
let tmp_zdotdir = tempdir().unwrap();
with_vars(
[
(EnvVars::ZDOTDIR, None),
(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_home_dir.path().join(".zshenv")]
);
},
);
with_vars(
[
(EnvVars::ZDOTDIR, tmp_zdotdir.path().to_str()),
(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_home_dir.path().join(".zshenv")]
);
},
);
}
#[test]
fn configuration_files_zsh_existing_zdotdir_zshenv() {
let tmp_home_dir = tempdir().unwrap();
let tmp_zdotdir = tempdir().unwrap();
File::create(tmp_zdotdir.path().join(".zshenv")).unwrap();
with_vars(
[
(EnvVars::ZDOTDIR, tmp_zdotdir.path().to_str()),
(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_zdotdir.path().join(".zshenv")]
);
},
);
}
}