Skip to content

Commit cc779c8

Browse files
author
Michael Wright
committed
dev-fmt: better error handling
Check if rustfmt is installed at the start and exit if it isn't.
1 parent dc69a5c commit cc779c8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

clippy_dev/src/fmt.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum CliError {
1010
CommandFailed(String),
1111
IoError(io::Error),
1212
ProjectRootNotFound,
13+
RustfmtNotInstalled,
1314
WalkDirError(walkdir::Error),
1415
}
1516

@@ -36,6 +37,8 @@ pub fn run(check: bool, verbose: bool) {
3637

3738
let project_root = project_root()?;
3839

40+
rustfmt_test(context)?;
41+
3942
success &= cargo_fmt(context, project_root.as_path())?;
4043
success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
4144
success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
@@ -69,6 +72,9 @@ pub fn run(check: bool, verbose: bool) {
6972
CliError::ProjectRootNotFound => {
7073
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
7174
},
75+
CliError::RustfmtNotInstalled => {
76+
eprintln!("error: rustfmt nightly is not installed.");
77+
},
7278
CliError::WalkDirError(err) => {
7379
eprintln!("error: {}", err);
7480
},
@@ -139,6 +145,29 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
139145
Ok(success)
140146
}
141147

148+
fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
149+
let program = "rustfmt";
150+
let dir = std::env::current_dir()?;
151+
let args = &["+nightly", "--version"];
152+
153+
if context.verbose {
154+
println!("{}", format_command(&program, &dir, args));
155+
}
156+
157+
let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
158+
159+
if output.status.success() {
160+
Ok(())
161+
} else if std::str::from_utf8(&output.stderr)
162+
.unwrap_or("")
163+
.starts_with("error: 'rustfmt' is not installed")
164+
{
165+
Err(CliError::RustfmtNotInstalled)
166+
} else {
167+
Err(CliError::CommandFailed(format_command(&program, &dir, args)))
168+
}
169+
}
170+
142171
fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
143172
let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
144173
if context.check {

0 commit comments

Comments
 (0)