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 8, 2024
1 parent 969c3c2 commit 2e438b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,25 @@ 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()?)?;

if active_toolchain
.as_ref()
.is_some_and(|n| n == &toolchain_name)
{
warn!("removing the active toolchain; a toolchain override will be required for running Rust tools");
}
if default_toolchain
.as_ref()
.is_some_and(|n| n == &toolchain_name)
{
warn!("removing the default 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: 23 additions & 2 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,39 @@ 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"])
.expect_stderr_ok(
&["rustup", "toolchain", "remove", "nightly"],
"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"],
"removing the active toolchain; a toolchain override will be required for running Rust tools",
)
.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 2e438b0

Please sign in to comment.