Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hint to run rustup self when err desc is self #3901

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::str::FromStr;

use anyhow::{anyhow, Error, Result};
use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, ValueEnum};
Expand Down Expand Up @@ -135,7 +136,7 @@ enum RustupSubcmd {
)]
Update {
/// Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more information see `rustup help toolchain`
#[arg(num_args = 1..)]
#[arg(num_args = 1.., value_parser = update_toolchain_value_parser)]
toolchain: Vec<PartialToolchainDesc>,

/// Don't perform self update when running the `rustup update` command
Expand Down Expand Up @@ -258,6 +259,14 @@ enum RustupSubcmd {
},
}

fn update_toolchain_value_parser(s: &str) -> Result<PartialToolchainDesc> {
PartialToolchainDesc::from_str(s).inspect_err(|_| {
if s == "self" {
info!("if you meant to update rustup itself, use `rustup self update`");
}
})
}

#[derive(Debug, Subcommand)]
enum ShowSubcmd {
/// Show the active toolchain
Expand Down
6 changes: 6 additions & 0 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ impl<'a> Toolchain<'a> {
fs::remove_dir_all(&path)?;
true
} else {
let name = name.to_string();
info!("no toolchain installed for '{name}'");
if name == "self" {
info!(
"if you meant to uninstall rustup itself, use `rustup self uninstall`"
);
}
false
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,3 +1180,27 @@ async fn toolchain_link_then_list_verbose() {
.expect_stdout_ok(&["rustup", "toolchain", "list", "-v"], "/custom-1")
.await;
}

#[tokio::test]
async fn update_self_smart_guess() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
let out = cx.config.run("rustup", &["update", "self"], &[]).await;
let invalid_toolchain = out.stderr.contains("invalid toolchain name");
if !out.ok && invalid_toolchain {
assert!(out
.stderr
.contains("if you meant to update rustup itself, use `rustup self update`"))
}
}

#[tokio::test]
async fn uninstall_self_smart_guess() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
let out = cx.config.run("rustup", &["uninstall", "self"], &[]).await;
let no_toolchain_installed = out.stdout.contains("no toolchain installed");
if out.ok && no_toolchain_installed {
assert!(out
.stdout
.contains("if you meant to uninstall rustup itself, use `rustup self uninstall`"))
}
}
Loading