-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Warn or error when using mandatory dependencies as a feature #4363
Comments
Hm this is surprising! Definitely sounds like a bug! |
So I wrote these testcases demonstrating the issue, both of which currently fail (cargo tries to build the package): However, I have a hard time wrapping my head around the features code trying to fix this.^^ |
In particular, something is wrong with the comments here it seems:
Above the loop it says something about whitelisting optional features, but then what it actually does it go over all enabled (required and optional) dependencies and remove them from feature_deps. |
In fact, the existing data structures seem unsuited to even detect this case: If The fact that |
There are valid use cases for using these kinds of features. You may happen to use a crate internally, and also want to offer e.g. trait implementations for its types optionally. Unconditionally implementing the traits locks you into depending on that library, while putting the public trait implementations behind a feature gives you more flexibility in the future. |
@sfackler Not sure what you are saying. I never doubted that |
I am saying there are times where you do want to use features named after required dependencies. |
I see. I somehow forgot about the
So it seems fairly clear that such features were not intended to work. The trouble with allowing such features (which is what lead me to discover this bug) is that usually, if you enable a feature that's called like a dependency, that actually makes a difference. So I thought by setting that feature, I accomplished something -- but actually, I did not. That's just like enabling a feature that does not exist at all. Unfortunately, there is no way for cargo to know whether some particular required dependency is used as a feature or not. (TBH, I think features and optional dependencies should be disjoint concepts. Allowing others to enable random combinations of optional dependencies in my crate seems scary, I'd rather carefully control which features flags are actually available. If that was the case, features and dependencies could have the same name, and we wouldn't have any of these problems. But oh well, there's no way changing that now.) Note that there is no fundamental reason why the feature has to be called like the required dependency. The crate author could pick any name. One way to fix all of this may be to actually allow features that have the same name as a dependency, and specify them as adding additional things that are enabled (beyond just enablng the dependency, in case it is an optional one) when the feature is set. |
Yeah, making feature names disjoint from dependency names would be great. |
Required dependencies are not features Also, while I was at it, I fixed an error message which complained about something not being an optional dependency, when really what mattered was that it was not a dependency at all. I made a bunch of guesses about how things work. These guesses ended up as comments in the commit (so hopefully, the next reader of these files has to guess less). I am not totally certain these comments are all correct, so please yell if not. :) In particular, for resolve_features, I observed that dependencies get compiled even when they are not returned from that function. But judging from how the function used to behave, it actually returns all dependencies, even those that have nothing to do with any features. (Making the name rather misleading, TBH...) Fixes #4363
Currently, Cargo warns when I ask for a feature of another crate that does not exist. However, it seems that it treats all dependencies also as features. For optional dependencies, this makes sense, and indeed the documentation says "A feature of a package is either an optional dependency, or a set of other features."
So, I can depend on
regex = { version = "0.2.2", features = ["memchr"]}
and there is no warning or error that this (a) doesn't make any sense, and (b)memchr
is not technically a feature according to the definition of the docs (and indeed, it should not be).The text was updated successfully, but these errors were encountered: