1 use crate::clippy_project_root;
2 use shell_escape::escape;
5 use std::process::{self, Command};
11 CommandFailed(String),
14 WalkDirError(walkdir::Error),
18 impl From<io::Error> for CliError {
19 fn from(error: io::Error) -> Self {
24 impl From<walkdir::Error> for CliError {
25 fn from(error: walkdir::Error) -> Self {
26 Self::WalkDirError(error)
35 // the "main" function of cargo dev fmt
36 pub fn run(check: bool, verbose: bool) {
37 fn try_run(context: &FmtContext) -> Result<bool, CliError> {
38 let mut success = true;
40 let project_root = clippy_project_root();
42 // if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
43 // format because rustfmt would also format the entire rustc repo as it is a local
45 if fs::read_to_string(project_root.join("Cargo.toml"))
46 .expect("Failed to read clippy Cargo.toml")
47 .contains(&"[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
49 return Err(CliError::RaSetupActive);
52 rustfmt_test(context)?;
54 success &= cargo_fmt(context, project_root.as_path())?;
55 success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
56 success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
58 for entry in WalkDir::new(project_root.join("tests")) {
60 let path = entry.path();
62 if path.extension() != Some("rs".as_ref())
63 || entry.file_name() == "ice-3891.rs"
64 // Avoid rustfmt bug rust-lang/rustfmt#1873
65 || cfg!(windows) && entry.file_name() == "implicit_hasher.rs"
70 success &= rustfmt(context, &path)?;
76 fn output_err(err: CliError) {
78 CliError::CommandFailed(command) => {
79 eprintln!("error: A command failed! `{}`", command);
81 CliError::IoError(err) => {
82 eprintln!("error: {}", err);
84 CliError::RustfmtNotInstalled => {
85 eprintln!("error: rustfmt nightly is not installed.");
87 CliError::WalkDirError(err) => {
88 eprintln!("error: {}", err);
90 CliError::RaSetupActive => {
92 "error: a local rustc repo is enabled as path dependency via `cargo dev ra_setup`.
93 Not formatting because that would format the local repo as well!
94 Please revert the changes to Cargo.tomls first."
100 let context = FmtContext { check, verbose };
101 let result = try_run(&context);
102 let code = match result {
106 eprintln!("Formatting check failed.");
107 eprintln!("Run `cargo dev fmt` to update formatting.");
118 fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
119 let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
123 escape(dir.as_ref().to_string_lossy()),
124 escape(program.as_ref().to_string_lossy()),
125 arg_display.join(" ")
130 context: &FmtContext,
131 program: impl AsRef<OsStr>,
132 dir: impl AsRef<Path>,
133 args: &[impl AsRef<OsStr>],
134 ) -> Result<bool, CliError> {
136 println!("{}", format_command(&program, &dir, args));
139 let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
140 let code = child.wait()?;
141 let success = code.success();
143 if !context.check && !success {
144 return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
150 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
151 let mut args = vec!["+nightly", "fmt", "--all"];
154 args.push("--check");
156 let success = exec(context, "cargo", path, &args)?;
161 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
162 let program = "rustfmt";
163 let dir = std::env::current_dir()?;
164 let args = &["+nightly", "--version"];
167 println!("{}", format_command(&program, &dir, args));
170 let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
172 if output.status.success() {
174 } else if std::str::from_utf8(&output.stderr)
176 .starts_with("error: 'rustfmt' is not installed")
178 Err(CliError::RustfmtNotInstalled)
180 Err(CliError::CommandFailed(format_command(&program, &dir, args)))
184 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
185 let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
187 args.push("--check".as_ref());
189 let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
191 eprintln!("rustfmt failed on {}", path.display());