Skip to content

Commit 69d8691

Browse files
authored
Merge pull request #1379 from runcom/rustup-from-non-existing-path
do not get cwd if not env set
2 parents 5a91e33 + ee24bb1 commit 69d8691

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/rustup-utils/src/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ error_chain! {
157157
description("failed to set permissions")
158158
display("failed to set permissions for '{}'", path.display())
159159
}
160+
GettingCwd {
161+
description("couldn't get current working directory")
162+
}
160163
CargoHome {
161164
description("couldn't find value of CARGO_HOME")
162165
}

src/rustup-utils/src/utils.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,15 @@ pub fn cargo_home() -> Result<PathBuf> {
520520
None
521521
};
522522

523-
let cwd = env::current_dir().chain_err(|| ErrorKind::CargoHome)?;
524-
let cargo_home = env_var.clone().map(|home| cwd.join(home));
523+
let cargo_home = if env_var.is_some() {
524+
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
525+
env_var.clone().map(|home| {
526+
cwd.join(home)
527+
})
528+
} else {
529+
None
530+
};
531+
525532
let user_home = home_dir().map(|p| p.join(".cargo"));
526533
cargo_home.or(user_home).ok_or(ErrorKind::CargoHome.into())
527534
}
@@ -716,9 +723,17 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
716723

717724
pub fn rustup_home() -> Result<PathBuf> {
718725
let use_rustup_dir = do_rustup_home_upgrade();
726+
let rustup_home_env = env::var_os("RUSTUP_HOME");
727+
728+
let rustup_home = if rustup_home_env.is_some() {
729+
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
730+
rustup_home_env.clone().map(|home| {
731+
cwd.join(home)
732+
})
733+
} else {
734+
None
735+
};
719736

720-
let cwd = env::current_dir().chain_err(|| ErrorKind::RustupHome)?;
721-
let rustup_home = env::var_os("RUSTUP_HOME").map(|home| cwd.join(home));
722737
let user_home = if use_rustup_dir {
723738
dot_dir(".rustup")
724739
} else {
@@ -822,6 +837,18 @@ fn rename(name: &'static str, src: &Path, dest: &Path) -> Result<()> {
822837
mod tests {
823838
use super::*;
824839

840+
#[test]
841+
fn test_cargo_home() {
842+
// CARGO_HOME unset, we'll get the default ending in /.cargo
843+
let cargo_home1 = cargo_home();
844+
let ch = format!("{}", cargo_home1.unwrap().display());
845+
assert!(ch.contains("/.cargo"));
846+
847+
env::set_var("CARGO_HOME", "/test");
848+
let cargo_home2 = cargo_home();
849+
assert_eq!("/test", format!("{}", cargo_home2.unwrap().display()));
850+
}
851+
825852
#[test]
826853
fn test_toochain_sort() {
827854
let expected = vec![

0 commit comments

Comments
 (0)