Skip to content

Commit 86b1ef3

Browse files
committed
Improve error message for adding unknown target
Signed-off-by: hi-rustin <[email protected]>
1 parent 929385c commit 86b1ef3

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/errors.rs

Lines changed: 17 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,19 @@ 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+
},
117133
#[error("unknown metadata version: '{0}'")]
118134
UnknownMetadataVersion(String),
119135
#[error("manifest version '{0}' is not supported")]

src/toolchain/distributable.rs

Lines changed: 16 additions & 2 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
}

tests/suite/cli_v2.rs

Lines changed: 3 additions & 3 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
}

0 commit comments

Comments
 (0)