Skip to content

Commit 7525e07

Browse files
committed
fix(toolchain): forbid toolchain names starting with +
When users accidentally use '+stable' with 'rustup run', we now provide a message instructing them to remove the '+' prefix.
1 parent c75045d commit 7525e07

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/toolchain/names.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub enum InvalidName {
6767
ToolchainPath(String),
6868
#[error("invalid toolchain name '{0}'")]
6969
ToolchainName(String),
70+
#[error("invalid toolchain name '+{0}'; valid toolchain names do not start with '+'")]
71+
PlusPrefix(String),
7072
}
7173

7274
macro_rules! from_variant {
@@ -117,6 +119,9 @@ macro_rules! try_from_str {
117119

118120
/// Common validate rules for all sorts of toolchain names
119121
fn validate(candidate: &str) -> Result<&str, InvalidName> {
122+
if let Some(without_plus) = candidate.strip_prefix('+') {
123+
return Err(InvalidName::PlusPrefix(without_plus.to_string()));
124+
}
120125
let normalized_name = candidate.trim_end_matches('/');
121126
if normalized_name.is_empty() {
122127
Err(InvalidName::ToolchainName(candidate.into()))
@@ -501,8 +506,9 @@ mod tests {
501506

502507
prop_compose! {
503508
fn arb_custom_name()
504-
(s in r"[^\\/]+") -> String {
509+
(s in r"[^\\/+][^\\/]*") -> String {
505510
// perhaps need to filter 'none' and partial toolchains - but they won't typically be generated anyway.
511+
// Also filter '+' prefix as that's reserved for +toolchain syntax.
506512
s
507513
}
508514
}

tests/suite/cli_rustup.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,25 @@ error:[..] relative path toolchain '[..]/toolchains/nightly-[HOST_TRIPLE]'
22222222
.is_err();
22232223
}
22242224

2225+
#[tokio::test]
2226+
async fn run_with_plus_prefix_fails_with_helpful_message() {
2227+
let cx = CliTestContext::new(Scenario::SimpleV2).await;
2228+
cx.config
2229+
.expect(["rustup", "default", "stable"])
2230+
.await
2231+
.is_ok();
2232+
2233+
cx.config
2234+
.expect(["rustup", "run", "+stable", "cargo", "check"])
2235+
.await
2236+
.with_stderr(snapbox::str![[r#"
2237+
...
2238+
error:[..] invalid toolchain name '+stable'; valid toolchain names do not start with '+'
2239+
...
2240+
"#]])
2241+
.is_err();
2242+
}
2243+
22252244
#[tokio::test]
22262245
async fn plus_override_abspath_is_supported() {
22272246
let cx = CliTestContext::new(Scenario::SimpleV2).await;

0 commit comments

Comments
 (0)