-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- 1.83.0
- 1.82.0
- 1.81.0
- 1.80.1
- 1.80.0
- 1.79.0
- 1.78.0
- 1.77.2
- 1.77.1
- 1.77.0
- 1.76.0
- 1.75.0
- 1.74.1
- 1.74.0
- 1.73.0
- 1.72.1
- 1.72.0
- 1.71.1
- 1.71.0
- 1.70.0
- 1.69.0
- 1.68.2
- 1.68.1
- 1.68.0
- 1.67.1
- 1.67.0
- 1.66.1
- 1.66.0
- 1.65.0
- 1.64.0
- 1.63.0
- 1.62.1
- 1.62.0
- 1.61.0
- 1.60.0
- 1.59.0
- 1.58.1
- 1.58.0
- 1.57.0
- 1.56.1
- 1.56.0
- 1.55.0
- 1.54.0
- 1.53.0
- 1.52.1
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.2
- 1.45.1
- 1.45.0
- 1.44.1
- 1.44.0
- 1.43.1
- 1.43.0
- 1.42.0
1 parent
2b081ab
commit dddd872
Showing
5 changed files
with
52 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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