Skip to content

Commit 5d9e058

Browse files
Add CI check to ensure that rustdoc JSON FORMAT_VERSION is correctly updated
1 parent 1bb3352 commit 5d9e058

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

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);
113114
check!(known_bug, &crashes_path);
114115
check!(unknown_revision, &tests_path);
115116

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was
2+
//! updated as well.
3+
4+
use std::process::Command;
5+
6+
fn git_diff(base_commit: &str, extra_arg: &str) -> Option<String> {
7+
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
8+
Some(String::from_utf8_lossy(&output.stdout).into())
9+
}
10+
11+
pub fn check(bad: &mut bool) {
12+
let Ok(base_commit) = std::env::var("BASE_COMMIT") else {
13+
// Not in CI so nothing we can check here.
14+
println!("not checking rustdoc JSON `FORMAT_VERSION` update");
15+
return;
16+
};
17+
18+
// First we check that `src/rustdoc-json-types` was modified.
19+
match git_diff(&base_commit, "--name-status") {
20+
Some(output) => {
21+
if output
22+
.lines()
23+
.any(|line| line.starts_with("M") && line.contains("src/rustdoc-json-types"))
24+
{
25+
// `rustdoc-json-types` was not modified so nothing more to check here.
26+
return;
27+
}
28+
}
29+
None => {
30+
*bad = true;
31+
eprintln!("Failed to run `git diff`");
32+
return;
33+
}
34+
}
35+
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
36+
match git_diff(&base_commit, "src/rustdoc-json-types") {
37+
Some(output) => {
38+
let mut format_version_updated = false;
39+
let mut latest_feature_comment_updated = false;
40+
for line in output.lines() {
41+
if line.starts_with("+pub const FORMAT_VERSION: u32 =") {
42+
format_version_updated = true;
43+
} else if line.starts_with("+// Latest feature:") {
44+
latest_feature_comment_updated = true;
45+
}
46+
}
47+
if format_version_updated != latest_feature_comment_updated {
48+
*bad = true;
49+
if latest_feature_comment_updated {
50+
eprintln!(
51+
"`Latest feature` comment was updated whereas `FORMAT_VERSION` wasn't"
52+
);
53+
} else {
54+
eprintln!(
55+
"`Latest feature` comment was not updated whereas `FORMAT_VERSION` was"
56+
);
57+
}
58+
}
59+
}
60+
None => {
61+
*bad = true;
62+
eprintln!("Failed to run `git diff`");
63+
return;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)