Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rustup-init): detect and warn about existing settings.toml #4064

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ pub(crate) async fn install(
.map_or(false, |s| s == "yes")
{
check_existence_of_rustc_or_cargo_in_path(no_prompt, process)?;
check_existence_of_settings_file(process)?;
}

#[cfg(unix)]
Expand Down Expand Up @@ -631,7 +632,7 @@ fn check_existence_of_rustc_or_cargo_in_path(no_prompt: bool, process: &Process)
}

if let Err(path) = rustc_or_cargo_exists_in_path(process) {
warn!("it looks like you have an existing installation of Rust at:");
warn!("It looks like you have an existing installation of Rust at:");
warn!("{}", path);
warn!("It is recommended that rustup be the primary Rust installation.");
warn!("Otherwise you may have confusion unless you are careful with your PATH.");
Expand All @@ -643,6 +644,18 @@ fn check_existence_of_rustc_or_cargo_in_path(no_prompt: bool, process: &Process)
Ok(())
}

fn check_existence_of_settings_file(process: &Process) -> Result<()> {
let rustup_dir = process.rustup_home()?;
let settings_file_path = rustup_dir.join("settings.toml");
if utils::path_exists(&settings_file_path) {
warn!("It looks like you have an existing rustup settings file at:");
warn!("{}", settings_file_path.display());
warn!("Rustup will install the default toolchain as specified in the settings file,");
warn!("instead of the one inferred from the default host triple.");
}
Ok(())
}

fn pre_install_msg(no_modify_path: bool, process: &Process) -> Result<String> {
let cargo_home = process.cargo_home()?;
let cargo_home_bin = cargo_home.join("bin");
Expand Down
41 changes: 39 additions & 2 deletions tests/suite/cli_inst_interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ async fn install_stops_if_rustc_exists() {
assert!(!out.ok);
assert!(out
.stderr
.contains("it looks like you have an existing installation of Rust at:"));
.contains("It looks like you have an existing installation of Rust at:"));
assert!(out
.stderr
.contains("If you are sure that you want both rustup and your already installed Rust"));
Expand Down Expand Up @@ -530,7 +530,7 @@ async fn install_stops_if_cargo_exists() {
assert!(!out.ok);
assert!(out
.stderr
.contains("it looks like you have an existing installation of Rust at:"));
.contains("It looks like you have an existing installation of Rust at:"));
assert!(out
.stderr
.contains("If you are sure that you want both rustup and your already installed Rust"));
Expand Down Expand Up @@ -579,3 +579,40 @@ async fn install_non_installable_toolchain() {
)
.await;
}

#[tokio::test]
async fn install_warns_about_existing_settings_file() {
let temp_dir = tempfile::Builder::new()
.prefix("fakehome")
.tempdir()
.unwrap();
// Create `settings.toml`
let settings_file = temp_dir.path().join("settings.toml");
raw::write_file(
&settings_file,
for_host!(
r#"default_toolchain = "{}"
profile = "default"
version = "12""#
),
)
.unwrap();
let temp_dir_path = temp_dir.path().to_str().unwrap();

let cx = CliTestContext::new(Scenario::SimpleV2).await;
let out = cx
.config
.run(
"rustup-init",
["-y", "--no-modify-path"],
&[
("RUSTUP_INIT_SKIP_PATH_CHECK", "no"),
("RUSTUP_HOME", temp_dir_path),
],
)
.await;
assert!(out.ok);
assert!(out
.stderr
.contains("It looks like you have an existing rustup settings file at:"));
}
Loading