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

Automatically activate a dependency if its used elsewhere in dependency graph #1555

Open
cgaebel opened this issue Apr 28, 2015 · 10 comments
Open
Labels
A-crate-dependencies Area: [dependencies] of any kind A-features Area: features — conditional compilation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@cgaebel
Copy link

cgaebel commented Apr 28, 2015

Cargo should be able to express the idea of: "I want to depend on this package iff whoever depends on me is already depending on that package." This is especially useful because rust doesn't have orphan instances.

Specifically, I want to add a quickcheck Arbitrary instance to Iobuf. However, I don't want to add the dependency on Quickcheck for people not using it (non-test executables, for most people). It would be nice if it "just worked", perhaps in a new section:

[optional-dependencies]
quickcheck = "*"

And then anyone that depends on iobuf can have the quickcheck instances built for tests by including quickcheck in their dev-dependencies.

@alexcrichton
Copy link
Member

Are you familiar with inline table syntax for TOML? I think this is much more ergonomic than a new section:

[dependencies]
quickcheck = { optional = true }

@cgaebel
Copy link
Author

cgaebel commented Apr 28, 2015

I didn't know about that syntax, but it sounds good to me. I care much more about the "magic, just-works" behavior than the syntax. :)

@alexcrichton
Copy link
Member

Ok, in that case I'm going to close this as the inline table syntax should be much more ergonomic than a whole new section.

@cgaebel
Copy link
Author

cgaebel commented Apr 28, 2015

Wait, this feature exists already? How do I express "I want to depend on this package iff whoever depends on me is already depending on that package." ?

@alexcrichton
Copy link
Member

Ah I believe I have misread.

@alexcrichton alexcrichton reopened this Apr 29, 2015
@erickt
Copy link
Contributor

erickt commented Jan 25, 2016

While this doesn't happen automatically, I believe this can currently be expressed with:

crate foo:

[dependencies]
quickcheck = { version = "*", optional = true }

crate bar:

[features]
with-quickcheck = ["quickcheck", "foo/quickcheck"]

[dependencies]
foo = "*"
quickcheck { version = "*", optional = true }

Then for the downstream, if they're using quickcheck, they can opt into bar using quickcheck via:

[features]
with-quickcheck = ["quickcheck", "bar/with-quickcheck"]

[dependencies]
bar = "*"
quickcheck = { version = "*", optional = true }

@Storyyeller
Copy link

Storyyeller commented Feb 6, 2017

I think this would be very useful. When writing a library that defines a new type, it often makes sense to impl traits from other popular libraries for interoperability. But I don't want to pull in all those extra dependencies if the user isn't using them.

@mehcode
Copy link

mehcode commented Feb 13, 2017

This would be awesome. 💯

[dependencies]
diesel = { version = "0.10.0", features = ["postgres", "uuid"] }
diesel_codegen = { version = "0.10.0", features = ["postgres", "dotenv"] }
uuid = { version = "0.4.0", features = ["serde"] }
serde = "0.9"
dotenv = "*"

Wouldn't it be simply beautiful if the above could instead be:

[dependencies]
diesel = { version = "0.10.0", features = ["postgres"] }
diesel_codegen = { version = "0.10.0", features = ["postgres"] }
uuid = "0.4.0"
serde = "0.9"
dotenv = "*"

Automatic use of features (the optional crate kind) that I have as explicit dependencies would be a very ergonomic addition.

@alexcrichton
Copy link
Member

I believe this feature has an associated RFC now: rust-lang/rfcs#1787

@carols10cents carols10cents added A-crate-dependencies Area: [dependencies] of any kind A-features Area: features — conditional compilation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` labels Sep 10, 2017
@epage epage changed the title Make Optional Dependencies More Ergonomic Automatic feature activation Apr 21, 2022
@epage epage changed the title Automatic feature activation Automatically activate a dependency if its used elsewhere in dependency graph May 24, 2023
@epage epage added the S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. label May 24, 2023
@epage
Copy link
Contributor

epage commented Sep 25, 2023

I've created a Pre-RFC for mutually-exclusive, global features which might be pertinent to the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-crate-dependencies Area: [dependencies] of any kind A-features Area: features — conditional compilation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

7 participants