Skip to content

cargo test --all-features at the workspace level doesn't behave as I would expect #15219

@tdelabro

Description

@tdelabro

Problem

Let's say I have a request with two crates, a lib and a bin.

Cargo.toml

[workspace]
resolver = "2"
members = [
  "my_lib", "my_bin"
]

[workspace.dependencies]
my_lib = { path = "./my_lib" }

my_lib/Cargo.toml

[package]
name = "my_lib"
version = "0.1.0"
edition = "2021"

[dependencies]

[features]
default = []
feature_b = []

my_lib/src/lib.rs

pub struct MyStruct {
    pub a: u64,
    #[cfg(feature = "feature_b")]
    pub b: u64,
}

my_bin/Cargo.toml

[package]
name = "my_bin"
version = "0.1.0"
edition = "2021"


[dependencies]
my_lib = { workspace = true }

my_bin/src/main.rs

use my_lib::MyStruct;

fn main() {
    let my_struct = MyStruct { a: 42 };
    println!("{}", my_struct.a);
}

Running cargo check --all-features at the workspace level I get:

cargo check --all-features
    Checking my_lib v0.1.0 (/private/tmp/wrspc/my_lib)
    Checking my_bin v0.1.0 (/private/tmp/wrspc/my_bin)
error[E0063]: missing field `b` in initializer of `MyStruct`
 --> my_bin/src/main.rs:4:21
  |
4 |     let my_struct = MyStruct { a: 42 };
  |                     ^^^^^^^^ missing `b`

For more information about this error, try `rustc --explain E0063`.
error: could not compile `my_bin` (bin "my_bin") due to 1 previous error

Meaning it's compiling the my_bin using my_lib with feature.feature_b enabled.

To me it seems like an error, as the my_bin crate doesn't ever specify the use of the feature_b feature, which is not enabled by default in my_lib.

Proposed Solution

The behavior I would expect is:

  • build my_lib with all ITS features enabled (here, feature_b)
  • build my_bin with all ITS features enabled (here, none) but for it's dependencies, only add the features specified, not all existing

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-featuresArea: features — conditional compilationC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions