~jplatte/cargo-depgraph

fd15b67373e28e903d50e4a5cb3e8f13e1a561e7 — Jonas Platte 1 year, 9 months ago 207c63e
Exclude proc-macros unless `--build-deps` is given
3 files changed, 36 insertions(+), 18 deletions(-)

M src/graph/build.rs
M src/package.rs
M src/util.rs
M src/graph/build.rs => src/graph/build.rs +17 -7
@@ 8,6 8,7 @@ use crate::{
    cli::Config,
    dep_info::{DepInfo, DepKind},
    package::Package,
    util::is_proc_macro,
};

pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Result<DepGraph> {


@@ 34,6 35,8 @@ pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Resu
        if !(config.root.is_empty() || config.root.contains(&pkg.name))
            // Excludes are specified and include this package
            || config.exclude.contains(&pkg.name)
            // Build dependencies are disabled and this package is a proc-macro
            || !config.build_deps && is_proc_macro(pkg)
        {
            continue;
        }


@@ 72,23 75,30 @@ pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Resu
                HashMapEntry::Occupied(o) => *o.get(),
                HashMapEntry::Vacant(v) => {
                    let is_workspace_member = metadata.workspace_members.contains(&dep.pkg);

                    // For workspace-only mode, don't add non-workspace
                    // dependencies to deps_add_queue or node_indices.
                    if config.workspace_only && !is_workspace_member {
                        // For workspace-only mode, don't add non-workspace
                        // dependencies to deps_add_queue or node_indices.
                        continue;
                    }

                    let idx = graph.add_node(Package::new(
                        get_package(&metadata.packages, &dep.pkg),
                        is_workspace_member,
                    ));
                    let dep_pkg = &get_package(&metadata.packages, &dep.pkg);
                    let dep_pkg = Package::new(dep_pkg, is_workspace_member);

                    // proc-macros are a bit weird because Cargo doesn't report
                    // them as build dependencies when really they are.
                    if !config.build_deps && dep_pkg.is_proc_macro {
                        continue;
                    }

                    let idx = graph.add_node(dep_pkg);
                    deps_add_queue.push_back(dep.pkg.clone());
                    v.insert(idx);
                    idx
                }
            };

            let child_is_proc_macro = graph[child_idx].dep_info.kind == DepKind::BUILD;
            let child_is_proc_macro = graph[child_idx].is_proc_macro;

            for info in &dep.dep_kinds {
                let extra = pkg.dependencies.iter().find(|d| {

M src/package.rs => src/package.rs +8 -11
@@ 6,7 6,10 @@ use std::{

use cargo_metadata::{semver::Version, Package as MetaPackage};

use crate::dep_info::{DepInfo, DepKind};
use crate::{
    dep_info::{DepInfo, DepKind},
    util::is_proc_macro,
};

#[derive(Clone)]
pub(crate) struct Package {


@@ 14,6 17,7 @@ pub(crate) struct Package {
    pub version: Version,
    pub dep_info: DepInfo,
    pub is_ws_member: bool,
    pub is_proc_macro: bool,

    pub name_uses: Option<Rc<Cell<u16>>>,
}


@@ 21,7 25,8 @@ pub(crate) struct Package {
impl Package {
    pub fn new(pkg: &MetaPackage, is_ws_member: bool) -> Self {
        let mut dep_info = DepInfo::default();
        if is_proc_macro(pkg) {
        let is_proc_macro = is_proc_macro(pkg);
        if is_proc_macro {
            dep_info.kind = DepKind::BUILD;
        }



@@ 30,6 35,7 @@ impl Package {
            version: pkg.version.clone(),
            dep_info,
            is_ws_member,
            is_proc_macro,
            name_uses: None,
        }
    }


@@ 45,12 51,3 @@ impl Debug for Package {
        Ok(())
    }
}

fn is_proc_macro(pkg: &MetaPackage) -> bool {
    let res = pkg.targets.iter().any(|t| t.kind.iter().any(|k| k == "proc-macro"));
    if res && pkg.targets.iter().any(|t| t.kind.iter().any(|k| k == "lib")) {
        eprintln!("enountered a crate that is both a regular library and a proc-macro");
    }

    res
}

M src/util.rs => src/util.rs +11 -0
@@ 1,5 1,7 @@
use std::{cell::Cell, collections::HashMap, rc::Rc};

use cargo_metadata::Package as MetaPackage;

use crate::graph::DepGraph;

pub(crate) fn set_name_stats(graph: &mut DepGraph) {


@@ 11,3 13,12 @@ pub(crate) fn set_name_stats(graph: &mut DepGraph) {
        pkg.name_uses = Some(name_uses);
    }
}

pub(crate) fn is_proc_macro(pkg: &MetaPackage) -> bool {
    let res = pkg.targets.iter().any(|t| t.kind.iter().any(|k| k == "proc-macro"));
    if res && pkg.targets.iter().any(|t| t.kind.iter().any(|k| k == "lib")) {
        eprintln!("enountered a crate that is both a regular library and a proc-macro");
    }

    res
}