Skip to content
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

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

Closed
khyperia opened this issue Mar 1, 2021 · 3 comments · Fixed by #9255
Closed

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

khyperia opened this issue Mar 1, 2021 · 3 comments · Fixed by #9255
Labels
A-dependency-resolution Area: dependency resolution and the resolver C-bug Category: bug

Comments

@khyperia
Copy link
Contributor

khyperia commented Mar 1, 2021

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

@khyperia khyperia added the C-bug Category: bug label Mar 1, 2021
@khyperia
Copy link
Contributor Author

khyperia commented Mar 1, 2021

@eddyb suggested adding resolver = "2" to the Cargo.toml:

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

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

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

and uh,

khyperia@cobalt ~/me/rusttest> cargo +nightly build
thread 'main' panicked at 'activated_features for invalid package: features did not find PackageId { name: "bitflags", version: "1.2.1", source: "registry `https://github.com/rust-lang/crates.io-index`" } false', src/tools/cargo/src/cargo/core/resolver/features.rs:235:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Backtrace, click to expand
khyperia@cobalt ~/me/rusttest> RUST_BACKTRACE=full cargo +nightly build
thread 'main' panicked at 'activated_features for invalid package: features did not find PackageId { name: "bitflags", version: "1.2.1", source: "registry `https://github.com/rust-lang/crates.io-index`" } false

Stack backtrace:
   0: cargo::core::resolver::features::ResolvedFeatures::activated_features_int
   1: cargo::core::compiler::unit_dependencies::new_unit_dep_with_profile
   2: cargo::core::compiler::unit_dependencies::compute_deps
   3: cargo::core::compiler::unit_dependencies::deps_of
   4: cargo::core::compiler::unit_dependencies::build_unit_dependencies
   5: cargo::ops::cargo_compile::create_bcx
   6: cargo::ops::cargo_compile::compile_ws
   7: cargo::ops::cargo_compile::compile
   8: cargo::commands::build::exec
   9: cargo::cli::main
  10: cargo::main
  11: std::sys_common::backtrace::__rust_begin_short_backtrace
  12: std::rt::lang_start::{{closure}}
  13: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/core/src/ops/function.rs:259:13
      std::panicking::try::do_call
             at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:379:40
      std::panicking::try
             at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:343:19
      std::panic::catch_unwind
             at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panic.rs:431:14
      std::rt::lang_start_internal
             at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/rt.rs:51:25
  14: main
  15: __libc_start_main
  16: <unknown>', src/tools/cargo/src/cargo/core/resolver/features.rs:235:14
