Skip to content

--features is silently ignored when --all-features is present #10333

Closed
@WaffleLapkin

Description

@WaffleLapkin

Problem

When --all-features flag is present, any --features flags are silently ignored.

At first glance that may not seem as a problem, since all features are enabled anyway. But there is a corner case: an [[example]] can depend on a feature of a dependency (required-features = ["dep/something"]). Dependency features are not enabled with --all-features and --features flags are ignored, so it's impossible to build such example with --all-features.

This is especially troublesome when using some external tools (like rust-analyzer) that pass --all-features to cargo.

Steps

  1. Clone the example repository: https://github.com/WaffleLapkin/cargo_all_some_features
  2. try running cargo run --example example --all-features --features "tracing/log"
  3. notice that cargo suggests using --features="aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg tracing/log"
  4. try running cargo run --example example --all-features --features="aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg tracing/log"
  5. notice that it doesn't work either (the suggestion is still the same)

Possible Solution(s)

  1. Do not ignore --features in the presence of --all-features.
    This seems to be the right & easy way to resolve this issue. I'll try to come up with a PR later.
  2. Add a warning or an error when --all-features is used with --features.
    This does not solve my issue, but at least it improves diagnostics.

Notes

With edition = 2021 (instead of edition = 2018) the behaviour is even weirder:

; sed -i 's/2018/2021/g' ./Cargo.toml           
; cargo run --example example --all-features --features="tracing/log" 
error: none of the selected packages contains these features: tracing/log
; cargo run --example example --all-features --features="aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg tracing/log"
error: none of the selected packages contains these features: aaaaaaaa, bbbbbbbb, cccccccc, dddddddd, eeeeeeee, ffffffff, gggggggg, tracing/log
; cargo run --example example --features="aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg tracing/log"               
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/examples/example`

Cargo says that all of the features do not exist, even though they clearly exist.


edition = 2018 though has the oposite problem of allowing clearly non-existing features (by ignoring all of them):

; cargo b --all-features --features "anahsddhjdhjdh definetly_non_existing_feature dep/aaaa"                         
   Compiling tracing v0.1.29
   Compiling cargo_all_some_features v0.1.0 (/home/waffle/projects/repos/cargo_all_some_features)
    Finished dev [unoptimized + debuginfo] target(s) in 0.94s

See also: #10332

Version

cargo 1.60.0-nightly (95bb3c92b 2022-01-18)

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