diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 54845b007f..5d482e046c 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -547,9 +547,10 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result info!("The currently active `rustc` version is `{}`", version), - Err(err) => trace!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err), + match cfg.active_rustc_version() { + Ok(Some(version)) => info!("The currently active `rustc` version is `{version}`"), + Ok(None) => info!("No `rustc` is currently active"), + Err(err) => trace!("Failed to display the current `rustc` version: {err}"), } return Ok(utils::ExitCode(0)); } diff --git a/src/config.rs b/src/config.rs index 2b4cb873ae..067da9db01 100644 --- a/src/config.rs +++ b/src/config.rs @@ -694,17 +694,16 @@ impl<'a> Cfg<'a> { } #[tracing::instrument(level = "trace")] - pub(crate) async fn active_rustc_version(&mut self) -> Result { + pub(crate) fn active_rustc_version(&mut self) -> Result> { if let Some(t) = self.process.args().find(|x| x.starts_with('+')) { trace!("Fetching rustc version from toolchain `{}`", t); self.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?); } - Ok(self - .find_or_install_active_toolchain() - .await? - .0 - .rustc_version()) + let Some((name, _)) = self.find_active_toolchain()? else { + return Ok(None); + }; + Ok(Some(Toolchain::new(self, name)?.rustc_version())) } pub(crate) async fn resolve_toolchain( diff --git a/tests/suite/cli_misc.rs b/tests/suite/cli_misc.rs index 3dee46f337..e8f3588d73 100644 --- a/tests/suite/cli_misc.rs +++ b/tests/suite/cli_misc.rs @@ -21,21 +21,25 @@ async fn smoke_test() { #[tokio::test] async fn version_mentions_rustc_version_confusion() { - let cx = CliTestContext::new(Scenario::SimpleV2).await; - let out = cx.config.run("rustup", vec!["--version"], &[]).await; - assert!(out.ok); - assert!(out - .stderr - .contains("This is the version for the rustup toolchain manager")); + let mut cx = CliTestContext::new(Scenario::SimpleV2).await; - let out = cx - .config - .run("rustup", vec!["+nightly", "--version"], &[]) + cx.config + .expect_stderr_ok( + &["rustup", "--version"], + "This is the version for the rustup toolchain manager", + ) + .await; + + cx.config + .expect_ok(&["rustup", "toolchain", "install", "nightly"]) + .await; + + cx.config + .expect_stderr_ok( + &["rustup", "+nightly", "--version"], + "The currently active `rustc` version is `1.3.0", + ) .await; - assert!(out.ok); - assert!(out - .stderr - .contains("The currently active `rustc` version is `1.3.0")); } #[tokio::test]