Skip to content

Commit

Permalink
feat(cli): warn when removing the default/active toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Jul 7, 2024
1 parent 044083c commit 20c7318
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,18 @@ async fn toolchain_link(
}

fn toolchain_remove(cfg: &mut Cfg<'_>, opts: UninstallOpts) -> Result<utils::ExitCode> {
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()?)?;

match (default_toolchain.as_ref(), active_toolchain.as_ref()) {
(Some(n), _) if n == &toolchain_name => Some("default"),
(_, Some(n)) if n == &toolchain_name => Some("active"),
_ => None,
}.inspect(|status| warn!("after removing the {status} toolchain, proc-macros and build scripts might no longer build"));

Toolchain::ensure_removed(cfg, (&toolchain_name).into())?;
}
Ok(utils::ExitCode(0))
Expand Down
9 changes: 9 additions & 0 deletions src/toolchain/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ from_variant!(
LocalToolchainName::Path
);

impl PartialEq<ToolchainName> 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 {
Expand Down
25 changes: 21 additions & 4 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,35 @@ 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;
cx.config.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"],
"after removing the active toolchain, proc-macros and build scripts might no longer build",
).await;
cx.config
.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly")
.await;
}

// Issue #2873
#[tokio::test]
async fn remove_toolchain_ignore_trailing_slash() {
Expand Down

0 comments on commit 20c7318

Please sign in to comment.