Skip to content
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

Allow formatting specific subdirectories #84783

Merged
merged 1 commit into from
May 11, 2021
Merged
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
3 changes: 2 additions & 1 deletion src/bootstrap/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum Subcommand {
paths: Vec<PathBuf>,
},
Format {
paths: Vec<PathBuf>,
check: bool,
},
Doc {
Expand Down Expand Up @@ -578,7 +579,7 @@ Arguments:

Subcommand::Clean { all: matches.opt_present("all") }
}
"fmt" => Subcommand::Format { check: matches.opt_present("check") },
"fmt" => Subcommand::Format { check: matches.opt_present("check"), paths },
"dist" => Subcommand::Dist { paths },
"install" => Subcommand::Install { paths },
"run" | "r" => {
Expand Down
17 changes: 14 additions & 3 deletions src/bootstrap/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct RustfmtConfig {
ignore: Vec<String>,
}

pub fn format(build: &Build, check: bool) {
pub fn format(build: &Build, check: bool, paths: &[PathBuf]) {
if build.config.dry_run {
return;
}
Expand Down Expand Up @@ -118,8 +118,19 @@ pub fn format(build: &Build, check: bool) {
.to_path_buf();
let src = build.src.clone();
let (tx, rx): (SyncSender<PathBuf>, _) = std::sync::mpsc::sync_channel(128);
let walker =
WalkBuilder::new(src.clone()).types(matcher).overrides(ignore_fmt).build_parallel();
let walker = match paths.get(0) {
Some(first) => {
let mut walker = WalkBuilder::new(first);
for path in &paths[1..] {
walker.add(path);
Copy link
Member

Choose a reason for hiding this comment

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

Hm. This doesn't do any verification of what the passed path is, which might be a problem. I'm not sure the ignore_fmt rules will work when not rooted at the build.src.clone() start; have you checked that e.g. passing src/test/ui doesn't change anything? If not, this isn't quite the right fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

have you checked that e.g. passing src/test/ui doesn't change anything

I have now! 😆 and there are no changes; I have a hard time believing everything there is formatted how rustfmt likes it, so I think we're good :)

}
walker
}
None => WalkBuilder::new(src.clone()),
}
.types(matcher)
.overrides(ignore_fmt)
.build_parallel();

// there is a lot of blocking involved in spawning a child process and reading files to format.
// spawn more processes than available concurrency to keep the CPU busy
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ impl Build {
job::setup(self);
}

if let Subcommand::Format { check } = self.config.cmd {
return format::format(self, check);
if let Subcommand::Format { check, paths } = &self.config.cmd {
return format::format(self, *check, &paths);
}

if let Subcommand::Clean { all } = self.config.cmd {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ help: to skip test's attempt to check tidiness, pass `--exclude src/tools/tidy`
);
std::process::exit(1);
}
crate::format::format(&builder.build, !builder.config.cmd.bless());
crate::format::format(&builder.build, !builder.config.cmd.bless(), &[]);
}
}

Expand Down