Skip to content

Commit 120331b

Browse files
authored
Merge pull request #3366 from hi-rustin/rustin-patch-target-error
Improve error message for adding/removing invalid target
2 parents 7a46e17 + ee8a5df commit 120331b

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

src/errors.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::path::PathBuf;
1010
use thiserror::Error as ThisError;
1111
use url::Url;
1212

13-
use crate::{currentprocess::process, dist::dist::ToolchainDesc};
13+
use crate::{
14+
currentprocess::process,
15+
dist::dist::{TargetTriple, ToolchainDesc},
16+
};
1417
use crate::{
1518
dist::manifest::{Component, Manifest},
1619
toolchain::names::{PathBasedToolchainName, ToolchainName},
@@ -114,6 +117,30 @@ pub(crate) enum RustupError {
114117
component: String,
115118
suggestion: Option<String>,
116119
},
120+
#[error("toolchain '{}' does not support target '{}'{}\n\
121+
note: you can see a list of supported targets with `rustc --print=target-list`\n\
122+
note: if you are adding support for a new target to rustc itself, see https://rustc-dev-guide.rust-lang.org/building/new-target.html", .desc, .target,
123+
if let Some(suggestion) = .suggestion {
124+
format!("; did you mean '{suggestion}'?")
125+
} else {
126+
"".to_string()
127+
})]
128+
UnknownTarget {
129+
desc: ToolchainDesc,
130+
target: TargetTriple,
131+
suggestion: Option<String>,
132+
},
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+
},
117144
#[error("unknown metadata version: '{0}'")]
118145
UnknownMetadataVersion(String),
119146
#[error("manifest version '{0}' is not supported")]

src/toolchain/distributable.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ impl<'a> DistributableToolchain<'a> {
7070
component = wildcard_component;
7171
} else {
7272
let config = manifestation.read_config()?.unwrap_or_default();
73+
let suggestion =
74+
self.get_component_suggestion(&component, &config, &manifest, false);
75+
// Check if the target is supported.
76+
if !targ_pkg
77+
.components
78+
.iter()
79+
.any(|c| c.target() == component.target())
80+
{
81+
return Err(RustupError::UnknownTarget {
82+
desc: self.desc.clone(),
83+
target: component.target.expect("component target should be known"),
84+
suggestion,
85+
}
86+
.into());
87+
}
7388
return Err(RustupError::UnknownComponent {
7489
desc: self.desc.clone(),
7590
component: component.description(&manifest),
76-
suggestion: self
77-
.get_component_suggestion(&component, &config, &manifest, false),
91+
suggestion,
7892
}
7993
.into());
8094
}
@@ -448,10 +462,25 @@ impl<'a> DistributableToolchain<'a> {
448462
if config.components.contains(&wildcard_component) {
449463
component = wildcard_component;
450464
} 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+
}
451480
return Err(RustupError::UnknownComponent {
452481
desc: self.desc.clone(),
453482
component: component.description(&manifest),
454-
suggestion: self.get_component_suggestion(&component, &config, &manifest, true),
483+
suggestion,
455484
}
456485
.into());
457486
}

tests/suite/cli_v2.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ fn add_target_bogus() {
752752
config.expect_ok(&["rustup", "default", "nightly"]);
753753
config.expect_err(
754754
&["rustup", "target", "add", "bogus"],
755-
"does not contain component 'rust-std' for target 'bogus'\n\
756-
note: not all platforms have the standard library pre-compiled: https://doc.rust-lang.org/nightly/rustc/platform-support.html\n\
757-
help: consider using `cargo build -Z build-std` instead",
755+
"does not support target 'bogus'\n\
756+
note: you can see a list of supported targets with `rustc --print=target-list`\n\
757+
note: if you are adding support for a new target to rustc itself, see https://rustc-dev-guide.rust-lang.org/building/new-target.html",
758758
);
759759
});
760760
}
@@ -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)