Skip to content

cargo metadata: Allow providing cfgs in addition to --filter-platform #15576

@seritools

Description

@seritools

Problem

Note: Split off of (and blocks) this rust-analyzer issue: rust-lang/rust-analyzer#19846

cargo metadata has a --filter-platform flag to return the effective dependency tree for a given platform, evaluating conditional dependencies like [target.'cfg(windows)'.dependencies] and culling dependencies that evaluate to false.

This target dependency syntax supports any cfg condition, though:

[target.'cfg(my_custom_cfg = "foobar")'.dependencies]
bar = { workspace = true }

However, there doesn't seem to be a way to provide additional cfgs for cargo metadata's dependency evaluation outside of setting one of the RUSTFLAGS environment variables.
This means that these conditional dependencies will be filtered out (for example by rust-analyzer's call of cargo metadata, see the linked issue above) as soon as --filter-platform is used.

Proposed Solution

From my limited perspective it seems that cargo metadata is missing a parameter like --filter-cfg that would allow specifying these additional cfgs, bringing them to parity with platform-defined cfgs.
The proposed solution would be to add --filter-cfg with effectively the same syntax as rustc's --cfg.

Notes

If this seems reasonable (or another way that might be) I'd like to give implementing it a try. Any guidance here would be appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`Command-metadataS-triageStatus: This issue is waiting on initial triage.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions