Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dddd872

Browse files
committedDec 22, 2019
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 parent 2b081ab commit dddd872

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed
 

‎Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ dependencies = [
192192
"cmake",
193193
"filetime",
194194
"getopts",
195+
"ignore",
195196
"lazy_static 1.3.0",
196197
"libc",
197198
"num_cpus",
@@ -1525,9 +1526,9 @@ checksum = "c3360c7b59e5ffa2653671fb74b4741a5d343c03f331c0a4aeda42b5c2b0ec7d"
15251526

15261527
[[package]]
15271528
name = "ignore"
1528-
version = "0.4.7"
1529+
version = "0.4.10"
15291530
source = "registry+https://github.com/rust-lang/crates.io-index"
1530-
checksum = "8dc57fa12805f367736a38541ac1a9fc6a52812a0ca959b1d4d4b640a89eb002"
1531+
checksum = "0ec16832258409d571aaef8273f3c3cc5b060d784e159d1a0f3b0017308f84a7"
15311532
dependencies = [
15321533
"crossbeam-channel",
15331534
"globset",

‎rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ignore = [
6868
"src/rustllvm/",
6969
"src/test/",
7070
"src/tools/",
71+
"src/etc",
7172

7273
# do not format submodules
7374
"src/doc/book",

‎src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ serde_json = "1.0.2"
4747
toml = "0.5"
4848
lazy_static = "1.3.0"
4949
time = "0.1"
50+
ignore = "0.4.10"
5051

5152
[dev-dependencies]
5253
pretty_assertions = "0.5"

‎src/bootstrap/format.rs

+46-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
11
//! Runs rustfmt on the repository.
22
3-
use crate::{util, Build};
3+
use crate::Build;
44
use std::process::Command;
5+
use ignore::WalkBuilder;
6+
use std::path::Path;
7+
use build_helper::t;
58

6-
pub fn format(build: &Build, check: bool) {
7-
let target = &build.build;
9+
fn rustfmt(build: &Build, path: &Path, check: bool) {
810
let rustfmt_path = build.config.initial_rustfmt.as_ref().unwrap_or_else(|| {
911
eprintln!("./x.py fmt is not supported on this channel");
1012
std::process::exit(1);
11-
}).clone();
12-
let cargo_fmt_path = rustfmt_path.with_file_name(util::exe("cargo-fmt", &target));
13-
assert!(cargo_fmt_path.is_file(), "{} not a file", cargo_fmt_path.display());
14-
15-
let mut cmd = Command::new(&cargo_fmt_path);
16-
// cargo-fmt calls rustfmt as a bare command, so we need it to only find the correct one
17-
cmd.env("PATH", cargo_fmt_path.parent().unwrap());
18-
cmd.current_dir(&build.src);
19-
cmd.arg("fmt");
13+
});
2014

15+
let mut cmd = Command::new(&rustfmt_path);
16+
// avoid the submodule config paths from coming into play,
17+
// we only allow a single global config for the workspace for now
18+
cmd.arg("--config-path").arg(&build.src.canonicalize().unwrap());
19+
cmd.arg("--unstable-features");
20+
cmd.arg("--skip-children");
2121
if check {
22-
cmd.arg("--");
2322
cmd.arg("--check");
2423
}
24+
cmd.arg(&path);
25+
let cmd_debug = format!("{:?}", cmd);
26+
let status = cmd.status().expect("executing rustfmt");
27+
assert!(status.success(), "running {} successful", cmd_debug);
28+
}
29+
30+
#[derive(serde::Deserialize)]
31+
struct RustfmtConfig {
32+
ignore: Vec<String>,
33+
}
34+
35+
pub fn format(build: &Build, check: bool) {
36+
let mut builder = ignore::types::TypesBuilder::new();
37+
builder.add_defaults();
38+
builder.select("rust");
39+
let matcher = builder.build().unwrap();
40+
41+
let rustfmt_config = t!(std::fs::read_to_string(build.src.join("rustfmt.toml")));
42+
let rustfmt_config: RustfmtConfig = t!(toml::from_str(&rustfmt_config));
43+
let mut ignore_fmt = ignore::overrides::OverrideBuilder::new(&build.src);
44+
for ignore in rustfmt_config.ignore {
45+
ignore_fmt.add(&format!("!{}", ignore)).expect(&ignore);
46+
}
47+
let ignore_fmt = ignore_fmt.build().unwrap();
2548

26-
let status = cmd.status().expect("executing cargo-fmt");
27-
assert!(status.success(), "cargo-fmt errored with status {:?}", status);
49+
let walker = WalkBuilder::new(&build.src)
50+
.types(matcher)
51+
.overrides(ignore_fmt)
52+
.build();
53+
for entry in walker {
54+
let entry = t!(entry);
55+
if entry.file_type().map_or(false, |t| t.is_file()) {
56+
rustfmt(build, &entry.path(), check);
57+
}
58+
}
2859
}

‎src/stage0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ cargo: beta
1919
# We use a nightly rustfmt to format the source because it solves some bootstrapping
2020
# issues with use of new syntax in this repo. If you're looking at the beta/stable branch, this key should be omitted,
2121
# as we don't want to depend on rustfmt from nightly there.
22-
rustfmt: nightly-2019-11-05
22+
rustfmt: nightly-2019-12-18
2323

2424
# When making a stable release the process currently looks like:
2525
#

0 commit comments

Comments
 (0)
Failed to load comments.