-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wasm_builder): Multiple crate versions check (#4159)
Co-authored-by: Dmitrii Novikov <[email protected]>
- Loading branch information
1 parent
b7c2683
commit 81fe12d
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters