Skip to content

feat: improve error message for rustup which #4429

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Bogay
Copy link

@Bogay Bogay commented Jul 26, 2025

Close #3321

This PR improves the error message for rustup which by providing a suggested command to install the missing component, provided that we know which one it is.

Limitations

Some edge cases are not coverd, e.g. if a binary is expected to be provided by a component but not found on disk. (which is handled in DistributableToolchain::recursion_error)

Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

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

Seems like a good start!

Obviously this should pass CI -- would also be great if you could make sure there are tests to cover the new code paths.

let toolchain_name = toolchain.name();
let Some(component_name) = component_for_bin(binary) else {
return Err(anyhow!(
"Unknown binary '{binary}' in toolchain '{toolchain_name}'.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should drop the capital at the start and period at the end here for consistency.

Comment on lines 1058 to 1060
return Err(anyhow!(
"'{binary}' is not installed for the toolchain '{toolchain_name}'.\nTo install, run `rustup component add {selector}{component_name}`"
));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: idiomatically, no need to use return here, can just make Err(..) the tail expression.

Comment on lines 1051 to 1045
let active = matches!(cfg.active_toolchain(), Ok(Some((t, _))) if &t == toolchain_name);
let selector = if active {
String::new()
} else {
format!("--toolchain {} ", toolchain.name())
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems little roundabout, what about something more like:

let selector = match cfg.active_toolchain() {
     Ok(Some((t, _))) if t == toolchain_name => String::new(),
     _ => format!("--toolchain {} ", toolchain.name()),
};

@@ -47,6 +46,7 @@ use crate::{
},
utils::{self, ExitCode},
};
use crate::{component_for_bin, dist::AutoInstallMode};
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're going to move this around, would be nicer to move both of these into the big crate block just above. 👍

@Bogay Bogay force-pushed the 3321-improve-rustup-which-error-message branch from fbfb71c to aa79802 Compare July 27, 2025 07:46
Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

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

LGTM! Would be good to squash your commits into a single one, and mark the PR as ready for review.

Copy link
Member

@rami3l rami3l left a comment

Choose a reason for hiding this comment

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

Thanks @djc for doing the review! I have one question though:

What do you think about the edge case mentioned here?

@djc
Copy link
Contributor

djc commented Jul 27, 2025

What do you think about the edge case mentioned here?

The situation with this PR merged seems strictly better to me than the current state on master. Maybe there are still further improvements to be made but it's not obvious to me they should necessarily be a part of this PR.

@rami3l
Copy link
Member

rami3l commented Jul 27, 2025

What do you think about the edge case mentioned here?

The situation with this PR merged seems strictly better to me than the current state on master. Maybe there are still further improvements to be made but it's not obvious to me they should necessarily be a part of this PR.

@djc I agree. That said, it might still be worth it if @Bogay can put these considerations at the top of this thread (in a separate section, say Limitations) instead of a separate commit comment :)

@Bogay Bogay force-pushed the 3321-improve-rustup-which-error-message branch from 54ef67d to 02c0813 Compare July 28, 2025 16:07
@Bogay Bogay marked this pull request as ready for review July 28, 2025 16:09
@Bogay
Copy link
Author

Bogay commented Jul 28, 2025

I have put these considerations to the PR description and squash my commits into one. I think it's ready for review now. Regarding the edge case I mentioned, I am not sure whether I should try to solve it? or wait for someone to encounter it and open an issue.

@Bogay Bogay requested review from djc and rami3l July 28, 2025 16:51
Copy link
Member

@rami3l rami3l left a comment

Choose a reason for hiding this comment

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

LGTM, and thanks for the patch!

@djc Can you confirm that your concerns have been properly addressed as well?

@rami3l
Copy link
Member

rami3l commented Jul 29, 2025

I have put these considerations to the PR description and squash my commits into one. I think it's ready for review now. Regarding the edge case I mentioned, I am not sure whether I should try to solve it? or wait for someone to encounter it and open an issue.

@Bogay If you think a further PR is possible, I would greatly appreciate it :)

Copy link
Author

@Bogay Bogay left a comment

Choose a reason for hiding this comment

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

I've updated the relevant error message to suggest how users can install the missing component. For the long term, I'm thinking we should find a more consistent way to manage error messages across the project. Do you have any thoughts on how we could approach this, perhaps with custom error types or utility functions?

Comment on lines 1714 to 1729
"To install, try `rustup component add --toolchain {} rust-docs`",
"run `rustup component add --toolchain {} rust-docs` to install it",
Copy link
Author

Choose a reason for hiding this comment

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

Because info! add info: prefix to output message and I think info: help: looks a little weird. I don't add the help: prefix here. Not sure whether it's better to merge these two lines info the error message below?

Copy link
Member

@rami3l rami3l Jul 29, 2025

Choose a reason for hiding this comment

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

Nice catch! Yeah, I think in that case it'd be better to just do a single info!() call instead of two...

Copy link
Author

Choose a reason for hiding this comment

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

Updated. We have only one info!() call now 👍

Bogay added 2 commits July 30, 2025 00:03
Also adds more tests for `rustup which`. Includes:
- ask binary included by uninstalled component
- ask binary included by uninstalled component with non-default
  toolchain
- ask binary not known to be included by what toolchain
to suggest command to install missing components. make them more
consistent.
@Bogay Bogay force-pushed the 3321-improve-rustup-which-error-message branch from 1248e0e to 0bcf123 Compare July 29, 2025 16:03
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.

Give a more helpful error message for rustup which rustfmt if it's not installed
3 participants