-use clippy_dev::clippy_project_root;
+use crate::clippy_project_root;
use shell_escape::escape;
use std::ffi::OsStr;
-use std::io;
use std::path::Path;
use std::process::{self, Command};
+use std::{fs, io};
use walkdir::WalkDir;
#[derive(Debug)]
pub enum CliError {
- CommandFailed(String),
+ CommandFailed(String, String),
IoError(io::Error),
RustfmtNotInstalled,
WalkDirError(walkdir::Error),
+ RaSetupActive,
}
impl From<io::Error> for CliError {
verbose: bool,
}
+// the "main" function of cargo dev fmt
pub fn run(check: bool, verbose: bool) {
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
let mut success = true;
let project_root = clippy_project_root();
+ // if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
+ // format because rustfmt would also format the entire rustc repo as it is a local
+ // dependency
+ if fs::read_to_string(project_root.join("Cargo.toml"))
+ .expect("Failed to read clippy Cargo.toml")
+ .contains(&"[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
+ {
+ return Err(CliError::RaSetupActive);
+ }
+
rustfmt_test(context)?;
success &= cargo_fmt(context, project_root.as_path())?;
fn output_err(err: CliError) {
match err {
- CliError::CommandFailed(command) => {
- eprintln!("error: A command failed! `{}`", command);
+ CliError::CommandFailed(command, stderr) => {
+ eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
},
CliError::IoError(err) => {
eprintln!("error: {}", err);
CliError::WalkDirError(err) => {
eprintln!("error: {}", err);
},
+ CliError::RaSetupActive => {
+ eprintln!(
+ "error: a local rustc repo is enabled as path dependency via `cargo dev ra_setup`.
+Not formatting because that would format the local repo as well!
+Please revert the changes to Cargo.tomls first."
+ );
+ },
}
}
println!("{}", format_command(&program, &dir, args));
}
- let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
- let code = child.wait()?;
- let success = code.success();
+ let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
+ let output = child.wait_with_output()?;
+ let success = output.status.success();
if !context.check && !success {
- return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
+ let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
+ return Err(CliError::CommandFailed(
+ format_command(&program, &dir, args),
+ String::from(stderr),
+ ));
}
Ok(success)
{
Err(CliError::RustfmtNotInstalled)
} else {
- Err(CliError::CommandFailed(format_command(&program, &dir, args)))
+ Err(CliError::CommandFailed(
+ format_command(&program, &dir, args),
+ std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
+ ))
}
}