-
Notifications
You must be signed in to change notification settings - Fork 958
Implement shell PATH fixes via UnixShell trait #2387
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
Changes from all commits
4451be6
bfacec9
f7296b7
86c97e4
c300af2
850adef
13bd1f2
de4bf2f
e138ce8
353ee5d
bcbbfd5
7887b3a
60491a3
1b61dec
5625974
7a0c72d
3cc0ef3
29483af
d1746a0
0862366
db11263
e45a1ec
c10b410
72a2d5e
1ae7ae8
914007c
da957b2
9488dac
a66e4e1
cc10149
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# rustup shell setup | ||
# affix colons on either side of $PATH to simplify matching | ||
case ":${PATH}:" in | ||
*:"{cargo_bin}":*) | ||
;; | ||
*) | ||
# Prepending path in case a system-installed rustc must be overwritten | ||
export PATH="{cargo_bin}:$PATH" | ||
;; | ||
esac |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
//! Paths and Unix shells | ||
//! | ||
//! MacOS, Linux, FreeBSD, and many other OS model their design on Unix, | ||
//! so handling them is relatively consistent. But only relatively. | ||
//! POSIX postdates Unix by 20 years, and each "Unix-like" shell develops | ||
//! unique quirks over time. | ||
//! | ||
//! | ||
//! Windowing Managers, Desktop Environments, GUI Terminals, and PATHs | ||
//! | ||
//! Duplicating paths in PATH can cause performance issues when the OS searches | ||
//! the same place multiple times. Traditionally, Unix configurations have | ||
//! resolved this by setting up PATHs in the shell's login profile. | ||
//! | ||
//! This has its own issues. Login profiles are only intended to run once, but | ||
//! changing the PATH is common enough that people may run it twice. Desktop | ||
//! environments often choose to NOT start login shells in GUI terminals. Thus, | ||
//! a trend has emerged to place PATH updates in other run-commands (rc) files, | ||
//! leaving Rustup with few assumptions to build on for fulfilling its promise | ||
//! to set up PATH appropriately. | ||
//! | ||
//! Rustup addresses this by: | ||
//! 1) using a shell script that updates PATH if the path is not in PATH | ||
//! 2) sourcing this script in any known and appropriate rc file | ||
|
||
use std::path::PathBuf; | ||
|
||
use error_chain::bail; | ||
|
||
use super::*; | ||
use crate::process; | ||
|
||
pub type Shell = Box<dyn UnixShell>; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct ShellScript { | ||
content: &'static str, | ||
name: &'static str, | ||
} | ||
|
||
impl ShellScript { | ||
pub fn write(&self) -> Result<()> { | ||
let home = utils::cargo_home()?; | ||
let cargo_bin = format!("{}/bin", cargo_home_str()?); | ||
let env_name = home.join(self.name); | ||
let env_file = self.content.replace("{cargo_bin}", &cargo_bin); | ||
utils::write_file(self.name, &env_name, &env_file)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
// TODO: Update into a bytestring. | ||
pub fn cargo_home_str() -> Result<Cow<'static, str>> { | ||
let path = utils::cargo_home()?; | ||
|
||
let default_cargo_home = utils::home_dir() | ||
.unwrap_or_else(|| PathBuf::from(".")) | ||
.join(".cargo"); | ||
Ok(if default_cargo_home == path { | ||
"$HOME/.cargo".into() | ||
} else { | ||
match path.to_str() { | ||
Some(p) => p.to_owned().into(), | ||
None => bail!("Non-Unicode path!"), | ||
} | ||
}) | ||
} | ||
|
||
// TODO: Tcsh (BSD) | ||
// TODO?: Make a decision on Ion Shell, Power Shell, Nushell | ||
// Cross-platform non-POSIX shells have not been assessed for integration yet | ||
fn enumerate_shells() -> Vec<Shell> { | ||
vec![Box::new(Posix), Box::new(Bash), Box::new(Zsh)] | ||
kinnison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub fn get_available_shells() -> impl Iterator<Item = Shell> { | ||
enumerate_shells().into_iter().filter(|sh| sh.does_exist()) | ||
} | ||
|
||
pub trait UnixShell { | ||
// Detects if a shell "exists". Users have multiple shells, so an "eager" | ||
// heuristic should be used, assuming shells exist if any traces do. | ||
fn does_exist(&self) -> bool; | ||
|
||
// Gives all rcfiles of a given shell that rustup is concerned with. | ||
// Used primarily in checking rcfiles for cleanup. | ||
fn rcfiles(&self) -> Vec<PathBuf>; | ||
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. See below - but I think rcfiles vs update_rcs is an unneeded distinction, because we can be pretty certain we won't be 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. Mm, the evolution of the zsh implementation suggests it is going to be a concern anyways. 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. Looking through this, I cannot see rcfiles() being called on the trait; only from within trait implementations. All I'm suggesting is that it be removed from the trait unless-and-until something that is calling methods on the trait needs to call it. |
||
|
||
// Gives rcs that should be written to. | ||
fn update_rcs(&self) -> Vec<PathBuf>; | ||
|
||
// Writes the relevant env file. | ||
fn env_script(&self) -> ShellScript { | ||
ShellScript { | ||
name: "env", | ||
content: include_str!("env.sh"), | ||
} | ||
} | ||
|
||
fn source_string(&self) -> Result<String> { | ||
Ok(format!(r#"source "{}/env""#, cargo_home_str()?)) | ||
} | ||
} | ||
|
||
struct Posix; | ||
impl UnixShell for Posix { | ||
fn does_exist(&self) -> bool { | ||
true | ||
} | ||
|
||
fn rcfiles(&self) -> Vec<PathBuf> { | ||
match utils::home_dir() { | ||
Some(dir) => vec![dir.join(".profile")], | ||
_ => vec![], | ||
} | ||
} | ||
|
||
fn update_rcs(&self) -> Vec<PathBuf> { | ||
// Write to .profile even if it doesn't exist. It's the only rc in the | ||
// POSIX spec so it should always be set up. | ||
self.rcfiles() | ||
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. With the suggestion above, rcfiles here could be inlined into update_rcs for this impl. 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. unix.sh, line 65: for rc in sh.rcfiles().iter().filter(|rc| rc.is_file()) { 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. I know it's basically that one place. ^^; |
||
} | ||
} | ||
|
||
struct Bash; | ||
|
||
impl UnixShell for Bash { | ||
fn does_exist(&self) -> bool { | ||
self.update_rcs().len() > 0 | ||
} | ||
|
||
fn rcfiles(&self) -> Vec<PathBuf> { | ||
// Bash also may read .profile, however Rustup already includes handling | ||
// .profile as part of POSIX and always does setup for POSIX shells. | ||
[".bash_profile", ".bash_login", ".bashrc"] | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.iter() | ||
.filter_map(|rc| utils::home_dir().map(|dir| dir.join(rc))) | ||
.collect() | ||
} | ||
|
||
fn update_rcs(&self) -> Vec<PathBuf> { | ||
self.rcfiles() | ||
.into_iter() | ||
.filter(|rc| rc.is_file()) | ||
.collect() | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
struct Zsh; | ||
|
||
impl Zsh { | ||
fn zdotdir() -> Result<PathBuf> { | ||
use std::ffi::OsStr; | ||
use std::os::unix::ffi::OsStrExt; | ||
|
||
if matches!(process().var("SHELL"), Ok(sh) if sh.contains("zsh")) { | ||
match process().var("ZDOTDIR") { | ||
Ok(dir) if dir.len() > 0 => Ok(PathBuf::from(dir)), | ||
_ => bail!("Zsh setup failed."), | ||
} | ||
} else { | ||
match std::process::Command::new("zsh") | ||
.args(&["-c", "'echo $ZDOTDIR'"]) | ||
.output() | ||
{ | ||
Ok(io) if io.stdout.len() > 0 => Ok(PathBuf::from(OsStr::from_bytes(&io.stdout))), | ||
_ => bail!("Zsh setup failed."), | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl UnixShell for Zsh { | ||
fn does_exist(&self) -> bool { | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// zsh has to either be the shell or be callable for zsh setup. | ||
matches!(process().var("SHELL"), Ok(sh) if sh.contains("zsh")) | ||
|| matches!(utils::find_cmd(&["zsh"]), Some(_)) | ||
} | ||
|
||
fn rcfiles(&self) -> Vec<PathBuf> { | ||
[Zsh::zdotdir().ok(), utils::home_dir()] | ||
.iter() | ||
.filter_map(|dir| dir.as_ref().map(|p| p.join(".zshenv"))) | ||
.collect() | ||
} | ||
|
||
fn update_rcs(&self) -> Vec<PathBuf> { | ||
// zsh can change $ZDOTDIR both _before_ AND _during_ reading .zshenv, | ||
// so we: write to $ZDOTDIR/.zshenv if-exists ($ZDOTDIR changes before) | ||
// OR write to $HOME/.zshenv if it exists (change-during) | ||
// if neither exist, we create it ourselves, but using the same logic, | ||
// because we must still respond to whether $ZDOTDIR is set or unset. | ||
// In any case we only write once. | ||
self.rcfiles() | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.into_iter() | ||
.filter(|env| env.is_file()) | ||
.chain(self.rcfiles().into_iter()) | ||
.take(1) | ||
.collect() | ||
} | ||
} | ||
|
||
pub fn legacy_paths() -> impl Iterator<Item = PathBuf> { | ||
let zprofiles = Zsh::zdotdir() | ||
.into_iter() | ||
.chain(utils::home_dir()) | ||
.map(|d| d.join(".zprofile")); | ||
let profiles = [".bash_profile", ".profile"] | ||
.iter() | ||
.filter_map(|rc| utils::home_dir().map(|d| d.join(rc))); | ||
|
||
profiles.chain(zprofiles) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.