Skip to content

Commit 3c7726c

Browse files
committed
do not get cwd if not env set
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent cc87d74 commit 3c7726c

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/rustup-utils/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ error_chain! {
156156
description("failed to set permissions")
157157
display("failed to set permissions for '{}'", path.display())
158158
}
159+
GettingCwd {
160+
description("couldn't get current working directory")
161+
}
159162
CargoHome {
160163
description("couldn't find value of CARGO_HOME")
161164
}

src/rustup-utils/src/utils.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,15 @@ pub fn cargo_home() -> Result<PathBuf> {
546546
None
547547
};
548548

549-
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::CargoHome));
550-
let cargo_home = env_var.clone().map(|home| {
551-
cwd.join(home)
552-
});
549+
let cargo_home = if env_var.is_some() {
550+
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
551+
env_var.clone().map(|home| {
552+
cwd.join(home)
553+
})
554+
} else {
555+
None
556+
};
557+
553558
let user_home = home_dir().map(|p| p.join(".cargo"));
554559
cargo_home.or(user_home).ok_or(ErrorKind::CargoHome.into())
555560
}
@@ -739,11 +744,17 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
739744

740745
pub fn rustup_home() -> Result<PathBuf> {
741746
let use_rustup_dir = do_rustup_home_upgrade();
747+
let rustup_home_env = env::var_os("RUSTUP_HOME");
748+
749+
let rustup_home = if rustup_home_env.is_some() {
750+
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
751+
rustup_home_env.clone().map(|home| {
752+
cwd.join(home)
753+
})
754+
} else {
755+
None
756+
};
742757

743-
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::RustupHome));
744-
let rustup_home = env::var_os("RUSTUP_HOME").map(|home| {
745-
cwd.join(home)
746-
});
747758
let user_home = if use_rustup_dir {
748759
dot_dir(".rustup")
749760
} else {
@@ -836,6 +847,18 @@ pub fn toolchain_sort<T: AsRef<str>>(v: &mut Vec<T>) {
836847
mod tests {
837848
use super::*;
838849

850+
#[test]
851+
fn test_cargo_home() {
852+
// CARGO_HOME unset, we'll get the default ending in /.cargo
853+
let cargo_home1 = cargo_home();
854+
let ch = format!("{}", cargo_home1.unwrap().display());
855+
assert!(ch.contains("/.cargo"));
856+
857+
env::set_var("CARGO_HOME", "/test");
858+
let cargo_home2 = cargo_home();
859+
assert_eq!("/test", format!("{}", cargo_home2.unwrap().display()));
860+
}
861+
839862
#[test]
840863
fn test_toochain_sort() {
841864
let expected = vec![

0 commit comments

Comments
 (0)