Respect $CARGO_HOME when looking up toolchains.

This commit is contained in:
Tomoki Nakagawa 2023-02-24 15:26:25 +09:00
parent 289208bc9f
commit e4b184a776
2 changed files with 35 additions and 16 deletions

View file

@ -31,8 +31,9 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
// 2) `<executable_name>`
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
// 3) `~/.cargo/bin/<executable_name>`
// example: for cargo, this tries ~/.cargo/bin/cargo
// 3) `$CARGO_HOME/bin/<executable_name>`
// where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html)
// example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
let env_var = executable_name.to_ascii_uppercase();
if let Some(path) = env::var_os(env_var) {
@ -43,8 +44,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
return executable_name.into();
}
if let Some(mut path) = home::home_dir() {
path.push(".cargo");
if let Some(mut path) = get_cargo_home() {
path.push("bin");
path.push(executable_name);
if let Some(path) = probe(path) {
@ -60,6 +60,19 @@ fn lookup_in_path(exec: &str) -> bool {
env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
}
fn get_cargo_home() -> Option<PathBuf> {
if let Some(path) = env::var_os("CARGO_HOME") {
return Some(path.into());
}
if let Some(mut path) = home::home_dir() {
path.push(".cargo");
return Some(path);
}
None
}
fn probe(path: PathBuf) -> Option<PathBuf> {
let with_extension = match env::consts::EXE_EXTENSION {
"" => None,