Description
Problem
cargo metadata
(and cargo tree
, possibly also others) downloads the .crate
files for dependencies that are not used under the current cfg
parameters. This means that it will, for example, download windows-only dependencies even if run on a UNIX platform. cargo build
and friends do not do this. This occurs even with --filter-platform
/--target
is passed to filter for a particular target (I understand cargo metadata
is supposed to cover all targets unless explicitly otherwise specified).
Steps
- Construct a
Cargo.toml
with acfg
-guarded dependency:[package] name = "bug" version = "0.1.0" edition = "2018" [target.'cfg(bogus)'.dependencies] serde = "1"
- Run
cargo check
, and notice that it does not downloadserde
. - Run
cargo metadata --filter-platform x86_64-unknown-linux-gnu
, and notice that it does downloadserde
.
Possible Solution(s)
The cause of the bug is here:
cargo/src/cargo/ops/cargo_output_metadata.rs
Lines 133 to 136 in de42302
As the comment notes, that logic should do target filtering as expressed in PackageSet::download_accessible
:
cargo/src/cargo/core/package.rs
Line 485 in 862df61
Notes
Output of cargo version
: cargo 1.48.0 (65cbdd2 2020-10-14)