|
| 1 | +use std::fs; |
| 2 | +use xshell::Shell; |
| 3 | + |
| 4 | +use crate::environment::{get_crate_dirs, println_verbose}; |
| 5 | +use crate::quiet_cmd; |
| 6 | + |
| 7 | +/// Configuration for allowed duplicate dependencies. |
| 8 | +#[derive(Debug, serde::Deserialize)] |
| 9 | +struct WhitelistConfig { |
| 10 | + #[serde(default)] |
| 11 | + allowed_duplicates: Vec<String>, |
| 12 | +} |
| 13 | + |
| 14 | +/// Run the lint task. |
| 15 | +pub fn run(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> { |
| 16 | + println_verbose("Running lint task..."); |
| 17 | + |
| 18 | + lint_workspace(sh)?; |
| 19 | + lint_crates(sh)?; |
| 20 | + check_duplicate_deps(sh)?; |
| 21 | + |
| 22 | + println_verbose("Lint task completed successfully"); |
| 23 | + Ok(()) |
| 24 | +} |
| 25 | + |
| 26 | +/// Lint the workspace with clippy. |
| 27 | +fn lint_workspace(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> { |
| 28 | + println_verbose("Linting workspace..."); |
| 29 | + |
| 30 | + // Run clippy on workspace with all features. |
| 31 | + quiet_cmd!( |
| 32 | + sh, |
| 33 | + "cargo clippy --workspace --all-targets --all-features --keep-going" |
| 34 | + ) |
| 35 | + .args(&["--", "-D", "warnings"]) |
| 36 | + .run()?; |
| 37 | + |
| 38 | + // Run clippy on workspace without features. |
| 39 | + quiet_cmd!(sh, "cargo clippy --workspace --all-targets --keep-going") |
| 40 | + .args(&["--", "-D", "warnings"]) |
| 41 | + .run()?; |
| 42 | + |
| 43 | + Ok(()) |
| 44 | +} |
| 45 | + |
| 46 | +/// Run extra crate-specific lints. |
| 47 | +/// |
| 48 | +/// # Why run at the crate level? |
| 49 | +/// |
| 50 | +/// When running `cargo clippy --workspace --no-default-features`, cargo resolves |
| 51 | +/// features across the entire workspace, which can enable features through dependencies |
| 52 | +/// even when a crate's own default features are disabled. Running clippy on each crate |
| 53 | +/// individually ensures that each crate truly compiles and passes lints with only its |
| 54 | +/// explicitly enabled features. |
| 55 | +fn lint_crates(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> { |
| 56 | + println_verbose("Running crate-specific lints..."); |
| 57 | + |
| 58 | + let crate_dirs = get_crate_dirs(sh)?; |
| 59 | + println_verbose(&format!("Found crates: {}", crate_dirs.join(", "))); |
| 60 | + |
| 61 | + for crate_dir in crate_dirs { |
| 62 | + let _old_dir = sh.push_dir(&crate_dir); |
| 63 | + |
| 64 | + // Run clippy without default features. |
| 65 | + quiet_cmd!( |
| 66 | + sh, |
| 67 | + "cargo clippy --all-targets --no-default-features --keep-going" |
| 68 | + ) |
| 69 | + .args(&["--", "-D", "warnings"]) |
| 70 | + .run()?; |
| 71 | + } |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
| 75 | + |
| 76 | +/// Check for duplicate dependencies. |
| 77 | +fn check_duplicate_deps(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> { |
| 78 | + println_verbose("Checking for duplicate dependencies..."); |
| 79 | + |
| 80 | + // Load whitelist configuration if it exists. |
| 81 | + let whitelist_path = sh.current_dir().join("contrib/whitelist_dups.toml"); |
| 82 | + let allowed_duplicates = if whitelist_path.exists() { |
| 83 | + println_verbose(&format!( |
| 84 | + "Loading whitelist from {}", |
| 85 | + whitelist_path.display() |
| 86 | + )); |
| 87 | + let contents = fs::read_to_string(&whitelist_path)?; |
| 88 | + let config: WhitelistConfig = toml::from_str(&contents)?; |
| 89 | + config.allowed_duplicates |
| 90 | + } else { |
| 91 | + Vec::new() |
| 92 | + }; |
| 93 | + |
| 94 | + // Run cargo tree to find duplicates. |
| 95 | + let output = quiet_cmd!(sh, "cargo tree --target=all --all-features --duplicates") |
| 96 | + .ignore_status() |
| 97 | + .read()?; |
| 98 | + |
| 99 | + let duplicates: Vec<&str> = output |
| 100 | + .lines() |
| 101 | + // Filter out non crate names. |
| 102 | + .filter(|line| line.chars().next().is_some_and(|c| c.is_alphanumeric())) |
| 103 | + // Filter out whitelisted crates. |
| 104 | + .filter(|line| { |
| 105 | + !allowed_duplicates |
| 106 | + .iter() |
| 107 | + .any(|allowed| line.contains(allowed)) |
| 108 | + }) |
| 109 | + .collect(); |
| 110 | + |
| 111 | + if !duplicates.is_empty() { |
| 112 | + eprintln!("Error: Found duplicate dependencies in workspace!"); |
| 113 | + for dup in &duplicates { |
| 114 | + eprintln!(" {}", dup); |
| 115 | + } |
| 116 | + // Show full tree for context. |
| 117 | + quiet_cmd!(sh, "cargo tree --target=all --all-features --duplicates").run()?; |
| 118 | + return Err("Dependency tree contains duplicates".into()); |
| 119 | + } |
| 120 | + |
| 121 | + println_verbose("No duplicate dependencies found"); |
| 122 | + Ok(()) |
| 123 | +} |
0 commit comments