Skip to content

Commit ad54f2d

Browse files
committed
Escape the CARGO_HOME path displayed on Windows installation
1 parent 41a96cd commit ad54f2d

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/cli/self_update.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use rustup::utils::Notification;
4141
use rustup::{Cfg, UpdateStatus};
4242
use rustup::{DUP_TOOLS, TOOLS};
4343
use same_file::Handle;
44+
use std::borrow::Cow;
4445
use std::env;
4546
use std::env::consts::EXE_SUFFIX;
4647
use std::fs;
@@ -215,22 +216,22 @@ static UPDATE_ROOT: &str = "https://static.rust-lang.org/rustup";
215216

216217
/// `CARGO_HOME` suitable for display, possibly with $HOME
217218
/// substituted for the directory prefix
218-
fn canonical_cargo_home() -> Result<String> {
219+
fn canonical_cargo_home() -> Result<Cow<'static, str>> {
219220
let path = utils::cargo_home()?;
220-
let mut path_str = path.to_string_lossy().into_owned();
221221

222222
let default_cargo_home = utils::home_dir()
223223
.unwrap_or_else(|| PathBuf::from("."))
224224
.join(".cargo");
225-
if default_cargo_home == path {
225+
let path = if default_cargo_home == path {
226226
if cfg!(unix) {
227-
path_str = String::from("$HOME/.cargo");
227+
Cow::Borrowed("$HOME/.cargo")
228228
} else {
229-
path_str = String::from(r"%USERPROFILE%\.cargo");
229+
Cow::Borrowed(r"%USERPROFILE%\.cargo")
230230
}
231-
}
232-
233-
Ok(path_str)
231+
} else {
232+
Cow::Owned(path.to_string_lossy().into_owned())
233+
};
234+
Ok(path)
234235
}
235236

236237
/// Installing is a simple matter of copying the running binary to
@@ -327,22 +328,19 @@ pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpt
327328
}
328329

329330
let cargo_home = canonical_cargo_home()?;
330-
let msg = if !opts.no_modify_path {
331-
if cfg!(unix) {
332-
format!(post_install_msg_unix!(), cargo_home = cargo_home)
333-
} else {
334-
format!(post_install_msg_win!(), cargo_home = cargo_home)
335-
}
336-
} else if cfg!(unix) {
337-
format!(
331+
#[cfg(windows)]
332+
let cargo_home = cargo_home.replace('\\', r"\\");
333+
let msg = match (opts.no_modify_path, cfg!(unix)) {
334+
(false, true) => format!(post_install_msg_unix!(), cargo_home = cargo_home),
335+
(false, false) => format!(post_install_msg_win!(), cargo_home = cargo_home),
336+
(true, true) => format!(
338337
post_install_msg_unix_no_modify_path!(),
339338
cargo_home = cargo_home
340-
)
341-
} else {
342-
format!(
339+
),
340+
(true, false) => format!(
343341
post_install_msg_win_no_modify_path!(),
344342
cargo_home = cargo_home
345-
)
343+
),
346344
};
347345
md(&mut term, msg);
348346

0 commit comments

Comments
 (0)