Skip to content

Commit 366cda0

Browse files
committed
feat(errors)!: suggest rustup toolchain install when active toolchain is not installed
1 parent 6a3a04c commit 366cda0

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

src/cli/rustup_mode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ async fn update(
837837
d.update_extra(&components, &targets, profile, force, allow_downgrade)
838838
.await?
839839
}
840-
Err(RustupError::ToolchainNotInstalled(_)) => {
840+
Err(RustupError::ToolchainNotInstalled { .. }) => {
841841
DistributableToolchain::install(
842842
cfg,
843843
&desc,
@@ -1321,7 +1321,7 @@ async fn override_add(
13211321
let toolchain_name = toolchain.resolve(&cfg.get_default_host_triple()?)?;
13221322
match Toolchain::new(cfg, (&toolchain_name).into()) {
13231323
Ok(_) => {}
1324-
Err(e @ RustupError::ToolchainNotInstalled(_)) => match &toolchain_name {
1324+
Err(e @ RustupError::ToolchainNotInstalled { .. }) => match &toolchain_name {
13251325
ToolchainName::Custom(_) => Err(e)?,
13261326
ToolchainName::Official(desc) => {
13271327
let status =

src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ impl<'a> Cfg<'a> {
640640
// XXX: this awkwardness deals with settings file being locked already
641641
let toolchain_name = toolchain_name.resolve(&default_host_triple)?;
642642
match Toolchain::new(self, (&toolchain_name).into()) {
643-
Err(RustupError::ToolchainNotInstalled(_)) => {
643+
Err(RustupError::ToolchainNotInstalled { .. }) => {
644644
if matches!(toolchain_name, ToolchainName::Custom(_)) {
645645
bail!(
646646
"Toolchain {toolchain_name} in {} is custom and not installed",
@@ -817,7 +817,7 @@ impl<'a> Cfg<'a> {
817817
None => self.get_profile()?,
818818
};
819819
let (status, toolchain) = match DistributableToolchain::new(self, toolchain.clone()) {
820-
Err(RustupError::ToolchainNotInstalled(_)) => {
820+
Err(RustupError::ToolchainNotInstalled { .. }) => {
821821
DistributableToolchain::install(
822822
self,
823823
toolchain,

src/errors.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,19 @@ pub enum RustupError {
8787
#[error("toolchain '{0}' is not installable")]
8888
ToolchainNotInstallable(String),
8989
#[error(
90-
"toolchain '{0}' is not installed{}",
91-
if let ToolchainName::Official(t) = .0 {
92-
format!("\nhelp: run `rustup toolchain install {t}` to install it")
90+
"toolchain '{}' is not installed{}",
91+
.toolchain,
92+
if let ToolchainName::Official(t) = .toolchain {
93+
let t = if *.is_active { String::new() } else { format!(" {t}") };
94+
format!("\nhelp: run `rustup toolchain install{t}` to install it")
9395
} else {
9496
String::new()
9597
},
9698
)]
97-
ToolchainNotInstalled(ToolchainName),
99+
ToolchainNotInstalled {
100+
toolchain: ToolchainName,
101+
is_active: bool,
102+
},
98103
#[error("path '{0}' not found")]
99104
PathToolchainNotInstalled(PathBasedToolchainName),
100105
#[error(

src/toolchain.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ impl<'a> Toolchain<'a> {
5050
) -> anyhow::Result<Toolchain<'a>> {
5151
match Self::new(cfg, name) {
5252
Ok(tc) => Ok(tc),
53-
Err(RustupError::ToolchainNotInstalled(ToolchainName::Official(desc)))
54-
if install_if_missing =>
55-
{
53+
Err(RustupError::ToolchainNotInstalled {
54+
toolchain: ToolchainName::Official(desc),
55+
..
56+
}) if install_if_missing => {
5657
Ok(
5758
DistributableToolchain::install(cfg, &desc, &[], &[], cfg.get_profile()?, true)
5859
.await?
@@ -72,7 +73,7 @@ impl<'a> Toolchain<'a> {
7273
reason: &ActiveReason,
7374
) -> anyhow::Result<Self> {
7475
match Self::new(cfg, name.clone()) {
75-
Err(RustupError::ToolchainNotInstalled(_)) => (),
76+
Err(RustupError::ToolchainNotInstalled { .. }) => (),
7677
result => {
7778
return Ok(result?);
7879
}
@@ -106,7 +107,14 @@ impl<'a> Toolchain<'a> {
106107
let path = cfg.toolchain_path(&name);
107108
if !Toolchain::exists(cfg, &name)? {
108109
return Err(match name {
109-
LocalToolchainName::Named(name) => RustupError::ToolchainNotInstalled(name),
110+
LocalToolchainName::Named(name) => {
111+
let is_active =
112+
matches!(cfg.find_active_toolchain(), Ok(Some((tc, _))) if tc == name);
113+
RustupError::ToolchainNotInstalled {
114+
toolchain: name,
115+
is_active,
116+
}
117+
}
110118
LocalToolchainName::Path(name) => RustupError::PathToolchainNotInstalled(name),
111119
});
112120
}

tests/suite/cli_v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async fn remove_override_toolchain_err_handling() {
187187
"",
188188
for_host!(
189189
r"error: toolchain 'beta-{0}' is not installed
190-
help: run `rustup toolchain install beta-{0}` to install it
190+
help: run `rustup toolchain install` to install it
191191
"
192192
),
193193
)

tests/suite/cli_v2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ async fn remove_override_toolchain_err_handling() {
329329
"",
330330
for_host!(
331331
r"error: toolchain 'beta-{0}' is not installed
332-
help: run `rustup toolchain install beta-{0}` to install it
332+
help: run `rustup toolchain install` to install it
333333
"
334334
),
335335
)

0 commit comments

Comments
 (0)