Skip to content

Commit a99da2d

Browse files
authored
Merge pull request #2201 from kinnison/honour-args-during-reinstall
rustup-init: Support updating the installed toolchain
2 parents 31bbad3 + 36bc1f9 commit a99da2d

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

src/cli/self_update.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use std::process::{self, Command};
4848

4949
pub struct InstallOpts<'a> {
5050
pub default_host_triple: Option<String>,
51-
pub default_toolchain: String,
51+
pub default_toolchain: Option<String>,
5252
pub profile: String,
5353
pub no_modify_path: bool,
5454
pub components: &'a [&'a str],
@@ -284,9 +284,9 @@ pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpt
284284
}
285285
utils::create_rustup_home()?;
286286
maybe_install_rust(
287-
&opts.default_toolchain,
287+
opts.default_toolchain.as_deref(),
288288
&opts.profile,
289-
opts.default_host_triple.as_ref(),
289+
opts.default_host_triple.as_deref(),
290290
opts.components,
291291
opts.targets,
292292
verbose,
@@ -438,10 +438,10 @@ fn do_pre_install_options_sanity_checks(opts: &InstallOpts) -> Result<()> {
438438
.as_ref()
439439
.map(|s| dist::TargetTriple::new(s))
440440
.unwrap_or_else(TargetTriple::from_host_or_build);
441-
let toolchain_to_use = if opts.default_toolchain == "none" {
442-
"stable"
443-
} else {
444-
&opts.default_toolchain
441+
let toolchain_to_use = match &opts.default_toolchain {
442+
None => "stable",
443+
Some(s) if s == "none" => "stable",
444+
Some(s) => &s,
445445
};
446446
let partial_channel = dist::PartialToolchainDesc::from_str(toolchain_to_use)?;
447447
let resolved = partial_channel.resolve(&host_triple)?.to_string();
@@ -588,7 +588,9 @@ fn current_install_opts(opts: &InstallOpts) -> String {
588588
.as_ref()
589589
.map(|s| TargetTriple::new(s))
590590
.unwrap_or_else(TargetTriple::from_host_or_build),
591-
opts.default_toolchain,
591+
opts.default_toolchain
592+
.as_deref()
593+
.unwrap_or("stable (default)"),
592594
opts.profile,
593595
if !opts.no_modify_path { "yes" } else { "no" }
594596
)
@@ -610,10 +612,10 @@ fn customize_install(mut opts: InstallOpts) -> Result<InstallOpts> {
610612
.unwrap_or_else(|| TargetTriple::from_host_or_build().to_string()),
611613
)?);
612614

613-
opts.default_toolchain = common::question_str(
615+
opts.default_toolchain = Some(common::question_str(
614616
"Default toolchain? (stable/beta/nightly/none)",
615-
&opts.default_toolchain,
616-
)?;
617+
opts.default_toolchain.as_deref().unwrap_or("stable"),
618+
)?);
617619

618620
opts.profile = common::question_str(
619621
&format!(
@@ -725,9 +727,9 @@ pub fn install_proxies() -> Result<()> {
725727
}
726728

727729
fn maybe_install_rust(
728-
toolchain_str: &str,
730+
toolchain: Option<&str>,
729731
profile_str: &str,
730-
default_host_triple: Option<&String>,
732+
default_host_triple: Option<&str>,
731733
components: &[&str],
732734
targets: &[&str],
733735
verbose: bool,
@@ -744,21 +746,43 @@ fn maybe_install_rust(
744746
info!("default host triple is {}", cfg.get_default_host_triple()?);
745747
}
746748

747-
// If there is already an install, then `toolchain_str` may not be
748-
// a toolchain the user actually wants. Don't do anything. FIXME:
749-
// This logic should be part of InstallOpts so that it isn't
750-
// possible to select a toolchain then have it not be installed.
751-
if toolchain_str == "none" {
749+
let user_specified_something =
750+
toolchain.is_some() || !targets.is_empty() || !components.is_empty();
751+
752+
// If the user specified they want no toolchain, we skip this, otherwise
753+
// if they specify something directly, or we have no default, then we install
754+
// a toolchain (updating if it's already present) and then if neither of
755+
// those are true, we have a user who doesn't mind, and already has an
756+
// install, so we leave their setup alone.
757+
if toolchain == Some("none") {
752758
info!("skipping toolchain installation");
759+
if !components.is_empty() {
760+
warn!(
761+
"ignoring requested component{}: {}",
762+
if components.len() == 1 { "" } else { "s" },
763+
components.join(", ")
764+
);
765+
}
766+
if !targets.is_empty() {
767+
warn!(
768+
"ignoring requested target{}: {}",
769+
if targets.len() == 1 { "" } else { "s" },
770+
targets.join(", ")
771+
);
772+
}
753773
println!();
754-
} else if cfg.find_default()?.is_none() {
774+
} else if user_specified_something || cfg.find_default()?.is_none() {
775+
let toolchain_str = toolchain.unwrap_or("stable");
755776
let toolchain = cfg.get_toolchain(toolchain_str, false)?;
777+
if toolchain.exists() {
778+
warn!("Updating existing toolchain, profile choice will be ignored");
779+
}
756780
let status = toolchain.install_from_dist(true, false, components, targets)?;
757781
cfg.set_default(toolchain_str)?;
758782
println!();
759783
common::show_channel_update(&cfg, toolchain_str, Ok(status))?;
760784
} else {
761-
info!("updating existing rustup installation");
785+
info!("updating existing rustup installation - leaving toolchains alone");
762786
println!();
763787
}
764788

src/cli/setup_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn main() -> Result<()> {
8888
let verbose = matches.is_present("verbose");
8989
let quiet = matches.is_present("quiet");
9090
let default_host = matches.value_of("default-host").map(ToOwned::to_owned);
91-
let default_toolchain = matches.value_of("default-toolchain").unwrap_or("stable");
91+
let default_toolchain = matches.value_of("default-toolchain").map(ToOwned::to_owned);
9292
let profile = matches
9393
.value_of("profile")
9494
.expect("Unreachable: Clap should supply a default");

tests/cli-self-upd.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,43 @@ fn reinstall_exact() {
900900
expect_stderr_ok(
901901
config,
902902
&["rustup-init", "-y"],
903-
r"info: updating existing rustup installation
904-
",
903+
r"info: updating existing rustup installation - leaving toolchains alone",
904+
);
905+
});
906+
}
907+
908+
#[test]
909+
fn reinstall_specifying_toolchain() {
910+
setup(&|config| {
911+
expect_ok(config, &["rustup-init", "-y"]);
912+
expect_stdout_ok(
913+
config,
914+
&["rustup-init", "-y", "--default-toolchain=stable"],
915+
r"stable unchanged - 1.1.0",
916+
);
917+
});
918+
}
919+
920+
#[test]
921+
fn reinstall_specifying_component() {
922+
setup(&|config| {
923+
expect_ok(config, &["rustup-init", "-y", "--component=rls"]);
924+
expect_stdout_ok(
925+
config,
926+
&["rustup-init", "-y", "--default-toolchain=stable"],
927+
r"stable unchanged - 1.1.0",
928+
);
929+
});
930+
}
931+
932+
#[test]
933+
fn reinstall_specifying_different_toolchain() {
934+
setup(&|config| {
935+
expect_ok(config, &["rustup-init", "-y"]);
936+
expect_stderr_ok(
937+
config,
938+
&["rustup-init", "-y", "--default-toolchain=nightly"],
939+
r"info: default toolchain set to 'nightly'",
905940
);
906941
});
907942
}

0 commit comments

Comments
 (0)