stack backtrace:
   0:     0x55dd1e2ad400 - std::backtrace_rs::backtrace::libunwind::trace::h9d49145f95eb5894
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x55dd1e2ad400 - std::backtrace_rs::backtrace::trace_unsynchronized::hab1d020365bb6864
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55dd1e2ad400 - std::sys_common::backtrace::_print_fmt::h7659588431e304bd
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x55dd1e2ad400 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h09f4a9e3befae3c7
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x55dd1e2d459c - core::fmt::write::hf3fdfde304b9a088
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/core/src/fmt/mod.rs:1092:17
   5:     0x55dd1e2a47d2 - std::io::Write::write_fmt::h1cb850689c7116f0
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/io/mod.rs:1568:15
   6:     0x55dd1e2b0355 - std::sys_common::backtrace::_print::hdbccd5aa093ba544
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x55dd1e2b0355 - std::sys_common::backtrace::print::hc639c4f320222558
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x55dd1e2b0355 - std::panicking::default_hook::{{closure}}::hdb012dd7a485bb5d
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:208:50
   9:     0x55dd1e2afe03 - std::panicking::default_hook::h75facbce77b6ba91
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:225:9
  10:     0x55dd1e2b0af1 - std::panicking::rust_panic_with_hook::hbcaa5de2cb5e22d5
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:591:17
  11:     0x55dd1e2b0637 - std::panicking::begin_panic_handler::{{closure}}::h4ee6cde415c8f62d
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:497:13
  12:     0x55dd1e2ad8bc - std::sys_common::backtrace::__rust_end_short_backtrace::h895319f2d3f611c0
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/sys_common/backtrace.rs:141:18
  13:     0x55dd1e2b0599 - rust_begin_unwind
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:493:5
  14:     0x55dd1e2d2a91 - core::panicking::panic_fmt::h0123abb763a6e96f
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/core/src/panicking.rs:92:14
  15:     0x55dd1e2d28b3 - core::option::expect_none_failed::h9fff90ea7603aa1d
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/core/src/option.rs:1300:5
  16:     0x55dd1da11f8f - cargo::core::compiler::unit_dependencies::new_unit_dep_with_profile::h5964cde2c2abca55
  17:     0x55dd1da1095f - cargo::core::compiler::unit_dependencies::compute_deps::h69e985c7bbf4d1bc
  18:     0x55dd1da0f303 - cargo::core::compiler::unit_dependencies::deps_of::h123cd8ebc552c83b
  19:     0x55dd1da0dbc1 - cargo::core::compiler::unit_dependencies::build_unit_dependencies::h83836656f1b0fb95
  20:     0x55dd1dc0b2ad - cargo::ops::cargo_compile::create_bcx::h78fae7bd213ba8be
  21:     0x55dd1dc06506 - cargo::ops::cargo_compile::compile_ws::h4fd239b9a12b765b
  22:     0x55dd1dc0641e - cargo::ops::cargo_compile::compile::hc792daa77ab07f92
  23:     0x55dd1d96ad82 - cargo::commands::build::exec::h1f89262783db7921
  24:     0x55dd1d98616b - cargo::cli::main::h0cbd726e1a4a310b
  25:     0x55dd1d97e7d8 - cargo::main::h3ab2228c9b96dd76
  26:     0x55dd1d9764b6 - std::sys_common::backtrace::__rust_begin_short_backtrace::h3f798627fa82d4dd
  27:     0x55dd1d96665c - std::rt::lang_start::{{closure}}::h571364af7911c191
  28:     0x55dd1e2b0ffa - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h31ee16c075ae2d62
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/core/src/ops/function.rs:259:13
  29:     0x55dd1e2b0ffa - std::panicking::try::do_call::h3993344bf97066b6
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:379:40
  30:     0x55dd1e2b0ffa - std::panicking::try::h09e523df7c9c2d28
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panicking.rs:343:19
  31:     0x55dd1e2b0ffa - std::panic::catch_unwind::h9d4ddb10ebb815ff
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/panic.rs:431:14
  32:     0x55dd1e2b0ffa - std::rt::lang_start_internal::hc92e27a69d75de2a
                               at /rustc/e37a13cc3594004663738bd18d8100e6db9666cf/library/std/src/rt.rs:51:25
  33:     0x55dd1d980b25 - main
  34:     0x7fe6bfc2ab25 - __libc_start_main
  35:     0x55dd1d916071 - <unknown>

@ehuss ehuss added the A-dependency-resolution Area: dependency resolution and the resolver label Mar 1, 2021
@eddyb
Copy link
Member

eddyb commented Mar 11, 2021

@ehuss @alexcrichton resolver = "2" is in beta, right? Seems like this might be important to prioritize for an investigation/fix before it gets into stable - but I can't any labels myself to nominate it or anything.

@ehuss
Copy link
Contributor

ehuss commented Mar 11, 2021

This was an unintended regression introduced by #8777 (first released in 1.49). I can try to take a look.

@bors bors closed this as completed in c4adfd5 Mar 11, 2021
ehuss pushed a commit to ehuss/cargo that referenced this issue Mar 12, 2021
Fix issue with filtering exclusive target dependencies.

rust-lang#8777 incorrectly changed the filtering logic for dependencies.  Essentially it split `filter(any(A && B && C && D))` into two parts `filter(any(A && B)).filter(any(C && D))` which doesn't have the same meaning. The solution here is to pass a closure so that the conditions are joined again.

Fixes rust-lang#9216
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dependency-resolution Area: dependency resolution and the resolver C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants