Skip to content

Commit 20c7318

Browse files
committed
feat(cli): warn when removing the default/active toolchain
1 parent 044083c commit 20c7318

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/cli/rustup_mode.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,18 @@ async fn toolchain_link(
12811281
}
12821282

12831283
fn toolchain_remove(cfg: &mut Cfg<'_>, opts: UninstallOpts) -> Result<utils::ExitCode> {
1284+
let default_toolchain = cfg.get_default().ok().flatten();
1285+
let active_toolchain = cfg.find_active_toolchain().ok().flatten().map(|(it, _)| it);
1286+
12841287
for toolchain_name in &opts.toolchain {
12851288
let toolchain_name = toolchain_name.resolve(&cfg.get_default_host_triple()?)?;
1289+
1290+
match (default_toolchain.as_ref(), active_toolchain.as_ref()) {
1291+
(Some(n), _) if n == &toolchain_name => Some("default"),
1292+
(_, Some(n)) if n == &toolchain_name => Some("active"),
1293+
_ => None,
1294+
}.inspect(|status| warn!("after removing the {status} toolchain, proc-macros and build scripts might no longer build"));
1295+
12861296
Toolchain::ensure_removed(cfg, (&toolchain_name).into())?;
12871297
}
12881298
Ok(utils::ExitCode(0))

src/toolchain/names.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ from_variant!(
359359
LocalToolchainName::Path
360360
);
361361

362+
impl PartialEq<ToolchainName> for LocalToolchainName {
363+
fn eq(&self, other: &ToolchainName) -> bool {
364+
match self {
365+
LocalToolchainName::Named(n) => n == other,
366+
_ => false,
367+
}
368+
}
369+
}
370+
362371
impl Display for LocalToolchainName {
363372
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
364373
match self {

tests/suite/cli_v2.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,35 @@ async fn list_toolchains_with_none() {
212212
}
213213

214214
#[tokio::test]
215-
async fn remove_toolchain() {
215+
async fn remove_toolchain_default() {
216216
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
217217
cx.config.expect_ok(&["rustup", "update", "nightly"]).await;
218-
cx.config
219-
.expect_ok(&["rustup", "toolchain", "remove", "nightly"])
220-
.await;
218+
cx.config.expect_stderr_ok(
219+
&["rustup", "toolchain", "remove", "nightly"],
220+
"after removing the default toolchain, proc-macros and build scripts might no longer build",
221+
).await;
221222
cx.config.expect_ok(&["rustup", "toolchain", "list"]).await;
222223
cx.config
223224
.expect_stdout_ok(&["rustup", "toolchain", "list"], "no installed toolchains")
224225
.await;
225226
}
226227

228+
#[tokio::test]
229+
async fn remove_toolchain_active() {
230+
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
231+
cx.config.expect_ok(&["rustup", "default", "nightly"]).await;
232+
cx.config
233+
.expect_ok(&["rustup", "override", "set", "stable"])
234+
.await;
235+
cx.config.expect_stderr_ok(
236+
&["rustup", "toolchain", "remove", "stable"],
237+
"after removing the active toolchain, proc-macros and build scripts might no longer build",
238+
).await;
239+
cx.config
240+
.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly")
241+
.await;
242+
}
243+
227244
// Issue #2873
228245
#[tokio::test]
229246
async fn remove_toolchain_ignore_trailing_slash() {

0 commit comments

Comments
 (0)