Skip to content

Commit cd85b9d

Browse files
authored
Rollup merge of #142677 - GuillaumeGomez:check-format-version-update, r=Kobzol
Add CI check to ensure that rustdoc JSON `FORMAT_VERSION` is correctly updated Follow-up of #142601. Tested it locally with: `BASE_COMMIT=1bb335244c311a07cee165c28c553c869e6f64a9 src/ci/docker/host-x86_64/mingw-check-1/validate-rustdoc-json-format-version-update.sh` (where `BASE_COMMIT` value was the commit before I made a wrong change with the `FORMAT_VERSION` update). This is a first version. I plan to send a follow-up to also ensure that `FORMAT_VERSION` is updated if any code change is done in `rustdoc-json-types`. For that I just need to check that a line not starting with `/` and not an empty line was updated. Fun times with `grep` ahead. :') cc `@aDotInTheVoid` r? `@nnethercote`
2 parents 4455d1e + 0fc9507 commit cd85b9d

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
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/build_helper/src/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn get_latest_upstream_commit_that_modified_files(
198198
/// author.
199199
///
200200
/// If we are in CI, we simply return our first parent.
201-
fn get_closest_upstream_commit(
201+
pub fn get_closest_upstream_commit(
202202
git_dir: Option<&Path>,
203203
config: &GitConfig<'_>,
204204
env: CiEnv,

src/tools/tidy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub mod pal;
8383
pub mod rustdoc_css_themes;
8484
pub mod rustdoc_gui_tests;
8585
pub mod rustdoc_js;
86+
pub mod rustdoc_json;
8687
pub mod rustdoc_templates;
8788
pub mod style;
8889
pub mod target_policy;

src/tools/tidy/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn main() {
110110
check!(rustdoc_css_themes, &librustdoc_path);
111111
check!(rustdoc_templates, &librustdoc_path);
112112
check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
113+
check!(rustdoc_json, &src_path);
113114
check!(known_bug, &crashes_path);
114115
check!(unknown_revision, &tests_path);
115116

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was
2+
//! updated as well.
3+
4+
use std::ffi::OsStr;
5+
use std::path::Path;
6+
use std::process::Command;
7+
8+
use build_helper::ci::CiEnv;
9+
use build_helper::git::{GitConfig, get_closest_upstream_commit};
10+
use build_helper::stage0_parser::parse_stage0_file;
11+
12+
const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types";
13+
14+
fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
15+
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
16+
Some(String::from_utf8_lossy(&output.stdout).into())
17+
}
18+
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+
28+
pub fn check(src_path: &Path, bad: &mut bool) {
29+
println!("Checking tidy rustdoc_json...");
30+
let stage0 = parse_stage0_file();
31+
let ci_env = CiEnv::current();
32+
let base_commit = match get_closest_upstream_commit(
33+
None,
34+
&GitConfig {
35+
nightly_branch: &stage0.config.nightly_branch,
36+
git_merge_commit_email: &stage0.config.git_merge_commit_email,
37+
},
38+
ci_env,
39+
) {
40+
Ok(Some(commit)) => commit,
41+
Ok(None) => {
42+
error_if_in_ci(ci_env, "no base commit found", bad);
43+
return;
44+
}
45+
Err(error) => {
46+
error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad);
47+
return;
48+
}
49+
};
50+
51+
// First we check that `src/rustdoc-json-types` was modified.
52+
match git_diff(&base_commit, "--name-status") {
53+
Some(output) => {
54+
if !output
55+
.lines()
56+
.any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES))
57+
{
58+
// `rustdoc-json-types` was not modified so nothing more to check here.
59+
println!("`rustdoc-json-types` was not modified.");
60+
return;
61+
}
62+
}
63+
None => {
64+
*bad = true;
65+
eprintln!("error: failed to run `git diff` in rustdoc_json check");
66+
return;
67+
}
68+
}
69+
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
70+
match git_diff(&base_commit, src_path.join("rustdoc-json-types")) {
71+
Some(output) => {
72+
let mut format_version_updated = false;
73+
let mut latest_feature_comment_updated = false;
74+
for line in output.lines() {
75+
if line.starts_with("+pub const FORMAT_VERSION: u32 =") {
76+
format_version_updated = true;
77+
} else if line.starts_with("+// Latest feature:") {
78+
latest_feature_comment_updated = true;
79+
}
80+
}
81+
if format_version_updated != latest_feature_comment_updated {
82+
*bad = true;
83+
if latest_feature_comment_updated {
84+
eprintln!(
85+
"error in `rustdoc_json` tidy check: `Latest feature` comment was updated \
86+
whereas `FORMAT_VERSION` wasn't in `{RUSTDOC_JSON_TYPES}/lib.rs`"
87+
);
88+
} else {
89+
eprintln!(
90+
"error in `rustdoc_json` tidy check: `Latest feature` comment was not \
91+
updated whereas `FORMAT_VERSION` was in `{RUSTDOC_JSON_TYPES}/lib.rs`"
92+
);
93+
}
94+
}
95+
}
96+
None => {
97+
*bad = true;
98+
eprintln!("error: failed to run `git diff` in rustdoc_json check");
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)