Skip to content

Commit 0fc9507

Browse files
Improve error message for rustdoc_json_types tidy check
Only emit git errors if we are in CI environment
1 parent 6367694 commit 0fc9507

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/build_helper/src/ci.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ impl CiEnv {
1717
}
1818

1919
pub fn is_ci() -> bool {
20-
Self::current() != CiEnv::None
20+
Self::current().is_running_in_ci()
21+
}
22+
23+
pub fn is_running_in_ci(self) -> bool {
24+
self != CiEnv::None
2125
}
2226

2327
/// Checks if running in rust-lang/rust managed CI job.

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,41 @@ use build_helper::ci::CiEnv;
99
use build_helper::git::{GitConfig, get_closest_upstream_commit};
1010
use build_helper::stage0_parser::parse_stage0_file;
1111

12+
const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types";
13+
1214
fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
1315
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
1416
Some(String::from_utf8_lossy(&output.stdout).into())
1517
}
1618

19+
fn error_if_in_ci(ci_env: CiEnv, msg: &str, bad: &mut bool) {
20+
if ci_env.is_running_in_ci() {
21+
*bad = true;
22+
eprintln!("error in `rustdoc_json` tidy check: {msg}");
23+
} else {
24+
eprintln!("{msg}. Skipping `rustdoc_json` tidy check");
25+
}
26+
}
27+
1728
pub fn check(src_path: &Path, bad: &mut bool) {
1829
println!("Checking tidy rustdoc_json...");
1930
let stage0 = parse_stage0_file();
31+
let ci_env = CiEnv::current();
2032
let base_commit = match get_closest_upstream_commit(
2133
None,
2234
&GitConfig {
2335
nightly_branch: &stage0.config.nightly_branch,
2436
git_merge_commit_email: &stage0.config.git_merge_commit_email,
2537
},
26-
CiEnv::current(),
38+
ci_env,
2739
) {
2840
Ok(Some(commit)) => commit,
2941
Ok(None) => {
30-
*bad = true;
31-
eprintln!("error: no base commit found for rustdoc_json check");
42+
error_if_in_ci(ci_env, "no base commit found", bad);
3243
return;
3344
}
3445
Err(error) => {
35-
*bad = true;
36-
eprintln!(
37-
"error: failed to retrieve base commit for rustdoc_json check because of `{error}`"
38-
);
46+
error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad);
3947
return;
4048
}
4149
};
@@ -45,7 +53,7 @@ pub fn check(src_path: &Path, bad: &mut bool) {
4553
Some(output) => {
4654
if !output
4755
.lines()
48-
.any(|line| line.starts_with("M") && line.contains("src/rustdoc-json-types"))
56+
.any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES))
4957
{
5058
// `rustdoc-json-types` was not modified so nothing more to check here.
5159
println!("`rustdoc-json-types` was not modified.");
@@ -74,11 +82,13 @@ pub fn check(src_path: &Path, bad: &mut bool) {
7482
*bad = true;
7583
if latest_feature_comment_updated {
7684
eprintln!(
77-
"error: `Latest feature` comment was updated whereas `FORMAT_VERSION` wasn't"
85+
"error in `rustdoc_json` tidy check: `Latest feature` comment was updated \
86+
whereas `FORMAT_VERSION` wasn't in `{RUSTDOC_JSON_TYPES}/lib.rs`"
7887
);
7988
} else {
8089
eprintln!(
81-
"error: `Latest feature` comment was not updated whereas `FORMAT_VERSION` was"
90+
"error in `rustdoc_json` tidy check: `Latest feature` comment was not \
91+
updated whereas `FORMAT_VERSION` was in `{RUSTDOC_JSON_TYPES}/lib.rs`"
8292
);
8393
}
8494
}

0 commit comments

Comments
 (0)