use anyhow::{Context, Result};
use crate::project::Project;
use colored::Colorize;
use std::fs;
use std::path::Path;
use std::process::Command;
use walkdir::WalkDir;
#[derive(Debug, Clone, serde::Serialize)]
pub struct UnusedDependency {
pub name: String,
pub location: String, }
#[derive(Debug, serde::Serialize)]
pub struct DependencyCleanResult {
pub path: String,
pub success: bool,
pub unused_deps: Vec<UnusedDependency>,
pub removed_count: usize,
pub error: Option<String>,
}
fn extract_dependencies(cargo_toml_path: &Path) -> Result<Vec<(String, String)>> {
let content = fs::read_to_string(cargo_toml_path)
.with_context(|| format!("Failed to read Cargo.toml: {:?}", cargo_toml_path))?;
let toml: toml::Value = toml::from_str(&content)
.with_context(|| format!("Failed to parse Cargo.toml: {:?}", cargo_toml_path))?;
let mut deps = Vec::new();
if let Some(deps_table) = toml.get("dependencies").and_then(|v| v.as_table()) {
for (name, _) in deps_table {
deps.push((name.clone(), "[dependencies]".to_string()));
}
}
if let Some(dev_deps_table) = toml.get("dev-dependencies").and_then(|v| v.as_table()) {
for (name, _) in dev_deps_table {
deps.push((name.clone(), "[dev-dependencies]".to_string()));
}
}
if let Some(build_deps_table) = toml.get("build-dependencies").and_then(|v| v.as_table()) {
for (name, _) in build_deps_table {
deps.push((name.clone(), "[build-dependencies]".to_string()));
}
}
Ok(deps)
}
fn normalize_crate_name(name: &str) -> String {
name.replace('-', "_")
}
fn search_patterns_for(normalized_dep: &str) -> [String; 7] {
[
format!("use {}::", normalized_dep),
format!("use {};", normalized_dep),
format!("use crate::{}", normalized_dep),
format!("{}::", normalized_dep),
format!("extern crate {}", normalized_dep),
format!("{}!", normalized_dep),
format!("#[{}", normalized_dep),
]
}
fn collect_source_text(project_path: &Path) -> Vec<String> {
let mut contents: Vec<String> = Vec::new();
for sub in ["src", "examples", "tests"] {
let dir = project_path.join(sub);
if !dir.exists() {
continue;
}
for entry in WalkDir::new(&dir).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file()
&& entry.path().extension().is_some_and(|e| e == "rs")
{
if let Ok(content) = fs::read_to_string(entry.path()) {
contents.push(content);
}
}
}
}
let build_rs = project_path.join("build.rs");
if let Ok(content) = fs::read_to_string(&build_rs) {
contents.push(content);
}
contents
}
fn is_dependency_used(
dep_name: &str,
sources: &[String],
cargo_toml_content: &str,
) -> bool {
let normalized = normalize_crate_name(dep_name);
let patterns = search_patterns_for(&normalized);
for content in sources {
for pattern in &patterns {
if content.contains(pattern.as_str()) {
return true;
}
}
}
if cargo_toml_content.contains(&format!("{}/", dep_name))
|| cargo_toml_content.contains(&format!("{}-", dep_name))
|| cargo_toml_content.contains(&format!("{}/", normalized))
|| cargo_toml_content.contains(&format!("{}-", normalized))
{
return true;
}
false
}
pub fn check_unused_dependencies(project: &Project) -> Result<Vec<UnusedDependency>> {
let cargo_toml = project.path.join("Cargo.toml");
if !cargo_toml.exists() {
return Ok(vec![]);
}
let all_deps = extract_dependencies(&cargo_toml)?;
const SKIP_LIST: &[&str] = &[
"proc-macro2",
"quote",
"syn",
"serde",
"serde_derive",
"serde_json", ];
let deps_to_check: Vec<(String, String)> = all_deps
.into_iter()
.filter(|(name, _)| {
!SKIP_LIST.contains(&name.as_str())
&& !name.ends_with("_derive")
&& !name.contains("proc-macro")
})
.collect();
if deps_to_check.is_empty() {
return Ok(vec![]);
}
let sources = collect_source_text(&project.path);
let cargo_content = fs::read_to_string(&cargo_toml).unwrap_or_default();
let mut unused = Vec::new();
for (dep_name, location) in deps_to_check {
if !is_dependency_used(&dep_name, &sources, &cargo_content) {
unused.push(UnusedDependency {
name: dep_name,
location,
});
}
}
Ok(unused)
}
pub fn remove_unused_dependencies(
project: &Project,
unused_deps: &[UnusedDependency],
dry_run: bool,
verbose: bool,
) -> Result<usize> {
if dry_run || unused_deps.is_empty() {
return Ok(0);
}
let check_output = Command::new("cargo")
.args(["remove", "--help"])
.output();
match check_output {
Ok(output) if output.status.success() => {
}
_ => {
return Err(anyhow::anyhow!(
"cargo-remove is not installed. Install it with: cargo install cargo-edit"
));
}
}
let mut removed = 0;
let mut errors = Vec::new();
for dep in unused_deps {
if verbose {
println!(" {} Attempting to remove dependency: {} ({})", "[DEBUG]".cyan(), dep.name, dep.location);
}
let is_dev = dep.location.contains("dev-dependencies");
let is_build = dep.location.contains("build-dependencies");
let mut cmd_args = vec!["remove".to_string(), dep.name.clone()];
if is_dev {
cmd_args.push("--dev".to_string());
} else if is_build {
cmd_args.push("--build".to_string());
}
let output = Command::new("cargo")
.args(&cmd_args)
.current_dir(&project.path)
.output()
.with_context(|| format!("Failed to run `cargo remove {}`", dep.name))?;
if output.status.success() {
removed += 1;
if verbose {
println!(" {} Successfully removed: {} ({})", "[DEBUG]".green(), dep.name, dep.location);
}
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let error_msg = format!("Failed to remove {} ({}): {}", dep.name, dep.location, stderr);
errors.push(error_msg.clone());
if verbose {
println!(" {} Failed to remove {} ({}): {}", "[DEBUG]".red(), dep.name, dep.location, stderr);
}
}
}
if !errors.is_empty() && removed == 0 {
return Err(anyhow::anyhow!(
"Failed to remove dependencies:\n{}",
errors.join("\n")
));
}
Ok(removed)
}
pub fn clean_dependencies(
project: &Project,
dry_run: bool,
remove: bool,
verbose: bool,
) -> Result<DependencyCleanResult> {
let unused_deps = check_unused_dependencies(project)
.with_context(|| format!("Failed to check unused dependencies in {:?}", project.path))?;
let removed_count = if remove && !unused_deps.is_empty() {
match remove_unused_dependencies(project, &unused_deps, dry_run, verbose) {
Ok(count) => count,
Err(e) => {
return Ok(DependencyCleanResult {
path: project.path.to_string_lossy().to_string(),
success: false,
unused_deps,
removed_count: 0,
error: Some(e.to_string()),
});
}
}
} else {
0
};
Ok(DependencyCleanResult {
path: project.path.to_string_lossy().to_string(),
success: true,
unused_deps,
removed_count,
error: None,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalize_crate_name() {
assert_eq!(normalize_crate_name("my-crate"), "my_crate");
assert_eq!(normalize_crate_name("my_crate"), "my_crate");
assert_eq!(normalize_crate_name("serde-json"), "serde_json");
}
#[test]
fn test_extract_dependencies() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
tokio = "1.0"
[dev-dependencies]
tempfile = "3.0"
"#,
).unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
assert!(deps.len() >= 2);
let dep_names: Vec<String> = deps.iter().map(|(n, _)| n.clone()).collect();
assert!(dep_names.contains(&"serde".to_string()));
assert!(dep_names.contains(&"tokio".to_string()));
}
#[test]
fn test_extract_dependencies_includes_build_deps() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
[build-dependencies]
cc = "1.0"
"#,
)
.unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
let names: Vec<String> = deps.iter().map(|(n, _)| n.clone()).collect();
assert!(names.contains(&"cc".to_string()));
let cc_loc = deps
.iter()
.find(|(n, _)| n == "cc")
.map(|(_, l)| l.as_str())
.unwrap();
assert_eq!(cc_loc, "[build-dependencies]");
}
#[test]
fn test_is_dependency_used_with_use_statement() {
let sources = vec!["use serde::Deserialize;".to_string()];
assert!(is_dependency_used("serde", &sources, ""));
}
#[test]
fn test_is_dependency_used_unused() {
let sources = vec!["fn main() {}".to_string()];
assert!(!is_dependency_used("serde", &sources, ""));
}
#[test]
fn test_is_dependency_used_dash_underscore_normalization() {
let sources = vec!["use serde_json::Value;".to_string()];
assert!(is_dependency_used("serde-json", &sources, ""));
}
#[test]
fn test_is_dependency_used_macro_invocation() {
let sources = vec!["println!(\"hi\");".to_string()];
assert!(is_dependency_used("println", &sources, ""));
}
#[test]
fn test_is_dependency_used_attribute() {
let sources = vec!["#[derive(Debug)]".to_string()];
assert!(is_dependency_used("derive", &sources, ""));
}
#[test]
fn test_is_dependency_used_cargo_feature_reference() {
let sources = vec!["fn main() {}".to_string()];
let cargo = r#"[features]
feat1 = ["some-crate/feature"]
"#;
assert!(is_dependency_used("some-crate", &sources, cargo));
}
#[test]
fn test_check_unused_dependencies_finds_unused() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
rayon = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
names.contains(&"rayon".to_string()),
"rayon should be detected as unused, got: {:?}",
names
);
}
#[test]
fn test_check_unused_dependencies_no_false_positive_for_used() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
rayon = "1.0"
"#,
)
.unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"use rayon::prelude::*;\nfn main() {}",
)
.unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
!names.contains(&"rayon".to_string()),
"rayon is used and should not be flagged as unused"
);
}
#[test]
fn test_check_unused_dependencies_skips_skip_list() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
serde_derive = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
!names.contains(&"serde".to_string()),
"serde is in skip list and should not be flagged"
);
}
#[test]
fn test_collect_source_text_includes_build_rs() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::create_dir_all(temp_dir.path().join("examples")).unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
fs::write(
temp_dir.path().join("examples/demo.rs"),
"fn main() {}",
)
.unwrap();
fs::write(temp_dir.path().join("build.rs"), "fn main() {}").unwrap();
let texts = collect_source_text(temp_dir.path());
assert_eq!(texts.len(), 3);
}
#[test]
fn test_collect_source_text_empty_project() {
let temp_dir = tempfile::TempDir::new().unwrap();
let texts = collect_source_text(temp_dir.path());
assert!(texts.is_empty());
}
}