Skip to content

Add CI check to ensure that rustdoc JSON FORMAT_VERSION is correctly updated #142677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
// are deliberately not in a doc comment, because they need not be in public docs.)
//
// Latest feature: Pretty printing of inline attributes changed
pub const FORMAT_VERSION: u32 = 48;
pub const FORMAT_VERSION: u32 = 49;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 48->49 change should be reverted before merging.


/// The root of the emitted JSON blob.
///
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub mod pal;
pub mod rustdoc_css_themes;
pub mod rustdoc_gui_tests;
pub mod rustdoc_js;
pub mod rustdoc_json;
pub mod rustdoc_templates;
pub mod style;
pub mod target_policy;
Expand Down
31 changes: 31 additions & 0 deletions src/tools/tidy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,49 @@
use std::collections::VecDeque;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::{self, ScopedJoinHandle, scope};
use std::{env, process};

use tidy::*;

// We set the `BASE_COMMIT` environment variable
fn set_env_base_commit() {
if let Ok(base_commit) = env::var("BASE_COMMIT") {
// Nothing to be done!
println!("base commit is {base_commit}");
return;
}
// If first commit is from `[email protected]`, then we need to skip it as it means we're in
// the CI and it's the merge commit generated to test the PR.
let output = Command::new("git")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is much more complicated than it might seem. Could you instead reuse

fn get_closest_upstream_commit(
? This is more complicated than it might seem 😅

.args(&["log", "-n", "1", "--pretty=format:%ae"])
.output()
.expect("Failed to run `git` command to retrieve base commit");
let output = String::from_utf8_lossy(&output.stdout);
let skip = if output.trim() == "[email protected]" { "--skip=1" } else { "--skip=0" };

// Now we find the last merge commit from bors to know what base commit to use.
let output = Command::new("git")
.args(&["log", "[email protected]", "-n", "1", "--pretty=format:%H", skip])
.output()
.expect("Failed to run `git` command to retrieve base commit");
let output = String::from_utf8_lossy(&output.stdout);

let base_commit = output.trim();
println!("setting base commit: {base_commit}");
env::set_var("BASE_COMMIT", base_commit);
}

fn main() {
// Running Cargo will read the libstd Cargo.toml
// which uses the unstable `public-dependency` feature.
//
// `setenv` might not be thread safe, so run it before using multiple threads.
env::set_var("RUSTC_BOOTSTRAP", "1");
set_env_base_commit();

let root_path: PathBuf = env::args_os().nth(1).expect("need path to root of repo").into();
let cargo: PathBuf = env::args_os().nth(2).expect("need path to cargo").into();
Expand Down Expand Up @@ -110,6 +140,7 @@ fn main() {
check!(rustdoc_css_themes, &librustdoc_path);
check!(rustdoc_templates, &librustdoc_path);
check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
check!(rustdoc_json);
check!(known_bug, &crashes_path);
check!(unknown_revision, &tests_path);

Expand Down
62 changes: 62 additions & 0 deletions src/tools/tidy/src/rustdoc_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was
//! updated as well.

use std::process::Command;

fn git_diff(base_commit: &str, extra_arg: &str) -> Option<String> {
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
Some(String::from_utf8_lossy(&output.stdout).into())
}

pub fn check(bad: &mut bool) {
let base_commit = std::env::var("BASE_COMMIT").unwrap();

// First we check that `src/rustdoc-json-types` was modified.
match git_diff(&base_commit, "--name-status") {
Some(output) => {
if !output
.lines()
.any(|line| line.starts_with("M") && line.contains("src/rustdoc-json-types"))
{
// `rustdoc-json-types` was not modified so nothing more to check here.
return;
}
}
None => {
*bad = true;
eprintln!("Failed to run `git diff`");
return;
}
}
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
match git_diff(&base_commit, "src/rustdoc-json-types") {
Some(output) => {
let mut format_version_updated = false;
let mut latest_feature_comment_updated = false;
for line in output.lines() {
if line.starts_with("+pub const FORMAT_VERSION: u32 =") {
format_version_updated = true;
} else if line.starts_with("+// Latest feature:") {
latest_feature_comment_updated = true;
}
}
if format_version_updated != latest_feature_comment_updated {
*bad = true;
if latest_feature_comment_updated {
eprintln!(
"`Latest feature` comment was updated whereas `FORMAT_VERSION` wasn't"
);
} else {
eprintln!(
"`Latest feature` comment was not updated whereas `FORMAT_VERSION` was"
);
}
}
}
None => {
*bad = true;
eprintln!("Failed to run `git diff`");
return;
}
}
}
Loading