-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Enable incremental rustfmt adoption #65939
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8369a1a
Add individual crates to rustfmt ignore list.
anp 72a844d
bootstrap.py fetches rustfmt.
anp a08c562
Implement `./x.py fmt [--check]`.
anp 5f17b63
Format src/librustc_fs_util.
anp 2b081ab
Include formatting check in the test step for tidy.
anp dddd872
Implement rustfmt running manually using ignore crate
Mark-Simulacrum b9e4174
Do not run if rustfmt.toml does not exist
Mark-Simulacrum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement rustfmt running manually using ignore crate
This replaces cargo-fmt with rustfmt with --skip-children which should allow us to format code without running into rust-lang/rustfmt#3930. This also bumps up the version of rustfmt used to a more recent one.
- Loading branch information
commit dddd8724271a2a38ce552f5b1062a7ea070ec126
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,59 @@ | ||
//! Runs rustfmt on the repository. | ||
|
||
use crate::{util, Build}; | ||
use crate::Build; | ||
use std::process::Command; | ||
use ignore::WalkBuilder; | ||
use std::path::Path; | ||
use build_helper::t; | ||
|
||
pub fn format(build: &Build, check: bool) { | ||
let target = &build.build; | ||
fn rustfmt(build: &Build, path: &Path, check: bool) { | ||
let rustfmt_path = build.config.initial_rustfmt.as_ref().unwrap_or_else(|| { | ||
eprintln!("./x.py fmt is not supported on this channel"); | ||
std::process::exit(1); | ||
}).clone(); | ||
let cargo_fmt_path = rustfmt_path.with_file_name(util::exe("cargo-fmt", &target)); | ||
assert!(cargo_fmt_path.is_file(), "{} not a file", cargo_fmt_path.display()); | ||
|
||
let mut cmd = Command::new(&cargo_fmt_path); | ||
// cargo-fmt calls rustfmt as a bare command, so we need it to only find the correct one | ||
cmd.env("PATH", cargo_fmt_path.parent().unwrap()); | ||
cmd.current_dir(&build.src); | ||
cmd.arg("fmt"); | ||
}); | ||
|
||
let mut cmd = Command::new(&rustfmt_path); | ||
// avoid the submodule config paths from coming into play, | ||
// we only allow a single global config for the workspace for now | ||
cmd.arg("--config-path").arg(&build.src.canonicalize().unwrap()); | ||
cmd.arg("--unstable-features"); | ||
cmd.arg("--skip-children"); | ||
if check { | ||
cmd.arg("--"); | ||
cmd.arg("--check"); | ||
} | ||
cmd.arg(&path); | ||
let cmd_debug = format!("{:?}", cmd); | ||
let status = cmd.status().expect("executing rustfmt"); | ||
assert!(status.success(), "running {} successful", cmd_debug); | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
struct RustfmtConfig { | ||
ignore: Vec<String>, | ||
} | ||
|
||
pub fn format(build: &Build, check: bool) { | ||
let mut builder = ignore::types::TypesBuilder::new(); | ||
builder.add_defaults(); | ||
builder.select("rust"); | ||
let matcher = builder.build().unwrap(); | ||
|
||
let rustfmt_config = t!(std::fs::read_to_string(build.src.join("rustfmt.toml"))); | ||
let rustfmt_config: RustfmtConfig = t!(toml::from_str(&rustfmt_config)); | ||
let mut ignore_fmt = ignore::overrides::OverrideBuilder::new(&build.src); | ||
for ignore in rustfmt_config.ignore { | ||
ignore_fmt.add(&format!("!{}", ignore)).expect(&ignore); | ||
} | ||
let ignore_fmt = ignore_fmt.build().unwrap(); | ||
|
||
let status = cmd.status().expect("executing cargo-fmt"); | ||
assert!(status.success(), "cargo-fmt errored with status {:?}", status); | ||
let walker = WalkBuilder::new(&build.src) | ||
.types(matcher) | ||
.overrides(ignore_fmt) | ||
.build(); | ||
for entry in walker { | ||
let entry = t!(entry); | ||
if entry.file_type().map_or(false, |t| t.is_file()) { | ||
rustfmt(build, &entry.path(), check); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One
rustfmt --check
failure should not prevent runningrustfmt --check
on other files, otherwise you only get one error at a time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When that bit was originally written we were running rustfmt once for the whole repository and it showed all errors. Is that no longer how it's running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like @Mark-Simulacrum pushed commits to this PR to run
rustfmt
on individual files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha - maybe file a bug so someone can pick up a fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, rustfmt on the whole repository was leading to errors in rustfmt (IIRC, it had some problems with ignoring files or so, I don't recall the specifics now).
This should be fixable though, like we do in tidy etc by keeping track of whether we errored and only stopping at the end.