Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo metadata's resolved output includes features from all packages in workspace, cannot be filtered. #12735

Closed
JarredAllen opened this issue Sep 25, 2023 · 1 comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-metadata S-triage Status: This issue is waiting on initial triage.

Comments

@JarredAllen
Copy link

Problem

I'm working on a tool that wraps multiple cargo build invocations and trying to make a progress bar for the overall output. Towards this end, I need to know, for each invocation, how many artifacts it needs to build, which I believe is the point of the cargo metadata command (and corresponding cargo_metadata crate that wraps it for Rust).

The problem is that I'm building in a workspace where I have something like the following setup:

  • Crate A exists in my workspace.
  • Crate B has an optional dependency on Crate A, if the a feature is supplied.
  • Crate C depends on Crate B but doesn't require the a feature.
  • Crate D depends on Crate B and requires the a feature.

When I run cargo metadata, it builds the dependency tree for the entire workspace, so I see that Crate B depends on Crate A (because Crate D requires the feature, and features are additive). However, when I run cargo build -p C to only build Crate C, the feature is not enabled (because Crate D doesn't appear) so only crates B and C are built. Thus, naively reading the dependency tree provided says 3 crates, but then cargo build only reports 2 crates.

The docs for cargo metadata say the following about how it decides which features are enabled:

The enabled features are based on the enabled features for the "current" package. Inactivated optional dependencies are not listed.

I want to set this "current package" to Crate C, which will set the features based on that. However, as far as I can tell, cargo metadata always sets this "current package" to be every package in the workspace, and it takes all the features from combining every crate. This even happens if I use --manifest-path to explicitly specify the Cargo.toml file from Crate C.

Proposed Solution

My preferred interface would be to add a --package option to cargo metadata which allows you to explicitly filter packages in the workspace, repeatable if you want to have multiple packages at the root of your dependency tree.

If combined with --no-deps, then only the explicitly-listed packages are included in the output. Otherwise, it outputs the listed packages and all of their transitive dependencies, from the workspace or from outside. I primarily care about the resolve field, and don't care much how this interacts with the packages field.

If only one --package argument is required, then any features listed in a --features argument applies to that one package (e.g. cargo metadata --package my_package --features foo,bar will enable the foo and bar features of my_package).

Beyond those two, I think the interactions with the other existing flags and arguments are straightforward and uncomplicated.

Notes

Coming from this rust-users forum thread, I assume what I want isn't currently possible based on the lack of responses, please correct me if I'm wrong.

@JarredAllen JarredAllen added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Sep 25, 2023
@epage
Copy link
Contributor

epage commented Sep 25, 2023

It looks like this is a duplicate of #7754. Closing in favor of that. If there is some difference I missed, let us know!

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-metadata S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants