Skip to content

Instantly share code, notes, and snippets.

@divi255
Created June 1, 2023 16:39
Show Gist options
  • Save divi255/8575aa22df0825291933fd03194421f6 to your computer and use it in GitHub Desktop.
Save divi255/8575aa22df0825291933fd03194421f6 to your computer and use it in GitHub Desktop.
Use cargo metadata to deny build if some dependency crate feature has been enabled
use cargo_metadata::{CargoOpt, MetadataCommand};
use std::path::Path;
fn main() {
let mut manifest = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).to_owned();
manifest.push("Cargo.toml");
let metadata = MetadataCommand::new()
.manifest_path(manifest)
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();
if let Some(resolve) = metadata.resolve {
for node in &resolve.nodes {
if node.id.repr.starts_with("serde_json ") {
if node.features.iter().any(|v| v == "arbitrary_precision") {
let deps = resolve
.nodes
.iter()
.filter(|d| d.dependencies.contains(&node.id))
.map(|d| d.id.repr.as_str())
.collect::<Vec<&str>>();
panic!(
"serde_json arbitrary_precision MUST be off. Dependents: \n\n{}\n\n",
deps.join("\n")
);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment