]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/fmt.rs
Auto merge of #80860 - camelid:nodeid-docs, r=sanxiyn
[rust.git] / src / tools / clippy / 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
58         for entry in WalkDir::new(project_root.join("tests")) {
59             let entry = entry?;
60             let path = entry.path();
61
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"
66             {
67                 continue;
68             }
69
70             success &= rustfmt(context, &path)?;
71         }
72
73         Ok(success)
74     }
75
76     fn output_err(err: CliError) {
77         match err {
78             CliError::CommandFailed(command, stderr) => {
79                 eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
80             },
81             CliError::IoError(err) => {
82                 eprintln!("error: {}", err);
83             },
84             CliError::RustfmtNotInstalled => {
85                 eprintln!("error: rustfmt nightly is not installed.");
86             },
87             CliError::WalkDirError(err) => {
88                 eprintln!("error: {}", err);
89             },
90             CliError::RaSetupActive => {
91                 eprintln!(
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."
95                 );
96             },
97         }
98     }
99
100     let context = FmtContext { check, verbose };
101     let result = try_run(&context);
102     let code = match result {
103         Ok(true) => 0,
104         Ok(false) => {
105             eprintln!();
106             eprintln!("Formatting check failed.");
107             eprintln!("Run `cargo dev fmt` to update formatting.");
108             1
109         },
110         Err(err) => {
111             output_err(err);
112             1
113         },
114     };
115     process::exit(code);
116 }
117
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();
120
121     format!(
122         "cd {} && {} {}",
123         escape(dir.as_ref().to_string_lossy()),
124         escape(program.as_ref().to_string_lossy()),
125         arg_display.join(" ")
126     )
127 }
128
129 fn exec(
130     context: &FmtContext,
131     program: impl AsRef<OsStr>,
132     dir: impl AsRef<Path>,
133     args: &[impl AsRef<OsStr>],
134 ) -> Result<bool, CliError> {
135     if context.verbose {
136         println!("{}", format_command(&program, &dir, args));
137     }
138
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();
142
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),
148         ));
149     }
150
151     Ok(success)
152 }
153
154 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
155     let mut args = vec!["+nightly", "fmt", "--all"];
156     if context.check {
157         args.push("--");
158         args.push("--check");
159     }
160     let success = exec(context, "cargo", path, &args)?;
161
162     Ok(success)
163 }
164
165 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
166     let program = "rustfmt";
167     let dir = std::env::current_dir()?;
168     let args = &["+nightly", "--version"];
169
170     if context.verbose {
171         println!("{}", format_command(&program, &dir, args));
172     }
173
174     let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
175
176     if output.status.success() {
177         Ok(())
178     } else if std::str::from_utf8(&output.stderr)
179         .unwrap_or("")
180         .starts_with("error: 'rustfmt' is not installed")
181     {
182         Err(CliError::RustfmtNotInstalled)
183     } else {
184         Err(CliError::CommandFailed(
185             format_command(&program, &dir, args),
186             std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
187         ))
188     }
189 }
190
191 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
192     let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
193     if context.check {
194         args.push("--check".as_ref());
195     }
196     let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
197     if !success {
198         eprintln!("rustfmt failed on {}", path.display());
199     }
200     Ok(success)
201 }