Skip to content

Commit 4bc4da5

Browse files
authored
Merge pull request #2465 from killercup/feature/fancy--version
Quick addition to `--version` to mention rustc
2 parents e20e14d + fbb5765 commit 4bc4da5

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/cli/rustup_mode.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,51 @@ 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 cfg = &mut common::set_globals(false, true)?;
79+
let cwd = std::env::current_dir()?;
80+
81+
if let Some(t) = process().args().find(|x| x.starts_with('+')) {
82+
debug!("Fetching rustc version from toolchain `{}`", t);
83+
cfg.set_toolchain_override(&t[1..]);
84+
}
85+
86+
let toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd)?.0;
87+
88+
Ok(toolchain.rustc_version())
89+
}
90+
91+
match rustc_version() {
92+
Ok(version) => info!("The currently active `rustc` version is `{}`", version),
93+
Err(err) => debug!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err),
94+
}
6595
return Ok(utils::ExitCode(0));
6696
}
67-
Err(e) if e.kind == clap::ErrorKind::MissingArgumentOrSubcommand => {
68-
writeln!(process().stdout().lock(), "{}", e.message)?;
97+
Err(clap::Error {
98+
kind: MissingArgumentOrSubcommand,
99+
message,
100+
..
101+
}) => {
102+
writeln!(process().stdout().lock(), "{}", message)?;
69103
return Ok(utils::ExitCode(1));
70104
}
71105
Err(e) => Err(e),

tests/cli-misc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ fn smoke_test() {
2727
});
2828
}
2929

30+
#[test]
31+
fn version_mentions_rustc_version_confusion() {
32+
setup(&|config| {
33+
let out = run(config, "rustup", &vec!["--version"], &[]);
34+
assert!(out.ok);
35+
assert!(out
36+
.stderr
37+
.contains("This is the version for the rustup toolchain manager"));
38+
39+
let out = run(config, "rustup", &vec!["+nightly", "--version"], &[]);
40+
assert!(out.ok);
41+
assert!(out
42+
.stderr
43+
.contains("The currently active `rustc` version is `1.3.0"));
44+
});
45+
}
46+
3047
#[test]
3148
fn no_colors_in_piped_error_output() {
3249
setup(&|config| {

0 commit comments

Comments
 (0)