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

Allow enabling a dependency if a feature is *off* #14188

Closed
Kixunil opened this issue Jul 4, 2024 · 2 comments
Closed

Allow enabling a dependency if a feature is *off* #14188

Kixunil opened this issue Jul 4, 2024 · 2 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.

Comments

@Kixunil
Copy link

Kixunil commented Jul 4, 2024

Problem

Disclaimer: this looks like "mutually exclusive features" but it's not really the same thing. Dependencies are not mutually exclusive.

This situation came up a few times in my experience. Let's say there are three crates: consumer, powerful and minimal and the latter two provide similar functionality except powerful is much bigger and may even involve FFI (written in C). consumer would like to depend on either of them and provide unified API that's just extended by depending on powerful, so users who don't need extra features would not incur the cost of compiling powerful and users who depend on powerful would not incur the cost of minimal. Some practical examples:

  • There's a PR against the secp256k1 crate to add partial pure-Rust implementation. This implementation needs a u256 type which happens to be the same code as one of the downstream dependencies uses already. Ideally, it'd be a shared crate but that would force compilation of u256 even when the C implementation is used for all other crates.
  • Libraries vendoring their C code and simultaneously providing bindings to the system libraries need cc crate if they're compiling the bindings but don't really need it if they're just linking.
  • no_std crates requiring equivalent of std::io traits but wanting to work with std traits too. E.g. using the core2 crate.

All current solutions to this have their problems:

  • Make both optional and have compile_error! if both are off. I've seen this done for core2 it was super horrible to work with in library dependencies because they had to propagate this thing downwards, they were annoying to test and if someone accidentally enabled both then both would get compiled without any warning. (And in some edge cases it even caused breakage.)
  • Enable minimal unconditionally, optionally have powerful - wastes compile time
  • Inline minimal into consumer as a module and conditionally disable the module. Saves compile time in some cases but makes it worse in others. Also forces the maintainers to manually sync the versions.

Proposed Solution

Allow specifying this:

[package]
namespaced-features = true

[features]
extra = ["crate:powerful"]
"!extra" = ["crate:minimal"]

[dependencies]
minimal = { version = "1.42.0", optional = true }
powerful = { version = "1.21.0", optional = true }

When the feature extra is disabled the powerful dependency is activated. When it's disabled, minimal is activated. Activating both minimal and powerful via other means is allowed in which case both dependencies get included. Perhaps this could issue a warning though.

Notes

No response

@Kixunil Kixunil added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Jul 4, 2024
@epage
Copy link
Contributor

epage commented Jul 4, 2024

Looks like this is a duplicate of #1839 and closing in favor of that.

If there is reason to keep this open separately, let us know!

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
@Kixunil
Copy link
Author

Kixunil commented Jul 4, 2024

Thanks, I was trying to find it but didn't manage to come up with a good search term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants