]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/fmt.rs
Merge remote-tracking branch 'upstream/master' into rustup
[rust.git] / clippy_dev / src / fmt.rs
1 use crate::clippy_project_root;
2 use shell_escape::escape;
3 use std::ffi::OsStr;
4 use std::path::Path;
5 use std::process::{self, Command};
6 use std::{fs, io};
7 use walkdir::WalkDir;
8
9 #[derive(Debug)]
10 pub enum CliError {
11     CommandFailed(String, String),
12     IoError(io::Error),
13     RustfmtNotInstalled,
14     WalkDirError(walkdir::Error),
15     RaSetupActive,
16 }
17
18 impl From<io::Error> for CliError {
19     fn from(error: io::Error) -> Self {
20         Self::IoError(error)
21     }
22 }
23
24 impl From<walkdir::Error> for CliError {
25     fn from(error: walkdir::Error) -> Self {
26         Self::WalkDirError(error)
27     }
28 }
29
30 struct FmtContext {
31     check: bool,
32     verbose: bool,
33 }
34
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;
39
40         let project_root = clippy_project_root();
41
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
44         // dependency
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]")
48         {
49             return Err(CliError::RaSetupActive);
50         }
51
52         rustfmt_test(context)?;
53
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"))?;
57         success &= cargo_fmt(context, &project_root.join("lintcheck"))?;
58
59         for entry in WalkDir::new(project_root.join("tests")) {
60             let entry = entry?;
61             let path = entry.path();
62
63             if path.extension() != Some("rs".as_ref())
64                 || entry.file_name() == "ice-3891.rs"
65                 // Avoid rustfmt bug rust-lang/rustfmt#1873
66                 || cfg!(windows) && entry.file_name() == "implicit_hasher.rs"
67             {
68                 continue;
69             }
70
71             success &= rustfmt(context, &path)?;
72         }
73
74         Ok(success)
75     }
76
77     fn output_err(err: CliError) {
78         match err {
79             CliError::CommandFailed(command, stderr) => {
80                 eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
81             },
82             CliError::IoError(err) => {
83                 eprintln!("error: {}", err);
84             },
85             CliError::RustfmtNotInstalled => {
86                 eprintln!("error: rustfmt nightly is not installed.");
87             },
88             CliError::WalkDirError(err) => {
89                 eprintln!("error: {}", err);
90             },
91             CliError::RaSetupActive => {
92                 eprintln!(
93                     "error: a local rustc repo is enabled as path dependency via `cargo dev ide_setup`.
94 Not formatting because that would format the local repo as well!
95 Please revert the changes to Cargo.tomls first."
96                 );
97             },
98         }
99     }
100
101     let context = FmtContext { check, verbose };
102     let result = try_run(&context);
103     let code = match result {
104         Ok(true) => 0,
105         Ok(false) => {
106             eprintln!();
107             eprintln!("Formatting check failed.");
108             eprintln!("Run `cargo dev fmt` to update formatting.");
109             1
110         },
111         Err(err) => {
112             output_err(err);
113             1
114         },
115     };
116     process::exit(code);
117 }
118
119 fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
120     let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
121
122     format!(
123         "cd {} && {} {}",
124         escape(dir.as_ref().to_string_lossy()),
125         escape(program.as_ref().to_string_lossy()),
126         arg_display.join(" ")
127     )
128 }
129
130 fn exec(
131     context: &FmtContext,
132     program: impl AsRef<OsStr>,
133     dir: impl AsRef<Path>,
134     args: &[impl AsRef<OsStr>],
135 ) -> Result<bool, CliError> {
136     if context.verbose {
137         println!("{}", format_command(&program, &dir, args));
138     }
139
140     let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
141     let output = child.wait_with_output()?;
142     let success = output.status.success();
143
144     if !context.check && !success {
145         let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
146         return Err(CliError::CommandFailed(
147             format_command(&program, &dir, args),
148             String::from(stderr),
149         ));
150     }
151
152     Ok(success)
153 }
154
155 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
156     let mut args = vec!["+nightly", "fmt", "--all"];
157     if context.check {
158         args.push("--");
159         args.push("--check");
160     }
161     let success = exec(context, "cargo", path, &args)?;
162
163     Ok(success)
164 }
165
166 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
167     let program = "rustfmt";
168     let dir = std::env::current_dir()?;
169     let args = &["+nightly", "--version"];
170
171     if context.verbose {
172         println!("{}", format_command(&program, &dir, args));
173     }
174
175     let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
176
177     if output.status.success() {
178         Ok(())
179     } else if std::str::from_utf8(&output.stderr)
180         .unwrap_or("")
181         .starts_with("error: 'rustfmt' is not installed")
182     {
183         Err(CliError::RustfmtNotInstalled)
184     } else {
185         Err(CliError::CommandFailed(
186             format_command(&program, &dir, args),
187             std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
188         ))
189     }
190 }
191
192 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
193     let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
194     if context.check {
195         args.push("--check".as_ref());
196     }
197     let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
198     if !success {
199         eprintln!("rustfmt failed on {}", path.display());
200     }
201     Ok(success)
202 }