Repository: taiki-e/cargo-hack (language: rust)

Issue:
Rollup rustup output in a CI log group
Current output for an MSRV job looks like:
```
Run cargo hack check --all-targets --rust-version --workspace --ignore-private
info: skipped running on private package `cargo-test-macro`
info: skipped running on private package `cargo-test-support`
info: skipped running on private package `mdman`
info: skipped running on private package `resolver-tests`
info: skipped running on private package `semver-check`
info: skipped running on private package `xtask-build-man`
info: skipped running on private package `xtask-bump-check`
info: skipped running on private package `xtask-stale-label`
info: skipped running on private package `benchsuite`
info: skipped running on private package `capture`
info: syncing channel updates for '1.70-x86_64-unknown-linux-gnu'
info: latest update on 2023-06-01, rust version 1.70.0 (90c541806 2023-05-31)
info: downloading component 'cargo'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
info: installing component 'rust-std'
info: installing component 'rustc'

  1.70-x86_64-unknown-linux-gnu installed - rustc 1.70.0 (90c541806 2023-05-31)

running `rustup run 1.70 cargo check --all-targets` on cargo-platform (1/10)
running `rustup run 1.70 cargo check --all-targets` on home (2/10)
running `rustup run 1.70 cargo check --all-targets` on cargo-credential (3/10)
running `rustup run 1.70 cargo check --all-targets` on cargo-credential-1password (4/10)
info: syncing channel updates for '1.72-x86_64-unknown-linux-gnu'
info: latest update on 2023-08-24, rust version 1.72.0 (5680fa18f 2023-08-23)
info: downloading component 'cargo'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
info: installing component 'rust-std'
info: installing component 'rustc'

  1.72-x86_64-unknown-linux-gnu installed - rustc 1.72.0 (5680fa18f 2023-08-23)

running `rustup run 1.72 cargo check --all-targets` on cargo-util (5/10)
running `rustup run 1.72 cargo check --all-targets` on crates-io (6/10)
running `rustup run 1.72 cargo check --all-targets` on cargo (7/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-libsecret (8/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-macos-keychain (9/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-wincred (10/10)
```

It'd be nice to instead be
```
Run cargo hack check --all-targets --rust-version --workspace --ignore-private
info: skipped running on private package `cargo-test-macro`
info: skipped running on private package `cargo-test-support`
info: skipped running on private package `mdman`
info: skipped running on private package `resolver-tests`
info: skipped running on private package `semver-check`
info: skipped running on private package `xtask-build-man`
info: skipped running on private package `xtask-bump-check`
info: skipped running on private package `xtask-stale-label`
info: skipped running on private package `benchsuite`
info: skipped running on private package `capture`
rustup toolchain install '1.70-x86_64-unknown-linux-gnu'
running `rustup run 1.70 cargo check --all-targets` on cargo-platform (1/10)
running `rustup run 1.70 cargo check --all-targets` on home (2/10)
running `rustup run 1.70 cargo check --all-targets` on cargo-credential (3/10)
running `rustup run 1.70 cargo check --all-targets` on cargo-credential-1password (4/10)
rustup toolchain install '1.72-x86_64-unknown-linux-gnu'
running `rustup run 1.72 cargo check --all-targets` on cargo-util (5/10)
running `rustup run 1.72 cargo check --all-targets` on crates-io (6/10)
running `rustup run 1.72 cargo check --all-targets` on cargo (7/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-libsecret (8/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-macos-keychain (9/10)
running `rustup run 1.72 cargo check --all-targets` on cargo-credential-wincred (10/10)
```

See https://github.com/rust-lang/cargo/pull/12654


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index a9e4e012..7abed124 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -70,6 +70,9 @@ jobs:
           cargo install --path . --debug
           cd tests/fixtures/real
           cargo hack check --feature-powerset --workspace
+          cd ../rust-version
+          rustup toolchain remove 1.63 1.64 1.65
+          cargo hack check --rust-version --workspace
           cargo uninstall cargo-hack
       - uses: taiki-e/install-action@cargo-hack
       - uses: taiki-e/install-action@cargo-minimal-versions
diff --git a/src/main.rs b/src/main.rs
index 7e29935c..9dc3681a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -334,7 +334,8 @@ fn versioned_cargo_exec_on_packages(
     line.leading_arg("run");
 
     let toolchain = format!("1.{cargo_version}");
-    rustup::install_toolchain(&toolchain, &cx.target, true)?;
+    let print_output = true;
+    rustup::install_toolchain(&toolchain, &cx.target, print_output, cx.log_group)?;
     if *generate_lockfile || *regenerate_lockfile_on_51_or_up && cargo_version >= 51 {
         let mut line = line.clone();
         line.leading_arg(&toolchain);
@@ -528,6 +529,26 @@ impl LogGroup {
             Self::None
         }
     }
+
+    fn print(self, msg: &str) -> Option<LogGroupGuard> {
+        match self {
+            Self::GithubActions => {
+                println!("::group::{msg}");
+                Some(LogGroupGuard)
+            }
+            Self::None => {
+                info!("{msg}");
+                None
+            }
+        }
+    }
+}
+
+struct LogGroupGuard;
+impl Drop for LogGroupGuard {
+    fn drop(&mut self) {
+        println!("::endgroup::");
+    }
 }
 
 impl FromStr for LogGroup {
@@ -596,22 +617,7 @@ fn exec_cargo_inner(
         write!(msg, "running {line} on {}", cx.packages(id).name).unwrap();
     }
     write!(msg, " ({}/{})", progress.count, progress.total).unwrap();
-    let _guard = match cx.log_group {
-        LogGroup::GithubActions => {
-            struct Guard;
-            impl Drop for Guard {
-                fn drop(&mut self) {
-                    println!("::endgroup::");
-                }
-            }
-            println!("::group::{msg}");
-            Some(Guard)
-        }
-        LogGroup::None => {
-            info!("{msg}");
-            None
-        }
-    };
+    let _guard = cx.log_group.print(&msg);
 
     line.run()
 }
diff --git a/src/rustup.rs b/src/rustup.rs
index 3f98fe7c..280703e7 100644
--- a/src/rustup.rs
+++ b/src/rustup.rs
@@ -6,7 +6,7 @@ use crate::{
     cargo,
     context::Context,
     version::{MaybeVersion, Version, VersionRange},
-    PackageRuns,
+    LogGroup, PackageRuns,
 };
 
 pub(crate) struct Rustup {
@@ -50,7 +50,8 @@ pub(crate) fn version_range(
         if let Some(stable_version) = stable_version {
             Ok(stable_version)
         } else {
-            install_toolchain("stable", &[], false)?;
+            let print_output = false;
+            install_toolchain("stable", &[], print_output, LogGroup::None)?;
             let version = cargo::version(cmd!("rustup", "run", "stable", "cargo"))?;
             stable_version = Some(version);
             Ok(version)
@@ -127,6 +128,7 @@ pub(crate) fn install_toolchain(
     mut toolchain: &str,
     target: &[String],
     print_output: bool,
+    log_group: LogGroup,
 ) -> Result<()> {
     toolchain = toolchain.strip_prefix('+').unwrap_or(toolchain);
 
@@ -145,6 +147,7 @@ pub(crate) fn install_toolchain(
     }
 
     if print_output {
+        let _guard = log_group.print(&format!("running {cmd}"));
         // The toolchain installation can take some time, so we'll show users
         // the progress.
         cmd.run()


Test patch: