Skip to content

Commit ee8a5df

Browse files
committed
Improve error message for removing uninstalled target
Signed-off-by: hi-rustin <[email protected]>
1 parent 86b1ef3 commit ee8a5df

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ pub(crate) enum RustupError {
130130
target: TargetTriple,
131131
suggestion: Option<String>,
132132
},
133+
#[error("toolchain '{}' does not have target '{}' installed{}\n", .desc, .target,
134+
if let Some(suggestion) = .suggestion {
135+
format!("; did you mean '{suggestion}'?")
136+
} else {
137+
"".to_string()
138+
})]
139+
TargetNotInstalled {
140+
desc: ToolchainDesc,
141+
target: TargetTriple,
142+
suggestion: Option<String>,
143+
},
133144
#[error("unknown metadata version: '{0}'")]
134145
UnknownMetadataVersion(String),
135146
#[error("manifest version '{0}' is not supported")]

src/toolchain/distributable.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,25 @@ impl<'a> DistributableToolchain<'a> {
462462
if config.components.contains(&wildcard_component) {
463463
component = wildcard_component;
464464
} else {
465+
let suggestion =
466+
self.get_component_suggestion(&component, &config, &manifest, true);
467+
// Check if the target is installed.
468+
if !config
469+
.components
470+
.iter()
471+
.any(|c| c.target() == component.target())
472+
{
473+
return Err(RustupError::TargetNotInstalled {
474+
desc: self.desc.clone(),
475+
target: component.target.expect("component target should be known"),
476+
suggestion,
477+
}
478+
.into());
479+
}
465480
return Err(RustupError::UnknownComponent {
466481
desc: self.desc.clone(),
467482
component: component.description(&manifest),
468-
suggestion: self.get_component_suggestion(&component, &config, &manifest, true),
483+
suggestion,
469484
}
470485
.into());
471486
}

tests/suite/cli_v2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ fn remove_target_not_installed() {
866866
config.expect_err(
867867
&["rustup", "target", "remove", clitools::CROSS_ARCH1],
868868
&format!(
869-
"toolchain 'nightly-{}' does not contain component 'rust-std' for target '{}'",
869+
"toolchain 'nightly-{}' does not have target '{}' installed",
870870
this_host_triple(),
871871
clitools::CROSS_ARCH1
872872
),
@@ -896,7 +896,7 @@ fn remove_target_bogus() {
896896
config.expect_ok(&["rustup", "default", "nightly"]);
897897
config.expect_err(
898898
&["rustup", "target", "remove", "bogus"],
899-
"does not contain component 'rust-std' for target 'bogus'",
899+
"does not have target 'bogus' installed",
900900
);
901901
});
902902
}
@@ -941,7 +941,7 @@ fn remove_target_again() {
941941
config.expect_err(
942942
&["rustup", "target", "remove", clitools::CROSS_ARCH1],
943943
&format!(
944-
"toolchain 'nightly-{}' does not contain component 'rust-std' for target '{}'",
944+
"toolchain 'nightly-{}' does not have target '{}' installed",
945945
this_host_triple(),
946946
clitools::CROSS_ARCH1
947947
),

0 commit comments

Comments
 (0)