Skip to content
v0.1.1latest release34 skills2 categories4 sub-categories79 tested scripts157 reference docsContact us

dep_audit.rs

Helper script for rust-cargo-workspace-designer · 145 lines

Programming / Rustv1.0.0Updated OCT 21 20267 references6 scripts
scripts/dep_audit.rs
//! Check how a Cargo workspace declares its dependencies.
//!   CW003 duplicated-dependency        versions set per member instead of once in [workspace.dependencies]
//!   CW007 unversioned-path-dependency  path/git dependency without `version` in a publishable crate
//!   CW013 wildcard-version             version = "*"
//!   CW014 git-dependency-unpinned      git dependency without `rev` or `tag`
//!   CW015 inherit-undefined            `x.workspace = true` with nothing in the workspace to inherit
//!   CW016 default-features-ignored     member sets default-features = false on a workspace dep that enables them
//!
//! Build: rustc -O --edition 2021 scripts/dep_audit.rs -o /tmp/dep_audit
//! Usage: dep_audit <DIR|Cargo.toml|-> [--json] [--strict] [--select IDS] [--ignore IDS]

mod cli;
mod toml;
mod workspace;

use cli::{Finding, Rule};
use toml::Val;
use workspace::{dep_of, Dep, Manifest, Workspace};

const RULES: &[Rule] = &[
    Rule { id: "CW003", name: "duplicated-dependency", severity: "medium",
           why: "versions repeated per member drift apart and duplicate the crate in the build",
           fix: "declare it once in [workspace.dependencies] and use NAME.workspace = true (members may add features)" },
    Rule { id: "CW007", name: "unversioned-path-dependency", severity: "high",
           why: "crates.io rejects path/git dependencies without a version, so `cargo publish` fails",
           fix: "dep = { path = \"../x\", version = \"0.3.0\" } (or publish = false for internal crates)" },
    Rule { id: "CW013", name: "wildcard-version", severity: "high",
           why: "\"*\" accepts any future major version; crates.io rejects it",
           fix: "a caret requirement such as \"0.9\" or \"1.2\"" },
    Rule { id: "CW014", name: "git-dependency-unpinned", severity: "medium",
           why: "a git dependency without rev/tag follows a moving branch: builds change without a code change",
           fix: "rev = \"<commit>\" or tag = \"v1.2.3\" (Cargo.lock pins it, but cargo update silently moves it)" },
    Rule { id: "CW015", name: "inherit-undefined", severity: "high",
           why: "`workspace = true` points at a key the workspace does not define: Cargo refuses to build",
           fix: "add it to [workspace.dependencies] / [workspace.package] / [workspace.lints], or set it in the member" },
    Rule { id: "CW016", name: "default-features-ignored", severity: "medium",
           why: "a member cannot turn default features off when the workspace dependency leaves them on (Cargo warns and ignores it)",
           fix: "default-features = false in [workspace.dependencies]; members then add the features they need" },
];

const USAGE: &str = "usage: dep_audit <DIR|Cargo.toml|-> [options]

Check dependency declarations across a Cargo workspace: per-member versions that belong in
[workspace.dependencies] (CW003), unversioned path/git deps in publishable crates (CW007),
\"*\" versions (CW013), unpinned git deps (CW014), workspace inheritance of undefined keys
(CW015) and member default-features = false that Cargo ignores (CW016).";

