From 02e0fc8e8c1ed8bb08e5d2152513362570ae1572 Mon Sep 17 00:00:00 2001 From: rami3l Date: Sun, 7 Jul 2024 20:29:16 +0800 Subject: [PATCH] feat(cli): warn when removing the default/active toolchain --- src/cli/rustup_mode.rs | 17 +++++++++++++++++ src/toolchain/names.rs | 9 +++++++++ tests/suite/cli_v2.rs | 26 +++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index c04c841b29..069dcd59e0 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1281,8 +1281,25 @@ async fn toolchain_link( } fn toolchain_remove(cfg: &mut Cfg<'_>, opts: UninstallOpts) -> Result { + let default_toolchain = cfg.get_default().ok().flatten(); + let active_toolchain = cfg.find_active_toolchain().ok().flatten().map(|(it, _)| it); + for toolchain_name in &opts.toolchain { let toolchain_name = toolchain_name.resolve(&cfg.get_default_host_triple()?)?; + + if active_toolchain + .as_ref() + .is_some_and(|n| n == &toolchain_name) + { + warn!("it seems that you are removing the active toolchain, is this intentional?"); + } + if default_toolchain + .as_ref() + .is_some_and(|n| n == &toolchain_name) + { + warn!("after removing the default toolchain, proc-macros and build scripts might no longer build"); + } + Toolchain::ensure_removed(cfg, (&toolchain_name).into())?; } Ok(utils::ExitCode(0)) diff --git a/src/toolchain/names.rs b/src/toolchain/names.rs index f4cefc875e..5ce4ba482a 100644 --- a/src/toolchain/names.rs +++ b/src/toolchain/names.rs @@ -359,6 +359,15 @@ from_variant!( LocalToolchainName::Path ); +impl PartialEq for LocalToolchainName { + fn eq(&self, other: &ToolchainName) -> bool { + match self { + LocalToolchainName::Named(n) => n == other, + _ => false, + } + } +} + impl Display for LocalToolchainName { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { diff --git a/tests/suite/cli_v2.rs b/tests/suite/cli_v2.rs index 57e9424abf..782bd873cc 100644 --- a/tests/suite/cli_v2.rs +++ b/tests/suite/cli_v2.rs @@ -212,18 +212,38 @@ async fn list_toolchains_with_none() { } #[tokio::test] -async fn remove_toolchain() { +async fn remove_toolchain_default() { let mut cx = CliTestContext::new(Scenario::SimpleV2).await; cx.config.expect_ok(&["rustup", "update", "nightly"]).await; cx.config - .expect_ok(&["rustup", "toolchain", "remove", "nightly"]) - .await; + .expect_stderr_ok( + &["rustup", "toolchain", "remove", "nightly"], + "after removing the default toolchain, proc-macros and build scripts might no longer build", + ).await; cx.config.expect_ok(&["rustup", "toolchain", "list"]).await; cx.config .expect_stdout_ok(&["rustup", "toolchain", "list"], "no installed toolchains") .await; } +#[tokio::test] +async fn remove_toolchain_active() { + let mut cx = CliTestContext::new(Scenario::SimpleV2).await; + cx.config.expect_ok(&["rustup", "default", "nightly"]).await; + cx.config + .expect_ok(&["rustup", "override", "set", "stable"]) + .await; + cx.config + .expect_stderr_ok( + &["rustup", "toolchain", "remove", "stable"], + "it seems that you are removing the active toolchain, is this intentional?", + ) + .await; + cx.config + .expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly") + .await; +} + // Issue #2873 #[tokio::test] async fn remove_toolchain_ignore_trailing_slash() {