Skip to content

Commit 2e438b0

Browse files
committed
feat(cli): warn when removing the default/active toolchain
1 parent 969c3c2 commit 2e438b0

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
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!("removing the active toolchain; a toolchain override will be required for running Rust tools");
1295+
}
1296+
if default_toolchain
1297+
.as_ref()
1298+
.is_some_and(|n| n == &toolchain_name)
1299+
{
1300+
warn!("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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,39 @@ 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"])
219+
.expect_stderr_ok(
220+
&["rustup", "toolchain", "remove", "nightly"],
221+
"removing the default toolchain; proc-macros and build scripts might no longer build",
222+
)
220223
.await;
221224
cx.config.expect_ok(&["rustup", "toolchain", "list"]).await;
222225
cx.config
223226
.expect_stdout_ok(&["rustup", "toolchain", "list"], "no installed toolchains")
224227
.await;
225228
}
226229

230+
#[tokio::test]
231+
async fn remove_toolchain_active() {
232+
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
233+
cx.config.expect_ok(&["rustup", "default", "nightly"]).await;
234+
cx.config
235+
.expect_ok(&["rustup", "override", "set", "stable"])
236+
.await;
237+
cx.config
238+
.expect_stderr_ok(
239+
&["rustup", "toolchain", "remove", "stable"],
240+
"removing the active toolchain; a toolchain override will be required for running Rust tools",
241+
)
242+
.await;
243+
cx.config
244+
.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly")
245+
.await;
246+
}
247+
227248
// Issue #2873
228249
#[tokio::test]
229250
async fn remove_toolchain_ignore_trailing_slash() {

0 commit comments

Comments
 (0)