Skip to content
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
66 changes: 63 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,66 @@ impl fmt::Display for TargetSuggestion {
}
}

#[derive(Debug, Clone)]
pub enum ComponentSuggestion {
Toolchain { name: String, component: String },
Component(String),
}

impl ComponentSuggestion {
pub(crate) fn for_removal(
desc: &ChannelToolchainName,
component: &Component,
config: &DistConfig,
manifest: &Manifest,
cfg: &Cfg<'_>,
) -> Option<Self> {
let Ok(toolchains) = cfg.list_toolchains(true) else {
return component_suggestion(desc, component, config, manifest, true)
.map(Self::Component);
};

let names = [component.name(), component.wildcard().name()];
for toolchain_name in toolchains {
if let ToolchainName::Channel(toolchain_desc) = &toolchain_name
&& toolchain_desc == desc
{
continue;
}

let Ok(toolchain) = Toolchain::new(cfg, toolchain_name.clone().into()) else {
continue;
};
let Ok(installed_components) = toolchain.installed_components() else {
continue;
};

if installed_components
.iter()
.any(|c| names.iter().any(|n| n == c.name()))
{
return Some(Self::Toolchain {
name: toolchain_name.to_string(),
component: manifest.short_name(component).to_string(),
});
}
}

component_suggestion(desc, component, config, manifest, true).map(Self::Component)
}
}

impl fmt::Display for ComponentSuggestion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Toolchain { name, component } => {
write!(f, "try `rustup +{name} component remove {component}`")
}
Self::Component(name) => write!(f, "did you mean '{name}'?"),
}
}
}

pub(crate) fn component_suggestion(
desc: &ChannelToolchainName,
component: &Component,
Expand Down Expand Up @@ -157,7 +217,7 @@ pub struct OperationError(pub anyhow::Error);
pub struct UnknownComponentInfo {
pub name: String,
pub description: String,
pub suggestion: Option<String>,
pub suggestion: Option<ComponentSuggestion>,
}

#[derive(ThisError, Debug)]
Expand Down Expand Up @@ -418,7 +478,7 @@ fn unknown_components_msg(
);

if let Some(suggestion) = &component.suggestion {
let _ = write!(buf, "\nhelp: did you mean '{suggestion}'?");
let _ = write!(buf, "\nhelp: {suggestion}");
}

if component.description.contains("rust-std") {
Expand All @@ -442,7 +502,7 @@ fn unknown_components_msg(
let _ = writeln!(buf, " - '{}'", component.name);

if let Some(suggestion) = &component.suggestion {
let _ = writeln!(buf, " help: did you mean '{suggestion}'?");
let _ = writeln!(buf, " help: {suggestion}");
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/toolchain/distributable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
manifestation::{Changes, Manifestation},
prefix::InstallPrefix,
},
errors::{TargetSuggestion, UnknownComponentInfo, component_suggestion},
errors::{ComponentSuggestion, TargetSuggestion, UnknownComponentInfo, component_suggestion},
install::InstallMethod,
};

Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'a> DistributableToolchain<'a> {
components: vec![UnknownComponentInfo {
name: manifest.short_name(&component).to_string(),
description: manifest.description(&component),
suggestion,
suggestion: suggestion.map(ComponentSuggestion::Component),
}],
}
.into());
Expand Down Expand Up @@ -361,7 +361,13 @@ impl<'a> DistributableToolchain<'a> {
.into());
}

let suggestion = component_suggestion(&self.desc, &component, &config, &manifest, true);
let suggestion = ComponentSuggestion::for_removal(
&self.desc,
&component,
&config,
&manifest,
self.toolchain.cfg,
);

unknown_components.push(UnknownComponentInfo {
name: manifest.short_name(&component).to_string(),
Expand Down
61 changes: 61 additions & 0 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,67 @@ error: toolchain 'stable-[HOST_TUPLE]' does not have target '[CROSS_ARCH_I]' ins
.is_err();
}

#[tokio::test]
async fn remove_component_not_installed_with_suggestion() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect(["rustup", "toolchain", "install", "stable"])
.await
.is_ok();
cx.config
.expect(["rustup", "toolchain", "install", "nightly"])
.await
.is_ok();
cx.config
.expect(["rustup", "default", "stable"])
.await
.is_ok();
cx.config
.expect([
"rustup",
"component",
"add",
"rust-analysis",
"--toolchain=nightly",
])
.await
.is_ok();
cx.config
.expect(["rustup", "component", "remove", "rust-analysis"])
.await
.with_stderr(snapbox::str![[r#"
error: toolchain 'stable-[HOST_TUPLE]' does not contain component 'rust-analysis' for target '[HOST_TUPLE]'
help: try `rustup +nightly-[HOST_TUPLE] component remove rust-analysis`

"#]])
.is_err();
}

#[tokio::test]
async fn remove_component_not_installed_no_alternative() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect(["rustup", "toolchain", "install", "stable"])
.await
.is_ok();
cx.config
.expect(["rustup", "toolchain", "install", "nightly"])
.await
.is_ok();
cx.config
.expect(["rustup", "default", "stable"])
.await
.is_ok();
cx.config
.expect(["rustup", "component", "remove", "rust-analysis"])
.await
.with_stderr(snapbox::str![[r#"
error: toolchain 'stable-[HOST_TUPLE]' does not contain component 'rust-analysis' for target '[HOST_TUPLE]'

"#]])
.is_err();
}

#[tokio::test]
async fn remove_target_no_toolchain() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
Expand Down
Loading