Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(errors)!: improve error messages for RustupError::ToolchainNotInstalled #4258

Merged
merged 5 commits into from
Mar 19, 2025

Conversation

rami3l
Copy link
Member

@rami3l rami3l commented Mar 18, 2025

Closes #4212.

@rami3l rami3l force-pushed the fix/help-run-install branch 2 times, most recently from bc1dd09 to 4b961f6 Compare March 18, 2025 14:52
@rami3l rami3l force-pushed the fix/help-run-install branch from 6231595 to 76816ea Compare March 18, 2025 15:43
};

Ok(Some(((&toolchain).into(), reason)))
pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
Copy link
Member Author

@rami3l rami3l Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to extract a sync function here because v1.28.1 has made the Cfg::find_active_toolchain() async again, but we really need to find the active toolchain in a sync context in the commits that follow... (sigh)

Copy link
Member Author

@rami3l rami3l Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I suggest renaming Cfg::find_active_toolchain() to something else. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I'm struggling to think of a good name.

Copy link
Member Author

@rami3l rami3l Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisDenton I'm currently thinking about something like:

Before After Remarks
find_active_toolchain() maybe_ensure_active_toolchain() Dispatches to one of the following (async)
find_active_toolchain() (old implementation in 1.28.0) active_toolchain() Queries the active toolchain (sync)
find_or_install_active_toolchain() ensure_active_toolchain() Ensures the installation of the active toolchain (async)

... how does that sound?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me. I think those names do make the relationship between them clearer to me at least.

@rami3l rami3l force-pushed the fix/help-run-install branch 2 times, most recently from 1cd3ec8 to 2a91a8b Compare March 19, 2025 02:20
@rami3l rami3l force-pushed the fix/help-run-install branch from 2a91a8b to c2ff01d Compare March 19, 2025 04:08
@@ -763,7 +763,7 @@ async fn default_(
}
};

if let Some((toolchain, reason)) = cfg.maybe_ensure_active_toolchain(None).await? {
if let Some((toolchain, reason)) = cfg.active_toolchain()? {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're just emitting an info!(), there's no need to install anything.

@rami3l rami3l requested a review from djc March 19, 2025 04:29
@rami3l rami3l marked this pull request as ready for review March 19, 2025 04:29
@rami3l rami3l requested a review from ChrisDenton March 19, 2025 07:50
Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@rami3l rami3l added this pull request to the merge queue Mar 19, 2025
Merged via the queue into rust-lang:master with commit 1015b50 Mar 19, 2025
29 checks passed
@rami3l rami3l deleted the fix/help-run-install branch March 19, 2025 15:46
if !should_install_active {
return Ok(Some(((&toolchain).into(), reason)));
return self.active_toolchain();
Copy link
Member Author

@rami3l rami3l Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If should_install_active is set to false, we return self.active_toolchain() which was the old behavior of find_active_toolchain() in v1.28.0, corresponding to the last 2 early-returning branch of the previous match that gets deleted in this change:

rustup/src/config.rs

Lines 517 to 528 in e2d9e7e

pub(crate) fn find_active_toolchain(
&self,
) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
Ok(
if let Some((override_config, reason)) = self.find_override_config()? {
Some((override_config.into_local_toolchain_name(), reason))
} else {
self.get_default()?
.map(|x| (x.into(), ActiveReason::Default))
},
)
}

}

let components = components.iter().map(AsRef::as_ref).collect::<Vec<_>>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If should_install is set to true, we delegate the installation to find_or_install_active_toolchain() which has the same logic:

rustup/src/config.rs

Lines 758 to 765 in e2d9e7e

if let OverrideCfg::Official {
toolchain,
components,
targets,
profile,
} = override_config
{
self.ensure_installed(

rustup/src/config.rs

Lines 813 to 844 in e2d9e7e

let components: Vec<_> = components.iter().map(AsRef::as_ref).collect();
let targets: Vec<_> = targets.iter().map(AsRef::as_ref).collect();
let profile = match profile {
Some(profile) => profile,
None => self.get_profile()?,
};
let (status, toolchain) = match DistributableToolchain::new(self, toolchain.clone()) {
Err(RustupError::ToolchainNotInstalled { .. }) => {
DistributableToolchain::install(
self,
toolchain,
&components,
&targets,
profile,
false,
)
.await?
}
Ok(mut distributable) => {
if verbose {
(self.notify_handler)(Notification::UsingExistingToolchain(toolchain));
}
let status = if !distributable.components_exist(&components, &targets)? {
distributable.update(&components, &targets, profile).await?
} else {
UpdateStatus::Unchanged
};
(status, distributable)
}
Err(e) => return Err(e.into()),
};
Ok((status, toolchain.into()))

@@ -675,17 +675,13 @@ impl<'a> Cfg<'a> {

// XXX: this awkwardness deals with settings file being locked already
let toolchain_name = toolchain_name.resolve(&default_host_triple)?;
match Toolchain::new(self, (&toolchain_name).into()) {
Copy link
Member Author

@rami3l rami3l Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have manually inlined Toolchain::new() here and removed the statement causing an infinite recursion, because it was not needed; in fact, to allow any RustupError::ToolchainNotInstalled to happen with this call, the toolchain must not exist:

rustup/src/toolchain.rs

Lines 106 to 115 in f9edccd

pub(crate) fn new(cfg: &'a Cfg<'a>, name: LocalToolchainName) -> Result<Self, RustupError> {
let path = cfg.toolchain_path(&name);
if !Toolchain::exists(cfg, &name)? {
return Err(match name {
LocalToolchainName::Named(name) => RustupError::ToolchainNotInstalled { name },
LocalToolchainName::Path(name) => RustupError::PathToolchainNotInstalled(name),
});
}
Ok(Self { cfg, name, path })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recommended toolchain install command line isn't exactly right
2 participants