1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use anyhow::{Error, Result};
use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId};
use itertools::Itertools;
use std::collections::HashSet;

const DEFAULT_DENIED_DUPLICATE_CRATES: [&str; 1] = ["gstd"];
/// Returns the list of crates name.
fn denied_duplicate_crates() -> HashSet<&'static str> {
    option_env!("__GEAR_WASM_BUILDER_DENIED_DUPLICATE_CRATES").map_or_else(
        || DEFAULT_DENIED_DUPLICATE_CRATES.into(),
        |v| v.split(',').collect(),
    )
}

pub(crate) fn check(metadata: &Metadata, root_id: &PackageId) -> Result<()> {
    let denied_duplicate_crates = denied_duplicate_crates();
    let mut packages = metadata.packages.clone();
    packages.sort_by(|a, b| a.name.cmp(&b.name));

    let mut duplicates: Vec<String> = Vec::new();

    if let Some(resolve) = &metadata.resolve {
        for (name, group) in &packages
            .iter()
            .filter(|p| denied_duplicate_crates.contains(&p.name.as_str()))
            .chunk_by(|p| &p.name)
        {
            let group: Vec<&Package> = group.collect();

            if group.len() <= 1 {
                continue;
            }

            if group
                .iter()
                .all(|p| is_normal_dep(&resolve.nodes, root_id, &p.id))
            {
                let mut versions: Vec<_> = group.into_iter().map(|p| &p.version).collect();
                versions.sort();
                let versions = versions.iter().join(", ");

                duplicates.push(format!(
                    "multiple versions for dependency `{name}`: {versions}"
                ));
            }
        }
    }

    if duplicates.is_empty() {
        Ok(())
    } else {
        Err(Error::msg(duplicates.join("\n")))
    }
}

fn is_normal_dep(nodes: &[Node], local_id: &PackageId, dep_id: &PackageId) -> bool {
    fn depends_on(node: &Node, dep_id: &PackageId) -> bool {
        node.deps.iter().any(|dep| {
            dep.pkg == *dep_id
                && dep
                    .dep_kinds
                    .iter()
                    .any(|info| matches!(info.kind, DependencyKind::Normal))
        })
    }

    nodes
        .iter()
        .filter(|node| depends_on(node, dep_id))
        .any(|node| node.id == *local_id || is_normal_dep(nodes, local_id, &node.id))
}