Skip to content

Commit 3df4596

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

File tree

3 files changed

+34
-8
lines changed

3 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: 19 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 {

tests/cli-rustup.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,18 @@ fn multirust_dir_upgrade_rename_multirust_dir_to_rustup() {
856856
});
857857
}
858858

859+
#[test]
860+
fn rustup_from_non_existing_path() {
861+
setup(&|config| {
862+
let non_existing_dir = config.homedir.join(".remove_me");
863+
let mut cmd = clitools::cmd(config, "rustup", &["-h"]);
864+
cmd.env_remove("CARGO_HOME");
865+
cmd.current_dir(non_existing_dir);
866+
assert!(cmd.output().unwrap().status.success());
867+
//expect_ok(config, &["rustup", "-h"]);
868+
});
869+
}
870+
859871
// Renaming ~/.multirust to ~/.rustup but ~/.rustup/rustup-version (rustup.sh) exists
860872
#[test]
861873
fn multirust_dir_upgrade_old_rustup_exists() {

0 commit comments

Comments
 (0)