Skip to content

Commit

Permalink
fix(cargo-bazel): ignore example crates when checking if proc-macro (#…
Browse files Browse the repository at this point in the history
…2596)

# WHAT

* ignore example crate types when checking if it is a proc-macro

# WHY

When building the list of `deps` and `proc_macro_deps` for a given
crate,
dependency crates which contain examples in their Cargo.yaml of of type
`proc-macro` cause the dependency to be added to the `proc_macro_deps`.
The
example crate types should be ignored.

Fixes #2577

Co-authored-by: UebelAndre <[email protected]>
  • Loading branch information
qtica and UebelAndre authored Apr 2, 2024
1 parent 4769fe6 commit fe2bc68
Show file tree
Hide file tree
Showing 5 changed files with 2,157 additions and 4 deletions.
32 changes: 28 additions & 4 deletions crate_universe/src/metadata/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ fn is_lib_package(package: &Package) -> bool {
}

fn is_proc_macro_package(package: &Package) -> bool {
package
.targets
.iter()
.any(|target| target.crate_types.iter().any(|t| t == "proc-macro"))
package.targets.iter().any(|target| {
target.crate_types.iter().any(|t| t == "proc-macro")
&& !target.kind.iter().any(|t| t == "example")
})
}

fn is_dev_dependency(node_dep: &NodeDep) -> bool {
Expand Down Expand Up @@ -425,6 +425,30 @@ mod test {
.unwrap()
}

#[test]
fn example_proc_macro_dep() {
let metadata = metadata::example_proc_macro_dep();

let node = find_metadata_node("example-proc-macro-dep", &metadata);
let dependencies = DependencySet::new_for_node(node, &metadata);

let normal_deps: Vec<_> = dependencies
.normal_deps
.items()
.into_iter()
.map(|(_, dep)| dep.target_name)
.collect();
assert_eq!(normal_deps, vec!["proc-macro-rules"]);

let proc_macro_deps: Vec<_> = dependencies
.proc_macro_deps
.items()
.into_iter()
.map(|(_, dep)| dep.target_name)
.collect();
assert_eq!(proc_macro_deps, Vec::<&str>::new());
}

#[test]
fn sys_dependencies() {
let metadata = metadata::build_scripts();
Expand Down
8 changes: 8 additions & 0 deletions crate_universe/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ pub(crate) mod metadata {
.unwrap()
}

pub(crate) fn example_proc_macro_dep() -> cargo_metadata::Metadata {
serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_data/metadata/example_proc_macro_dep/metadata.json"
)))
.unwrap()
}

pub(crate) fn git_repos() -> cargo_metadata::Metadata {
serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "example-proc-macro-dep"
version = "0.1.0"
edition = "2018"

# Required to satisfy cargo but no `lib.rs` is expected to
# exist within test data.
[lib]
path = "lib.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proc-macro-rules = "=0.4.0"
Loading

0 comments on commit fe2bc68

Please sign in to comment.