diff --git a/src/build_helper/src/ci.rs b/src/build_helper/src/ci.rs index 60f319129a0b..9d114c70a671 100644 --- a/src/build_helper/src/ci.rs +++ b/src/build_helper/src/ci.rs @@ -17,7 +17,11 @@ impl CiEnv { } pub fn is_ci() -> bool { - Self::current() != CiEnv::None + Self::current().is_running_in_ci() + } + + pub fn is_running_in_ci(self) -> bool { + self != CiEnv::None } /// Checks if running in rust-lang/rust managed CI job. diff --git a/src/tools/tidy/src/rustdoc_json.rs b/src/tools/tidy/src/rustdoc_json.rs index 808341fbd061..f179acf4a510 100644 --- a/src/tools/tidy/src/rustdoc_json.rs +++ b/src/tools/tidy/src/rustdoc_json.rs @@ -9,33 +9,41 @@ use build_helper::ci::CiEnv; use build_helper::git::{GitConfig, get_closest_upstream_commit}; use build_helper::stage0_parser::parse_stage0_file; +const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types"; + fn git_diff>(base_commit: &str, extra_arg: S) -> Option { let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?; Some(String::from_utf8_lossy(&output.stdout).into()) } +fn error_if_in_ci(ci_env: CiEnv, msg: &str, bad: &mut bool) { + if ci_env.is_running_in_ci() { + *bad = true; + eprintln!("error in `rustdoc_json` tidy check: {msg}"); + } else { + eprintln!("{msg}. Skipping `rustdoc_json` tidy check"); + } +} + pub fn check(src_path: &Path, bad: &mut bool) { println!("Checking tidy rustdoc_json..."); let stage0 = parse_stage0_file(); + let ci_env = CiEnv::current(); let base_commit = match get_closest_upstream_commit( None, &GitConfig { nightly_branch: &stage0.config.nightly_branch, git_merge_commit_email: &stage0.config.git_merge_commit_email, }, - CiEnv::current(), + ci_env, ) { Ok(Some(commit)) => commit, Ok(None) => { - *bad = true; - eprintln!("error: no base commit found for rustdoc_json check"); + error_if_in_ci(ci_env, "no base commit found", bad); return; } Err(error) => { - *bad = true; - eprintln!( - "error: failed to retrieve base commit for rustdoc_json check because of `{error}`" - ); + error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad); return; } }; @@ -45,7 +53,7 @@ pub fn check(src_path: &Path, bad: &mut bool) { Some(output) => { if !output .lines() - .any(|line| line.starts_with("M") && line.contains("src/rustdoc-json-types")) + .any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES)) { // `rustdoc-json-types` was not modified so nothing more to check here. println!("`rustdoc-json-types` was not modified."); @@ -74,11 +82,13 @@ pub fn check(src_path: &Path, bad: &mut bool) { *bad = true; if latest_feature_comment_updated { eprintln!( - "error: `Latest feature` comment was updated whereas `FORMAT_VERSION` wasn't" + "error in `rustdoc_json` tidy check: `Latest feature` comment was updated \ + whereas `FORMAT_VERSION` wasn't in `{RUSTDOC_JSON_TYPES}/lib.rs`" ); } else { eprintln!( - "error: `Latest feature` comment was not updated whereas `FORMAT_VERSION` was" + "error in `rustdoc_json` tidy check: `Latest feature` comment was not \ + updated whereas `FORMAT_VERSION` was in `{RUSTDOC_JSON_TYPES}/lib.rs`" ); } }