Skip to content

Commit 54297ec

Browse files
authored
Merge pull request #2916 from K900/better-toolchain-error
Provide a more actionable error message when a toolchain is not selected
2 parents 5225e87 + e5e86c3 commit 54297ec

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
1414
#![recursion_limit = "1024"]
1515

16-
use std::path::PathBuf;
17-
1816
use anyhow::{anyhow, Result};
1917
use cfg_if::cfg_if;
2018
use rs_tracing::*;
@@ -62,18 +60,7 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
6260
utils::current_dir()?;
6361
utils::current_exe()?;
6462

65-
// The name of arg0 determines how the program is going to behave
66-
let arg0 = match process().var("RUSTUP_FORCE_ARG0") {
67-
Ok(v) => Some(v),
68-
Err(_) => process().args().next(),
69-
}
70-
.map(PathBuf::from);
71-
let name = arg0
72-
.as_ref()
73-
.and_then(|a| a.file_stem())
74-
.and_then(std::ffi::OsStr::to_str);
75-
76-
match name {
63+
match process().name().as_deref() {
7764
Some("rustup") => rustup_mode::main(),
7865
Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => {
7966
// NB: The above check is only for the prefix of the file

src/currentprocess.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ pub trait CurrentProcess:
8787
+ Debug
8888
{
8989
fn clone_boxed(&self) -> Box<dyn CurrentProcess>;
90+
91+
fn name(&self) -> Option<String>;
9092
}
9193

9294
// Machinery for Cloning boxes
@@ -107,6 +109,19 @@ where
107109
fn clone_boxed(&self) -> Box<dyn CurrentProcess + 'static> {
108110
Box::new(T::clone(self))
109111
}
112+
113+
fn name(&self) -> Option<String> {
114+
let arg0 = match self.var("RUSTUP_FORCE_ARG0") {
115+
Ok(v) => Some(v),
116+
Err(_) => self.args().next(),
117+
}
118+
.map(PathBuf::from);
119+
120+
arg0.as_ref()
121+
.and_then(|a| a.file_stem())
122+
.and_then(std::ffi::OsStr::to_str)
123+
.map(String::from)
124+
}
110125
}
111126

112127
impl Clone for Box<dyn CurrentProcess + 'static> {

src/errors.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::PathBuf;
88
use thiserror::Error as ThisError;
99
use url::Url;
1010

11+
use crate::currentprocess::process;
1112
use crate::dist::manifest::{Component, Manifest};
1213

1314
const TOOLSTATE_MSG: &str =
@@ -88,7 +89,11 @@ pub enum RustupError {
8889
ToolchainNotInstallable(String),
8990
#[error("toolchain '{0}' is not installed")]
9091
ToolchainNotInstalled(String),
91-
#[error("no override and no default toolchain set")]
92+
#[error(
93+
"rustup could not choose a version of {} to run, because one wasn't specified explicitly, and no default is configured.\n{}",
94+
process().name().unwrap_or_else(|| "Rust".into()),
95+
"help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain."
96+
)]
9297
ToolchainNotSelected,
9398
#[error("toolchain '{}' does not contain component {}{}{}", .name, .component, if let Some(suggestion) = .suggestion {
9499
format!("; did you mean '{}'?", suggestion)

tests/cli-exact.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,9 @@ fn default_none() {
599599
config,
600600
&["rustc", "--version"],
601601
"",
602-
"error: no override and no default toolchain set\n",
602+
"error: rustup could not choose a version of rustc to run, because one wasn't specified explicitly, and no default is configured.
603+
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
604+
",
603605
);
604606
})
605607
}

tests/cli-rustup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ fn rust_toolchain_toml() {
21792179
expect_err(
21802180
config,
21812181
&["rustc", "--version"],
2182-
"no override and no default toolchain set",
2182+
"rustup could not choose a version of rustc to run",
21832183
);
21842184

21852185
let cwd = config.current_dir();

tests/cli-v1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn rustc_no_default_toolchain() {
2222
expect_err(
2323
config,
2424
&["rustc"],
25-
"no override and no default toolchain set",
25+
"rustup could not choose a version of rustc to run",
2626
);
2727
});
2828
}
@@ -305,7 +305,7 @@ fn remove_override_no_default() {
305305
expect_err(
306306
config,
307307
&["rustc"],
308-
"no override and no default toolchain set",
308+
"rustup could not choose a version of rustc to run",
309309
);
310310
});
311311
});

tests/cli-v2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn rustc_no_default_toolchain() {
3030
expect_err(
3131
config,
3232
&["rustc"],
33-
"no override and no default toolchain set",
33+
"rustup could not choose a version of rustc to run",
3434
);
3535
});
3636
}
@@ -477,7 +477,7 @@ fn remove_override_no_default() {
477477
expect_err(
478478
config,
479479
&["rustc"],
480-
"no override and no default toolchain set",
480+
"rustup could not choose a version of rustc to run",
481481
);
482482
});
483483
});

0 commit comments

Comments
 (0)