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 doesn't indicate that feature-enabled transitive platform-specific deps are platform-specific #9863

Open
illicitonion opened this issue Aug 31, 2021 · 0 comments
Labels
C-bug Category: bug Command-metadata S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@illicitonion
Copy link
Contributor

illicitonion commented Aug 31, 2021

Problem
If on one platform a crate enables a feature which enables a transitive dependency, and on another it doesn't, there's no way to capture this from the output of cargo metadata.

Steps

  1. In this repo run cargo metadata or cargo metadata --filter-platform=x86_64-unknown-linux-gnu. In this crate, serde's derive feature is enabled for x86_64-apple-darwin but not for x86_64-unknown-linux-gnu, which means it depends on serde-derive on x86_64-apple-darwin but not on x86_64-unknown-linux-gnu.
  2. Try to work out whether serde-derive should be built on x86_64-unknown-linux-gnu.
  3. Find that there's not enough information to determine this.

Possible Solution(s)

  1. We could populate DepKindInfo.target for these dependencies, such that they can be identified as platform-specific
  2. When running with --filter-platform for a non-matching platform, we could omit these dependencies. That may require callers to run cargo metadata multiple times and splice the results, which would be a bit unfortunate.

Notes

Output of cargo version: cargo 1.54.0 (5ae8d74b3 2021-06-22), also cargo 1.56.0-nightly (f559c109c 2021-08-26)

This issue makes it hard to translate crates.io packages for building with non-cargo build systems.

@illicitonion illicitonion added the C-bug Category: bug label Aug 31, 2021
@ehuss ehuss added Command-metadata E-medium Experience: Medium labels Sep 11, 2021
illicitonion added a commit to illicitonion/cargo that referenced this issue Oct 25, 2021
If e.g. an optional feature is only enabled on certain platforms,
meaning a transitive dependency is only enabled on certain platforms,
currently that information can't be worked out from the output of
`cargo metadata`.

With this change, when a dependency is configured for a specific
platform, its dependencies are by default also configured for that
specific platform, unless they explicitly have a different platform
attached to them.

This makes it easier to translate crates.io packages for building with
non-cargo build systems, because this platform-specific behaviour of
feature resolution is now observable in `cargo metadata`, rather than
requiring those systems to track and re-implement feature resolution
themselves with regard to platforms.

Fixes rust-lang#9863.
@epage epage added S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. and removed E-medium Experience: Medium labels Oct 16, 2023
github-merge-queue bot pushed a commit to bazelbuild/rules_rust that referenced this issue May 8, 2024
The Cargo [Feature Resolver version
2](https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2)
behavior is currently not supported by the `cargo metadata` sub command
(rust-lang/cargo#9863) which `crate_universe`
uses to determine the dependencies of a target, leading to inaccuracies
when dependencies are introduced via feature resolution for a particular
configuration.

In #1710 functionality was
added to use `cargo tree` to perform feature resolution for each
supported platform. This change expands on this trick to collect
dependency information at the same time and use that to determine
whether or not to include optional dependencies located in standard
`cargo metadata` output in the rendered Bazel targets. Non optional or
`target.cfg` (conditional) dependencies behave as they did before this
change.

Implementation details:
- `FeatureGenerator` was replaced by `TreeResolver`
- Optional dependencies are now rendered as selects on explicit
platforms. This will expand the size of `cargo-bazel-lock.json` files
but is expected to be more correct.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-metadata S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants