From 7f157dcca7cd29eb55654718b12ae8fd436db0d1 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Wed, 19 Aug 2020 18:11:07 +0200 Subject: [PATCH 1/4] 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). --- src/cli/rustup_mode.rs | 43 +++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 1af687cc09..1eacbd4ef6 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -55,17 +55,46 @@ where pub fn main() -> Result { self_update::cleanup_self_updater()?; + use clap::ErrorKind::*; let matches = match cli().get_matches_from_safe(process().args_os()) { Ok(matches) => Ok(matches), - Err(e) - if e.kind == clap::ErrorKind::HelpDisplayed - || e.kind == clap::ErrorKind::VersionDisplayed => - { - writeln!(process().stdout().lock(), "{}", e.message)?; + Err(clap::Error { + kind: HelpDisplayed, + message, + .. + }) => { + writeln!(process().stdout().lock(), "{}", message)?; + return Ok(utils::ExitCode(0)); + } + Err(clap::Error { + kind: VersionDisplayed, + message, + .. + }) => { + writeln!(process().stdout().lock(), "{}", message)?; + info!("This is the version for the rustup toolchain manager, not the rustc compiler."); + + fn rustc_version() -> std::result::Result> { + let cmd = Command::new("rustc").arg("--version").output()?; + if cmd.status.success() { + Ok(String::from_utf8_lossy(&cmd.stdout).trim().into()) + } else { + Err(String::from_utf8_lossy(&cmd.stderr).into()) + } + } + + match rustc_version() { + Ok(version) => info!("The currently active `rustc` version is `{}`", version), + Err(err) => debug!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err), + } return Ok(utils::ExitCode(0)); } - Err(e) if e.kind == clap::ErrorKind::MissingArgumentOrSubcommand => { - writeln!(process().stdout().lock(), "{}", e.message)?; + Err(clap::Error { + kind: MissingArgumentOrSubcommand, + message, + .. + }) => { + writeln!(process().stdout().lock(), "{}", message)?; return Ok(utils::ExitCode(1)); } Err(e) => Err(e), From d708b1109ecbbf5e15f204e1c9a4abbdefd32acf Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Thu, 20 Aug 2020 17:41:49 +0200 Subject: [PATCH 2/4] `--version`: Show current toolchain rustc version --- src/cli/rustup_mode.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 1eacbd4ef6..d4a7e44c3c 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -75,12 +75,10 @@ pub fn main() -> Result { info!("This is the version for the rustup toolchain manager, not the rustc compiler."); fn rustc_version() -> std::result::Result> { - let cmd = Command::new("rustc").arg("--version").output()?; - if cmd.status.success() { - Ok(String::from_utf8_lossy(&cmd.stdout).trim().into()) - } else { - Err(String::from_utf8_lossy(&cmd.stderr).into()) - } + let cfg = &mut common::set_globals(false, true)?; + let cwd = std::env::current_dir()?; + let toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd)?.0; + Ok(toolchain.rustc_version()) } match rustc_version() { From 910b6aeac0259817eb92a157d663e27b6b945d07 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Sun, 23 Aug 2020 15:43:17 +0200 Subject: [PATCH 3/4] `+toolchain --version` shows correct rustc version --- src/cli/rustup_mode.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index d4a7e44c3c..3fb37e3eb6 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -77,7 +77,14 @@ pub fn main() -> Result { fn rustc_version() -> std::result::Result> { let cfg = &mut common::set_globals(false, true)?; let cwd = std::env::current_dir()?; + + if let Some(t) = process().args().find(|x| x.starts_with('+')) { + debug!("Fetching rustc version from toolchain `{}`", t); + cfg.set_toolchain_override(&t[1..]); + } + let toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd)?.0; + Ok(toolchain.rustc_version()) } From fbb57658fcd557114637d6bee8e56099e6642eb3 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Mon, 24 Aug 2020 16:37:10 +0200 Subject: [PATCH 4/4] Add test to ensure new rustup version output works --- tests/cli-misc.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/cli-misc.rs b/tests/cli-misc.rs index 5fbeab0628..0adcf3ccfd 100644 --- a/tests/cli-misc.rs +++ b/tests/cli-misc.rs @@ -27,6 +27,23 @@ fn smoke_test() { }); } +#[test] +fn version_mentions_rustc_version_confusion() { + setup(&|config| { + let out = run(config, "rustup", &vec!["--version"], &[]); + assert!(out.ok); + assert!(out + .stderr + .contains("This is the version for the rustup toolchain manager")); + + let out = run(config, "rustup", &vec!["+nightly", "--version"], &[]); + assert!(out.ok); + assert!(out + .stderr + .contains("The currently active `rustc` version is `1.3.0")); + }); +} + #[test] fn no_colors_in_piped_error_output() { setup(&|config| {