Skip to content
Open
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
123 changes: 79 additions & 44 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
config::Config as DistConfig,
manifest::{Component, Manifest},
},
toolchain::{Toolchain, ToolchainName, ToolchainPath},
toolchain::{ChannelToolchain, Toolchain, ToolchainName, ToolchainPath},
};

pub(crate) const DEFAULT_STABLE_HINT: &str = "help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.";
Expand All @@ -40,28 +40,14 @@ impl TargetSuggestion {
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);
};

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_targets) = toolchain.installed_targets() else {
for (name, channel_toolchain) in other_channel_toolchains(cfg, desc) {
let Ok(installed_targets) = channel_toolchain.toolchain.installed_targets() else {
continue;
};

if installed_targets.contains(target) {
return Some(Self::Toolchain {
name: toolchain_name.to_string(),
name: name.to_string(),
target: target.clone(),
});
}
Expand All @@ -84,7 +70,11 @@ impl fmt::Display for TargetSuggestion {

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

Expand All @@ -96,46 +86,91 @@ impl ComponentSuggestion {
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;
for (name, channel_toolchain) in other_channel_toolchains(cfg, desc) {
if toolchain_has_installed_component(&channel_toolchain.toolchain, component) {
return Some(Self::Toolchain {
name: name.to_string(),
component: manifest.short_name(component).to_string(),
action: "remove",
});
}
}

let Ok(toolchain) = Toolchain::new(cfg, toolchain_name.clone().into()) else {
continue;
};
let Ok(installed_components) = toolchain.installed_components() else {
continue;
};
component_suggestion(desc, component, config, manifest, true).map(Self::Component)
}

if installed_components
.iter()
.any(|c| names.iter().any(|n| n == c.name()))
{
pub(crate) fn for_addition(
Comment thread
djc marked this conversation as resolved.
desc: &ChannelToolchainName,
component: &Component,
config: &DistConfig,
manifest: &Manifest,
cfg: &Cfg<'_>,
) -> Option<Self> {
for (name, channel_toolchain) in other_channel_toolchains(cfg, desc) {
if toolchain_has_available_component(&channel_toolchain, component) {
return Some(Self::Toolchain {
name: toolchain_name.to_string(),
name: name.to_string(),
component: manifest.short_name(component).to_string(),
action: "add",
});
}
}

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

fn other_channel_toolchains<'a>(
cfg: &'a Cfg<'a>,
desc: &ChannelToolchainName,
) -> impl Iterator<Item = (ChannelToolchainName, ChannelToolchain<'a>)> {
cfg.list_toolchains(true)
.ok()
.into_iter()
.flatten()
.filter_map(move |toolchain_name| match toolchain_name {
ToolchainName::Channel(toolchain_desc) if &toolchain_desc != desc => {
let channel_toolchain = ChannelToolchain::new(cfg, toolchain_desc.clone()).ok()?;
Some((toolchain_desc, channel_toolchain))
}
_ => None,
})
}

fn component_matches(component: &Component, candidate_name: &str) -> bool {
candidate_name == component.name() || candidate_name == component.wildcard().name()
}

fn toolchain_has_installed_component(toolchain: &Toolchain<'_>, component: &Component) -> bool {
let Ok(installed_components) = toolchain.installed_components() else {
return false;
};
installed_components
.iter()
.any(|installed| component_matches(component, installed.name()))
}

fn toolchain_has_available_component(
toolchain: &ChannelToolchain<'_>,
component: &Component,
) -> bool {
let Ok(components) = toolchain.components() else {
return false;
};
components
.iter()
.any(|status| status.available && component_matches(component, &status.component.name()))
}

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::Toolchain {
name,
component,
action,
} => {
write!(f, "try `rustup +{name} component {action} {component}`")
}
Self::Component(name) => write!(f, "did you mean '{name}'?"),
}
Expand Down
11 changes: 9 additions & 2 deletions src/toolchain/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,31 @@ impl<'a> ChannelToolchain<'a> {
}

let config = manifestation.read_config()?.unwrap_or_default();
let suggestion = component_suggestion(desc, &component, &config, &manifest, false);

if targ_pkg
.components
.iter()
.any(|c| c.target() == component.target())
{
let suggestion = ComponentSuggestion::for_addition(
desc,
&component,
&config,
&manifest,
self.toolchain.cfg,
);
return Err(RustupError::UnknownComponents {
desc: desc.clone(),
components: vec![UnknownComponentInfo {
name: manifest.short_name(&component).to_string(),
description: manifest.description(&component),
suggestion: suggestion.map(ComponentSuggestion::Component),
suggestion,
}],
}
.into());
}

let suggestion = component_suggestion(desc, &component, &config, &manifest, false);
let target = component.target.expect("component target should be known");
if let Some(platform) = Platform::find(&target) {
return Err(RustupError::UnavailableTarget {
Expand Down
39 changes: 39 additions & 0 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,45 @@ error: toolchain 'stable-[HOST_TUPLE]' does not contain component 'rust-analysis
.is_err();
}

#[tokio::test]
async fn add_component_not_available_with_suggestion() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
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", "rls-preview"])
.await
.with_stderr(snapbox::str![[r#"
error: toolchain 'stable-[HOST_TUPLE]' does not contain component 'rls-preview' for target '[HOST_TUPLE]'
help: try `rustup +nightly-[HOST_TUPLE] component add rls-preview`

"#]])
.is_err();
}

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

"#]])
.is_err();
}

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