Skip to content

Weird behavior with target.cfg.dependencies/build-dependencies #9216

@khyperia

Description

@khyperia

For this src/lib.rs, compiling with various configurations produces some weird results. I'm using bitflags as a random example, I don't think there's anything special about it:

use bitflags;
pub fn f() {}

This Cargo.toml fails, as expected (I am on linux, flip to cfg(unix) or whatever if you're on windows):

[package]
name = "rusttest"
version = "0.1.0"
edition = "2018"

[target.'cfg(windows)'.dependencies]
bitflags = ""

# [target.'cfg(not(windows))'.build-dependencies]
# bitflags = ""
error[E0432]: unresolved import `bitflags`
 --> src/lib.rs:1:5
  |
1 | use bitflags;
  |     ^^^^^^^^ no external crate `bitflags`

This Cargo.toml also fails, as expected, with the same error.

[package]
name = "rusttest"
version = "0.1.0"
edition = "2018"

# [target.'cfg(windows)'.dependencies]
# bitflags = ""

[target.'cfg(not(windows))'.build-dependencies]
bitflags = ""

But, this Cargo.toml succeeds compilation, very unexpectedly (with a warning: unused import: bitflags):

[package]
name = "rusttest"
version = "0.1.0"
edition = "2018"

[target.'cfg(windows)'.dependencies]
bitflags = ""

[target.'cfg(not(windows))'.build-dependencies]
bitflags = ""

Is this intended behavior? This seems really weird and not at all what I expected. The first example shows that target.'cfg(windows)'.dependencies is not included in builds on linux. The second example shows that target.'cfg(not(windows))'.build-dependencies (which is enabled on linux) does not "leak" into the main program. So why, when combined together, does the build succeed?

Output of cargo version: cargo 1.50.0 (f04e7fab7 2021-02-04)
Also reproduces on cargo +nightly version: cargo 1.52.0-nightly (572e20153 2021-02-24)


Discovered while trying to investigate the issues hit in EmbarkStudios/rust-gpu#423

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dependency-resolutionArea: dependency resolution and the resolverC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions