Skip to content

Commit 7d46885

Browse files
committed
Split nested GHA groups instead of panicking
1 parent ff8fe76 commit 7d46885

File tree

1 file changed

+43
-19
lines changed
  • src/tools/build_helper/src

1 file changed

+43
-19
lines changed

src/tools/build_helper/src/ci.rs

+43-19
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,26 @@ impl CiEnv {
3636
}
3737

3838
pub mod gha {
39-
use std::sync::atomic::{AtomicBool, Ordering};
39+
use std::sync::Mutex;
4040

41-
static GROUP_ACTIVE: AtomicBool = AtomicBool::new(false);
41+
static ACTIVE_GROUPS: Mutex<Vec<String>> = Mutex::new(Vec::new());
4242

4343
/// All github actions log messages from this call to the Drop of the return value
44-
/// will be grouped and hidden by default in logs. Note that nesting these does
45-
/// not really work.
44+
/// will be grouped and hidden by default in logs. Note that since github actions doesn't
45+
/// support group nesting, any active group will be first finished when a subgroup is started,
46+
/// and then re-started when the subgroup finishes.
4647
#[track_caller]
4748
pub fn group(name: impl std::fmt::Display) -> Group {
48-
if std::env::var_os("GITHUB_ACTIONS").is_some() {
49-
eprintln!("::group::{name}");
50-
} else {
51-
eprintln!("{name}")
49+
let mut groups = ACTIVE_GROUPS.lock().unwrap();
50+
51+
// A group is currently active. End it first to avoid nesting.
52+
if !groups.is_empty() {
53+
end_group();
5254
}
53-
// https://github.com/actions/toolkit/issues/1001
54-
assert!(
55-
!GROUP_ACTIVE.swap(true, Ordering::Relaxed),
56-
"nested groups are not supported by GHA!"
57-
);
55+
56+
let name = name.to_string();
57+
start_group(&name);
58+
groups.push(name);
5859
Group(())
5960
}
6061

@@ -64,13 +65,36 @@ pub mod gha {
6465

6566
impl Drop for Group {
6667
fn drop(&mut self) {
67-
if std::env::var_os("GITHUB_ACTIONS").is_some() {
68-
eprintln!("::endgroup::");
68+
end_group();
69+
70+
let mut groups = ACTIVE_GROUPS.lock().unwrap();
71+
// Remove the current group
72+
groups.pop();
73+
74+
// If there was some previous group, restart it
75+
if is_in_gha() {
76+
if let Some(name) = groups.last() {
77+
start_group(format!("{name} (continued)"));
78+
}
6979
}
70-
assert!(
71-
GROUP_ACTIVE.swap(false, Ordering::Relaxed),
72-
"group dropped but no group active!"
73-
);
7480
}
7581
}
82+
83+
fn start_group(name: impl std::fmt::Display) {
84+
if is_in_gha() {
85+
eprintln!("::group::{name}");
86+
} else {
87+
eprintln!("{name}")
88+
}
89+
}
90+
91+
fn end_group() {
92+
if is_in_gha() {
93+
eprintln!("::endgroup::");
94+
}
95+
}
96+
97+
fn is_in_gha() -> bool {
98+
std::env::var_os("GITHUB_ACTIONS").is_some()
99+
}
76100
}

0 commit comments

Comments
 (0)