Skip to content

Commit 1ae968e

Browse files
authored
Rollup merge of rust-lang#139867 - WaffleLapkin:tidypaper, r=jieyouxu
Fix some tidy paper cuts The main thing this fixes is that currently, if you run `x t tidy` it will format ~6K files, even though it's supposed to format only modified files (whatever this is a useful optimization or not is besides the point). The problem is that `x t tidy` never writes the `rustfmt` stamp, so it always assumes `rustfmt` that was last used is outdated and we need to recheck everything. This PR fixes it by actually writing the stamp. There are also some minor tweaks to comments/diagnostics. cc `@Kobzol` this probably conflicts with rust-lang#138591. I didn't fix anything, just tried to document better the status quo. r? `@jieyouxu`
2 parents 9a0cdbe + 90aec13 commit 1ae968e

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/bootstrap/src/core/build_steps/format.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ fn update_rustfmt_version(build: &Builder<'_>) {
8181
let Some((version, stamp_file)) = get_rustfmt_version(build) else {
8282
return;
8383
};
84-
t!(std::fs::write(stamp_file.path(), version))
84+
85+
t!(stamp_file.add_stamp(version).write());
8586
}
8687

87-
/// Returns the Rust files modified between the `merge-base` of HEAD and
88-
/// rust-lang/master and what is now on the disk. Does not include removed files.
88+
/// Returns the Rust files modified between the last merge commit and what is now on the disk.
89+
/// Does not include removed files.
8990
///
9091
/// Returns `None` if all files should be formatted.
9192
fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, String> {
93+
// In CI `get_git_modified_files` returns something different to normal environment.
94+
// This shouldn't be called in CI anyway.
95+
assert!(!build.config.is_running_on_ci);
96+
9297
if !verify_rustfmt_version(build) {
9398
return Ok(None);
9499
}
@@ -103,7 +108,7 @@ struct RustfmtConfig {
103108

104109
// Prints output describing a collection of paths, with lines such as "formatted modified file
105110
// foo/bar/baz" or "skipped 20 untracked files".
106-
fn print_paths(build: &Builder<'_>, verb: &str, adjective: Option<&str>, paths: &[String]) {
111+
fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
107112
let len = paths.len();
108113
let adjective =
109114
if let Some(adjective) = adjective { format!("{adjective} ") } else { String::new() };
@@ -114,9 +119,6 @@ fn print_paths(build: &Builder<'_>, verb: &str, adjective: Option<&str>, paths:
114119
} else {
115120
println!("fmt: {verb} {len} {adjective}files");
116121
}
117-
if len > 1000 && !build.config.is_running_on_ci {
118-
println!("hint: if this number seems too high, try running `git fetch origin master`");
119-
}
120122
}
121123

122124
pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
@@ -189,7 +191,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
189191
)
190192
.map(|x| x.to_string())
191193
.collect();
192-
print_paths(build, "skipped", Some("untracked"), &untracked_paths);
194+
print_paths("skipped", Some("untracked"), &untracked_paths);
193195

194196
for untracked_path in untracked_paths {
195197
// The leading `/` makes it an exact match against the
@@ -212,7 +214,13 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
212214
override_builder.add(&format!("/{file}")).expect(&file);
213215
}
214216
}
215-
Ok(None) => {}
217+
Ok(None) => {
218+
// NOTE: `Ok(None)` signifies that we need to format all files.
219+
// The tricky part here is that if `override_builder` isn't given any white
220+
// list files (i.e. files to be formatted, added without leading `!`), it
221+
// will instead look for *all* files. So, by doing nothing here, we are
222+
// actually making it so we format all files.
223+
}
216224
Err(err) => {
217225
eprintln!("fmt warning: Something went wrong running git commands:");
218226
eprintln!("fmt warning: {err}");
@@ -318,7 +326,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
318326
});
319327
let mut paths = formatted_paths.into_inner().unwrap();
320328
paths.sort();
321-
print_paths(build, if check { "checked" } else { "formatted" }, adjective, &paths);
329+
print_paths(if check { "checked" } else { "formatted" }, adjective, &paths);
322330

323331
drop(tx);
324332

@@ -328,7 +336,10 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
328336
crate::exit!(1);
329337
}
330338

331-
if !check {
332-
update_rustfmt_version(build);
333-
}
339+
// Update `build/.rustfmt-stamp`, allowing this code to ignore files which have not been changed
340+
// since last merge.
341+
//
342+
// NOTE: Because of the exit above, this is only reachable if formatting / format checking
343+
// succeeded. So we are not commiting the version if formatting was not good.
344+
update_rustfmt_version(build);
334345
}

src/build_helper/src/git.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ fn git_upstream_merge_base(
114114
Ok(output_result(git.arg("merge-base").arg(&updated_master).arg("HEAD"))?.trim().to_owned())
115115
}
116116

117-
/// Searches for the nearest merge commit in the repository that also exists upstream.
117+
/// Searches for the nearest merge commit in the repository.
118+
///
119+
/// **In CI** finds the nearest merge commit that *also exists upstream*.
118120
///
119121
/// It looks for the most recent commit made by the merge bot by matching the author's email
120122
/// address with the merge bot's email.
@@ -165,7 +167,7 @@ pub fn get_closest_merge_commit(
165167
Ok(output_result(&mut git)?.trim().to_owned())
166168
}
167169

168-
/// Returns the files that have been modified in the current branch compared to the master branch.
170+
/// Returns the files that have been modified in the current branch compared to the last merge.
169171
/// The `extensions` parameter can be used to filter the files by their extension.
170172
/// Does not include removed files.
171173
/// If `extensions` is empty, all files will be returned.

src/tools/tidy/src/deps.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,10 @@ pub static CRATES: &[&str] = &[
682682
pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
683683
!CiEnv::is_ci()
684684
&& submodules.iter().any(|submodule| {
685+
let path = root.join(submodule);
686+
!path.exists()
685687
// If the directory is empty, we can consider it as an uninitialized submodule.
686-
read_dir(root.join(submodule)).unwrap().next().is_none()
688+
|| read_dir(path).unwrap().next().is_none()
687689
})
688690
}
689691

0 commit comments

Comments
 (0)