Skip to content

The cargo metadata --filter-platform flag defaults to $CARGO_BUILD_TARGET and can't be overridden #15270

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

Closed
alexcrichton opened this issue Mar 5, 2025 · 2 comments · Fixed by #15271
Labels

Comments

@alexcrichton
Copy link
Member

Today the --filter-platform flag to the cargo metadata subcommand defaults to $CARGO_BUILD_TARGET or the configuration of build.target within .cargo/config.toml. This behavior does not appear to be overridable (or I'm also perhaps missing something in which case this issue is moot), meaning that if a tool wants to execute cargo metadata specifically without a platform filter then there's no way for the tool to opt-in to that behavior.

This issue is downstream of mozilla/cargo-vet#652 where the bug doesn't actually lie in that repository, but rather in the interaction of the various features in play here. In short cargo vet runs cargo metadata to learn about the crate graph and generally wants all information about all platforms. Locally I have build.target configured in .cargo/config.toml, however, which means that the results of cargo vet are subtly different on my machine. This discrepancy in behavior was the source of the original bug report.

To reproduce this behavior while I'm sure it can be minimized further I can see this with the wasmtime repository:

$ cargo metadata --format-version 1  | shasum
8272634713a68939780da2c8387b5ddcab57dbc7  -
$ cargo metadata --format-version 1 --filter-platform x86_64-unknown-linux-gnu | shasum
4525e3f757ea995e4691188f26b4fc44f7ef6379  -
$ CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu cargo metadata --format-version 1 | shasum
4525e3f757ea995e4691188f26b4fc44f7ef6379  -

Effectively --filter-platform, as expected, changes the output of cargo metadata. The CARGO_BUILD_TARGET then affects this by default so even when --filter-platform isn't passed it produces the same output as-if it were passed.

I'll note that I'm assuming that the behavior of reading CARGO_BUILD_TARGET is intentional, but I also realize that may also not be intentional. If it's not intentional, then that's also a possible fix to this! If it is intentional, then this would boil down to a feature request to have a CLI flag for something like --filter-platform all to explicitly request, regardless of other configuration, that no filter be applied.

@ehuss
Copy link
Contributor

ehuss commented Mar 6, 2025

I'll note that I'm assuming that the behavior of reading CARGO_BUILD_TARGET is intentional, but I also realize that may also not be intentional.

I don't think it was intentional. I believe this regressed in #8167 which changed metadata to call CompileKind::from_requested_targets.

@ehuss ehuss added C-bug Category: bug Command-metadata labels Mar 6, 2025
@alexcrichton
Copy link
Member Author

egad I must atone for my sins

github-merge-queue bot pushed a commit that referenced this issue Mar 6, 2025
This commit fixes a (five year old!) regression in `cargo metadata`
where if `--filter-platform` isn't explicitly specified it will
accidentally read `$CARGO_BUILD_TARGET` (or `build.target`
configuration) and use that as the default `--filter-platform`. The
reason for this is that the calculation for targets changed in #8167 and
while the shared function makes sense for other commands such as `cargo
build` the targets have a different meaning in `cargo metadata` so a
slightly different set of functionality is desired.

This commit fixes the issue by introducing a new constructor for the
list of `CompileKind` variants where the fallback of "if nothing is
specified" is explicitly chosen.

Closes #15270

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants