Skip to content

cargo metadata downloads crate files for unused targets #8981

Closed
@jonhoo

Description

@jonhoo

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

  1. Construct a Cargo.toml with a cfg-guarded dependency:
    [package]
    name = "bug"
    version = "0.1.0"
    edition = "2018"
    
    [target.'cfg(bogus)'.dependencies]
    serde = "1"
  2. Run cargo check, and notice that it does not download serde.
  3. Run cargo metadata --filter-platform x86_64-unknown-linux-gnu, and notice that it does download serde.

Possible Solution(s)
The cause of the bug is here:

// Download all Packages. This is needed to serialize the information
// for every package. In theory this could honor target filtering,
// but that would be somewhat complex.
let package_map: BTreeMap<PackageId, Package> = ws_resolve

As the comment notes, that logic should do target filtering as expressed in PackageSet::download_accessible:

pub fn download_accessible(

Notes

Output of cargo version: cargo 1.48.0 (65cbdd2 2020-10-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions