1 use crate::clippy_project_root;
2 use shell_escape::escape;
5 use std::process::{self, Command};
11 CommandFailed(String, 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, stderr) => {
79 eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
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 child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
140 let output = child.wait_with_output()?;
141 let success = output.status.success();
143 if !context.check && !success {
144 let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
145 return Err(CliError::CommandFailed(
146 format_command(&program, &dir, args),
147 String::from(stderr),
154 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
155 let mut args = vec!["+nightly", "fmt", "--all"];
158 args.push("--check");
160 let success = exec(context, "cargo", path, &args)?;
165 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
166 let program = "rustfmt";
167 let dir = std::env::current_dir()?;
168 let args = &["+nightly", "--version"];
171 println!("{}", format_command(&program, &dir, args));
174 let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
176 if output.status.success() {
178 } else if std::str::from_utf8(&output.stderr)
180 .starts_with("error: 'rustfmt' is not installed")
182 Err(CliError::RustfmtNotInstalled)
184 Err(CliError::CommandFailed(
185 format_command(&program, &dir, args),
186 std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
191 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
192 let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
194 args.push("--check".as_ref());
196 let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
198 eprintln!("rustfmt failed on {}", path.display());