Skip to content

Commit 7f157dc

Browse files
committed
Quick addition to --version to mention rustc
As discussed in #2447, rustup versions are getting closer to rustc versions (because the rustup maintainers are working too much I'm sure). This leads to confusion when running `rustup --version` while intending to run `rustc --version`. The change introduced here will print two additional (stderr) lines after rustup's default `--version` output. The first mentions this issue, and the second will print the output of `rustc --version` as a hint (by trivially calling out to `rustc`, whatever binary that may be).
1 parent 000079b commit 7f157dc

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/cli/rustup_mode.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,46 @@ where
5555
pub fn main() -> Result<utils::ExitCode> {
5656
self_update::cleanup_self_updater()?;
5757

58+
use clap::ErrorKind::*;
5859
let matches = match cli().get_matches_from_safe(process().args_os()) {
5960
Ok(matches) => Ok(matches),
60-
Err(e)
61-
if e.kind == clap::ErrorKind::HelpDisplayed
62-
|| e.kind == clap::ErrorKind::VersionDisplayed =>
63-
{
64-
writeln!(process().stdout().lock(), "{}", e.message)?;
61+
Err(clap::Error {
62+
kind: HelpDisplayed,
63+
message,
64+
..
65+
}) => {
66+
writeln!(process().stdout().lock(), "{}", message)?;
67+
return Ok(utils::ExitCode(0));
68+
}
69+
Err(clap::Error {
70+
kind: VersionDisplayed,
71+
message,
72+
..
73+
}) => {
74+
writeln!(process().stdout().lock(), "{}", message)?;
75+
info!("This is the version for the rustup toolchain manager, not the rustc compiler.");
76+
77+
fn rustc_version() -> std::result::Result<String, Box<dyn std::error::Error>> {
78+
let cmd = Command::new("rustc").arg("--version").output()?;
79+
if cmd.status.success() {
80+
Ok(String::from_utf8_lossy(&cmd.stdout).trim().into())
81+
} else {
82+
Err(String::from_utf8_lossy(&cmd.stderr).into())
83+
}
84+
}
85+
86+
match rustc_version() {
87+
Ok(version) => info!("The currently active `rustc` version is `{}`", version),
88+
Err(err) => debug!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err),
89+
}
6590
return Ok(utils::ExitCode(0));
6691
}
67-
Err(e) if e.kind == clap::ErrorKind::MissingArgumentOrSubcommand => {
68-
writeln!(process().stdout().lock(), "{}", e.message)?;
92+
Err(clap::Error {
93+
kind: MissingArgumentOrSubcommand,
94+
message,
95+
..
96+
}) => {
97+
writeln!(process().stdout().lock(), "{}", message)?;
6998
return Ok(utils::ExitCode(1));
7099
}
71100
Err(e) => Err(e),

0 commit comments

Comments
 (0)