Skip to content

Commit 02e0fc8

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

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/cli/rustup_mode.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,25 @@ 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+
if active_toolchain
1291+
.as_ref()
1292+
.is_some_and(|n| n == &toolchain_name)
1293+
{
1294+
warn!("it seems that you are removing the active toolchain, is this intentional?");
1295+
}
1296+
if default_toolchain
1297+
.as_ref()
1298+
.is_some_and(|n| n == &toolchain_name)
1299+
{
1300+
warn!("after removing the default toolchain, proc-macros and build scripts might no longer build");
1301+
}
1302+
12861303
Toolchain::ensure_removed(cfg, (&toolchain_name).into())?;
12871304
}
12881305
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: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,38 @@ 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;
218218
cx.config
219-
.expect_ok(&["rustup", "toolchain", "remove", "nightly"])
220-
.await;
219+
.expect_stderr_ok(
220+
&["rustup", "toolchain", "remove", "nightly"],
221+
"after removing the default toolchain, proc-macros and build scripts might no longer build",
222+
).await;
221223
cx.config.expect_ok(&["rustup", "toolchain", "list"]).await;
222224
cx.config
223225
.expect_stdout_ok(&["rustup", "toolchain", "list"], "no installed toolchains")
224226
.await;
225227
}
226228

229+
#[tokio::test]
230+
async fn remove_toolchain_active() {
231+
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
232+
cx.config.expect_ok(&["rustup", "default", "nightly"]).await;
233+
cx.config
234+
.expect_ok(&["rustup", "override", "set", "stable"])
235+
.await;
236+
cx.config
237+
.expect_stderr_ok(
238+
&["rustup", "toolchain", "remove", "stable"],
239+
"it seems that you are removing the active toolchain, is this intentional?",
240+
)
241+
.await;
242+
cx.config
243+
.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly")
244+
.await;
245+
}
246+
227247
// Issue #2873
228248
#[tokio::test]
229249
async fn remove_toolchain_ignore_trailing_slash() {

0 commit comments

Comments
 (0)