5
votes

I'm working on a CLI app that uses reqwest and self_update. self_update also uses reqwest. I want my app to use rustls and not pull in openssl dependencies. Cargo.toml allows choosing features of dependencies:

[dependencies.reqwest]
version = "0.10"
default-features = false
features = ["rustls-tls", "json", "blocking"]

It would be cool if sub-dependencies worked:

[dependencies.self_update.reqwest]
version = "0.10"
default-features = false
features = ["rustls-tls", "json", "blocking"]

I also looked at replace section, but only something like this works where I branch the code:

"reqwest:0.10.1" = { branch = "rustls", git = "https://github.com/ctaggart/reqwest" }

But what I want is default-features and features supported too:

"reqwest:0.10.1" = { tag="v0.10.1", git = "https://github.com/seanmonstar/reqwest", default-features = false, features = ["rustls-tls", "json", "blocking"] }

How do I configure the features of Reqwest or Tokio or any other highly configurable non-direct dependency using Cargo?

1
Features are meant to be additive. If you enable a feature in your own dependencies, it's also available in your sub-dependencies. You won't get two copies of reqwest in your application with and without that feature.mcarton
Ok, but how do I remove a default feature? How do I prevent self_update from pulling in the openssl dependency, which is a default feature of reqwest?Cameron Taggart
self_update itself must have a feature do that. Cargo can't know that it's safe to remove a feature from a sub-dependency because your dependencies might actually use that feature.mcarton
I thought about adding a rustls feature to self_update, but cargo does not support different dependencies based on features. github.com/rust-lang/cargo/issues/5954 I think it would be a weird design to have to bubble up every optional feature to the consuming library.Cameron Taggart

1 Answers

1
votes

I agree with you, having support for sub-dependencies features would be great.

It's not ideal but if you add your sub-dependency as a dependency the feature is going to work.

[dependencies]
...
surf = "2.1.0" # which depends on http-client, which depends on isahc
...

[dependencies.isahc]
features = ["static-ssl"]

If you don't specify a version you'll get a deprecation warning, but at least you won't have to keep track of the sub-dependency version manually:

warning: dependency (isahc) specified without providing a local path, Git repository, or version to use. This will be considered an error in future versions