fn check(ws: &Workspace, out: &mut Vec<Finding>) {
    let wt = ws.root.doc.table("workspace");
    let wdeps: Vec<Dep> = wt
        .and_then(|w| w.table("dependencies"))
        .map(|t| t.entries.iter().map(|(k, v, l)| dep_of("workspace.dependencies", k, v, *l)).collect())
        .unwrap_or_default();
    let mut push = |m: &Manifest, line: usize, id: &'static str, msg: String| {
        out.push(Finding { file: m.rel.clone(), line, id, msg, fix: String::new() });
    };
    let manifests: Vec<&Manifest> = std::iter::once(&ws.root).chain(ws.members.iter().filter(|m| m.dir != ws.root.dir)).collect();

    for d in &wdeps {
        if d.version.as_deref() == Some("*") {
            push(&ws.root, d.line, "CW013", format!("[workspace.dependencies] {} = \"*\"", d.key));
        }
        if d.git.is_some() && !d.pinned {
            push(&ws.root, d.line, "CW014", format!("[workspace.dependencies] {} follows a git branch", d.key));
        }
    }
    // External crates declared directly (not inherited) in more than one manifest.
    let direct: Vec<(&Manifest, Dep)> = manifests
        .iter()
        .flat_map(|m| m.deps().into_iter().map(move |d| (*m, d)))
        .filter(|(_, d)| !d.workspace && d.path.is_none() && d.section != "workspace.dependencies")
        .collect();
    for (m, d) in &direct {
        if wdeps.iter().any(|w| w.key == d.key) {
            push(m, d.line, "CW003", format!("{} sets its own `{}` although [workspace.dependencies] has it", m.name(), d.key));
            continue;
        }
        let others: Vec<String> = direct
            .iter()
            .filter(|(o, od)| o.dir != m.dir && od.package == d.package)
            .map(|(o, od)| format!("{} {}", o.name(), od.version.as_deref().unwrap_or("git")))
            .collect();
        if !others.is_empty() {
            push(m, d.line, "CW003", format!("`{}` {} is also declared by {}", d.key, d.version.as_deref().unwrap_or("(git)"), others.join(", ")));
        }
    }

    for m in &manifests {
        let pkg = m.package();
        for d in m.deps() {
            if d.version.as_deref() == Some("*") {
                push(m, d.line, "CW013", format!("{} depends on {} = \"*\"", m.name(), d.key));
            }
            if d.git.is_some() && !d.pinned {
                push(m, d.line, "CW014", format!("{} depends on git {} without rev or tag", m.name(), d.key));
            }
            let dev = d.section.ends_with("dev-dependencies") || d.section.ends_with("dev_dependencies");
            if pkg.is_some() && m.publishable() && !dev && !d.workspace && (d.path.is_some() || d.git.is_some()) && d.version.is_none() {
                let kind = if d.path.is_some() { "path" } else { "git" };
                push(m, d.line, "CW007", format!("{} is publishable but its {kind} dependency `{}` has no version", m.name(), d.key));
            }
            if d.workspace {
                match wdeps.iter().find(|w| w.key == d.key) {
                    None => push(m, d.line, "CW015", format!("`{}.workspace = true` but [workspace.dependencies] has no `{}`", d.key, d.key)),
                    Some(w) if d.default_features == Some(false) && w.default_features != Some(false) => push(m, d.line, "CW016",
                        format!("`default-features = false` on {} is ignored: [workspace.dependencies] enables them", d.key)),
                    _ => {}
                }
            }
        }
        if let Some(p) = pkg {
            let wpkg = wt.and_then(|w| w.table("package"));
            for (k, v, line) in &p.entries {
                if matches!(v, Val::Tbl(t) if t.get("workspace").and_then(Val::as_bool) == Some(true)) && wpkg.is_none_or(|w| w.get(k).is_none()) {
                    push(m, *line, "CW015", format!("`{k}.workspace = true` but [workspace.package] has no `{k}`"));
                }
            }
            let wants_lints = m.doc.table("lints").and_then(|l| l.get("workspace")).and_then(Val::as_bool) == Some(true);
            if wants_lints && wt.and_then(|w| w.table("lints")).is_none() {
                push(m, m.doc.table("lints").map_or(1, |l| l.line), "CW015", "`[lints] workspace = true` but the workspace has no [workspace.lints]".into());
            }
        }
    }
}

fn main() {
    let opts = cli::parse(USAGE, RULES, &[], &[]);
    let [arg] = opts.paths.as_slice() else { cli::die("pass one workspace directory, Cargo.toml or -") };
    let ws = workspace::load_arg(arg).unwrap_or_else(|e| cli::die(&e));
    for w in &ws.warnings {
        eprintln!("warning: {w}");
    }
    let mut found = Vec::new();
    check(&ws, &mut found);
    let findings: Vec<Finding> = found
        .into_iter()
        .filter(|f| RULES.iter().any(|r| r.id == f.id && opts.enabled(r)))
        .map(|mut f| {
            f.fix = RULES.iter().find(|r| r.id == f.id).map_or(String::new(), |r| r.fix.to_string());
            f
        })
        .collect();
    cli::finish(&opts, RULES, findings, 1 + ws.members.len(), "dependency")
}
More in Rust

rust-borrow-checker-error-explainer

Explain and fix Rust borrow-checker, ownership, move and lifetime compiler errors: E0382 use of moved value, E0499 cannot borrow as mutable more than once, E0502 also borrowed as immutable, E0505, E0506, E0503, E0507 cannot move out of index/Rc/reference, E0515 return reference to local, E0597 does not live long enough, E0716 temporary value dropped while borrowed, E0373 closure may outlive (spawn needs move), E0521 borrowed data escapes, E0384, E0596, E0594, E0381, E0713, 'lifetime may not live long enough', RefCell 'already borrowed: BorrowMutError' panics. Ranks fixes from restructuring (split borrows, entry API, split_at_mut, get_disjoint_mut, mem::take, Option::take, indices, thread::scope) down to clone and Rc<RefCell>/Arc<Mutex>; never unsafe. Covers NLL limits (get-or-insert), iterator invalidation, closure captures, self-referential structs. Ships std-only Rust triage tools.

rust-error-handling-thiserror-anyhow-designer

Design and review Rust error handling with thiserror 2 and anyhow 1: library vs application split, typed error enums with #[from], #[source], #[error(transparent)] and #[non_exhaustive], ErrorKind patterns, context via .context()/.with_context(), bail!/ensure!, source() chains, {:#} and {:?} reports, RUST_BACKTRACE/RUST_LIB_BACKTRACE, Box<dyn Error + Send + Sync>, main -> anyhow::Result or ExitCode, unwrap/expect/panic policy, Option to Result, collecting iterators of Results, axum IntoResponse mapping, retryable errors, tracing, async Send bounds, testing with matches! and downcast_ref, no_std core::error::Error, and snafu, eyre/color-eyre, miette, error-stack.

rust-lifetime-annotation-advisor

Decide when Rust lifetime annotations are needed and which ones: the three elision rules, structs holding references (struct Parser<'a>), owned vs borrowed (String, &str, Cow<'a, str>, Arc<str>), one vs several lifetimes, 'a: 'b bounds, T: 'static misconceptions, Box<dyn Trait + 'a> default object lifetimes, impl Trait + '_ and edition 2024 capture rules with use<..>, variance and PhantomData, HRTB for<'a> Fn(&'a T), GATs and lending iterators, async fn and tokio::spawn 'static, self-referential structs (ouroboros, self_cell, indices, arenas), Rc/Arc to escape lifetime soup. Ships std-only Rust checkers for redundant, missing, unused and over-restrictive lifetimes.

rust-tokio-async-service-builder

Build and review production async Rust services on Tokio 1.x and axum 0.8: runtime choice (#[tokio::main] multi_thread vs current_thread, Builder), axum routing with /{id} paths, State, extractors, IntoResponse error types, axum::serve with_graceful_shutdown, tower/tower-http middleware (TraceLayer, TimeoutLayer, CorsLayer, ConcurrencyLimitLayer, RequestBodyLimitLayer), Arc<AppState>, std vs tokio Mutex, JoinSet, TaskTracker, CancellationToken, select! cancel safety, bounded mpsc, oneshot, broadcast, watch, actors, Semaphore, timeouts, retries, spawn_blocking, sqlx PgPool, reqwest Client reuse, tracing, tests with start_paused and oneshot, Dockerfile. Fixes 'future cannot be sent between threads safely', E0373, Rc or MutexGuard held across await, 'Cannot start a runtime from within a runtime', 'Path segments must not start with :'. Ships std-only Rust scanners and a compiling axum 0.8 scaffold.

rust-trait-design-reviewer

Review and design Rust traits and generic APIs: generics vs impl Trait vs dyn Trait (static vs dynamic dispatch), dyn compatibility / object safety (generic methods, Self returns, where Self: Sized, error[E0038] not dyn compatible / cannot be made into an object), associated types vs generic parameters, blanket impls, coherence and orphan rule (E0117, E0119, E0210), newtype, extension, sealed and marker traits, supertraits and trait upcasting (1.86), async fn in traits (1.75) and the Send bound problem (trait_variant, async-trait), RPITIT, GATs, From/Into/TryFrom/AsRef/Borrow, std trait checklist (Debug, Clone, PartialEq, Hash, Default, Display, Error, Send/Sync, serde feature), #[must_use], #[non_exhaustive], semver hazards, Deref abuse, builders, typestate, PhantomData, Box<dyn Error + Send + Sync>, Fn traits, mockall.

rust-unsafe-block-auditor

Audit unsafe Rust for soundness: inventory every unsafe block, unsafe fn, unsafe impl/trait, extern block and unsafe attribute, check // SAFETY: comments and # Safety docs, and classify each as sound, needs-justification or likely UB, proposing safe alternatives first. Covers the five unsafe superpowers, aliasing (Stacked/Tree Borrows), invalid values, MaybeUninit vs mem::zeroed/uninitialized, transmute, slice::from_raw_parts, Vec::set_len/from_raw_parts, get_unchecked, CString::as_ptr dangling, unsafe impl Send/Sync, Pin projection, FFI (extern "C", repr(C), C-unwind, Box::into_raw/from_raw), strict provenance, static mut and &raw, edition 2024 (unsafe extern, #[unsafe(no_mangle)], unsafe_op_in_unsafe_fn, env::set_var), Miri, sanitizers, cargo-fuzz, loom, cargo-geiger and clippy safety lints.