]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/format.rs
Implement `./x.py fmt [--check]`.
[rust.git] / src / bootstrap / format.rs
1 //! Runs rustfmt on the repository.
2
3 use crate::{util, Build};
4 use std::process::Command;
5
6 pub fn format(build: &Build, check: bool) {
7     let target = &build.build;
8     let rustfmt_path = build.config.initial_rustfmt.as_ref().unwrap_or_else(|| {
9         eprintln!("./x.py fmt is not supported on this channel");
10         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");
20
21     if check {
22         cmd.arg("--");
23         cmd.arg("--check");
24     }
25
26     let status = cmd.status().expect("executing cargo-fmt");
27     assert!(status.success(), "cargo-fmt errored with status {:?}", status);
28 }