Skip to content

Poor interaction between workspace dependencies and default-features #12162

Open
@ParkMyCar

Description

@ParkMyCar

Problem

Apologies in advance if this is categorized incorrectly, I can see how this could be either a bug or feature request.

Recently introduced to Cargo is the ability to specify dependencies at the workspace level. Crates within that workspace can specify their dependencies as { workspace = true }, which results in that crate inheriting the dependency from their parent workspace.

With respect to features the inheritance works in a purely additive way, which I believe works well in general, but not in the case of default features.

For example say you have a workspace with two crates, foo and bar, both of these crates depend on tracing. You want to make sure foo and bar depend on the same version of tracing, so you define it in your workspace and have crates inherit from it. In the crate bar you want to disable default-features for tracing, but you cannot since when you inherit from a workspace it's in a purely additive way. The only way to disable default-features in bar is to disable it at the workspace level, this has the adverse effect of also disabling default-features for foo. In the crate foo you could specify default-features = true, but this feels like an anti-pattern.

Scaling this example up a bit and you can better see how this is an issue. Say you have a workspace of 50 crates, 10 of which depend on tracing. In 1 of the 10 crates you want to disable default-features, to do that it requires you to specify default-features = true for the other 9, and any crates in the future that depend on tracing.

Steps

  1. Create a new Rust project, make it a workspace, add a workspace.dependency of tracing = 0.1.37 (or any other crate).
  2. Add a new crate foo, add a dependency of tracing = { workspace = true }.
  3. Add a new crate bar, add a dependency of tracing = { workspace = true, default-features = false }.
  4. Observe a compiler warning when building bar, indicating that the default-features flag is ignored.

Possible Solution(s)

Allow inherited dependencies within a workspace to disable default features.

Notes

No response

Version

$ cargo version --verbose                                                                                                                                                 
cargo 1.69.0 (6e9a83356 2023-04-12)
release: 1.69.0
commit-hash: 6e9a83356b70586d4b77613a6b33f9ea067b9cdf
commit-date: 2023-04-12
host: aarch64-apple-darwin
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0 (sys:0.4.59+curl-7.86.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
os: Mac OS 13.2.0 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-nextArea: may require a breaking change over an editionA-featuresArea: features — conditional compilationA-manifestArea: Cargo.toml issuesA-workspace-inheritanceArea: workspace inheritance RFC 2906C-bugCategory: bugS-needs-infoStatus: Needs more info, such as a reproduction or more background for a feature request.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions