+use clippy_dev::clippy_project_root;
use shell_escape::escape;
use std::ffi::OsStr;
use std::io;
-use std::path::{Path, PathBuf};
+use std::path::Path;
use std::process::{self, Command};
use walkdir::WalkDir;
pub enum CliError {
CommandFailed(String),
IoError(io::Error),
- ProjectRootNotFound,
RustfmtNotInstalled,
WalkDirError(walkdir::Error),
}
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
let mut success = true;
- let project_root = project_root()?;
+ let project_root = clippy_project_root();
rustfmt_test(context)?;
CliError::IoError(err) => {
eprintln!("error: {}", err);
},
- CliError::ProjectRootNotFound => {
- eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
- },
CliError::RustfmtNotInstalled => {
eprintln!("error: rustfmt nightly is not installed.");
},
Ok(false) => {
eprintln!();
eprintln!("Formatting check failed.");
- eprintln!("Run `./util/dev fmt` to update formatting.");
+ eprintln!("Run `cargo dev fmt` to update formatting.");
1
},
Err(err) => {
}
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
- let arg_display: Vec<_> = args
- .iter()
- .map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
- .collect();
+ let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
format!(
"cd {} && {} {}",
args.push("--");
args.push("--check");
}
- let success = exec(context, &bin_path("cargo"), path, &args)?;
+ let success = exec(context, "cargo", path, &args)?;
Ok(success)
}
fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
- let program = bin_path("rustfmt");
+ let program = "rustfmt";
let dir = std::env::current_dir()?;
let args = &["+nightly", "--version"];
if context.check {
args.push("--check".as_ref());
}
- let success = exec(context, &bin_path("rustfmt"), std::env::current_dir()?, &args)?;
+ let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
if !success {
eprintln!("rustfmt failed on {}", path.display());
}
Ok(success)
}
-
-fn project_root() -> Result<PathBuf, CliError> {
- let current_dir = std::env::current_dir()?;
- for path in current_dir.ancestors() {
- let result = std::fs::read_to_string(path.join("Cargo.toml"));
- if let Err(err) = &result {
- if err.kind() == io::ErrorKind::NotFound {
- continue;
- }
- }
-
- let content = result?;
- if content.contains("[package]\nname = \"clippy\"") {
- return Ok(path.to_path_buf());
- }
- }
-
- Err(CliError::ProjectRootNotFound)
-}
-
-// Workaround for https://github.com/rust-lang/cargo/issues/7475.
-// FIXME: replace `&bin_path("command")` with `"command"` once the issue is fixed
-fn bin_path(bin: &str) -> String {
- let mut p = home::cargo_home().unwrap();
- p.push("bin");
- p.push(bin);
- p.display().to_string()
-}