Using crate specified as optional dependency of a dependency

Hi there!

I'm trying to use the google-drive3 crate which lists yup-oauth2 as an optional dependency. I want to get support for oauth2 so I enabled the feature in my Cargo.toml:

version = "1.0.2"
features = ["yup-oauth2"]

Then I tried to use some features of yup-oauth2 but I got the following error:

4 | extern crate yup_oauth2 as oauth2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

I probably want to use the same version that google-drive3 expects, so I resorted to adding the following to my dependencies section:

yup-oauth2 = "*"

The hope is that the google-drive3 crate forces the right version of the yup-oauth2 package. Is that the right way to do it?


You should specify the version of the crate you want to use.

Since whatever objects I create using yup-oauth2 are eventually going to be passed to google-drive3, don't I want the versions to match?

The problem is you still won't be guaranteed they will match, your crate will get the latest version of yup-oauth2 while google-drive3 will get whatever version it specifies, if they are different versions you'll get multiple copies of yup-oauth2 built and have compile time errors when trying to pass structs from your version into google-drive3's methods.

There's a long standing issue about having cargo distinguish between public and private dependencies, which would hopefully improve cases like this. But for now the best thing to do is to just use the same version specifier for yup-oauth2 that google-drive3 uses, and update it when you update google-drive3 (proper semver should ensure you only need to update the yup-oauth2 version specifier on a breaking change of google-drive3).

1 Like

For some reason I thought * would mean "whatever version is available" which in my case would be the one pulled by google-drive3 (somewhat similar to any in Dart's pub spec file).

Thanks for the explanation, I've updated my code accordingly.