-
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
There is currently no way to specify features automatically when using cargo doc
#8905
Comments
In an attempt to motivate this issue, I'd like to bring up the example of proc macros that add or modify documentation, of the form /// Foobaring a Baz.
#[modify_docs]
fn foobar(baz: Baz) {} or //! Crate docs
#![doc = generate_super_nice_html!()] Or, for a more realistic example, consider the embed-doc-image crate: //! A collection of crustaceans.
//!
//! # Ferris
//! ![Ferris](ferris)
//!
//! Ferris is a crab, as you can see in the above image.
#![doc = embed_image!("ferris", "images/ferris.png")] A bit of a problem with these solutions is that - although the macros shown in the above examples perhaps only modify documentation, they still need to run for normal compilation. This means that:
For example, There is no truly good workaround for these kind of macros at the moment. Sure, you can define a feature that you need to manually enable when calling
If we could define features that should be enabled when |
We talked about this at length in today's @rust-lang/cargo meeting. Copying some text from #10435 to move the discussion here: Currently, Rust has three kinds of dependencies: dependencies, dev-dependencies, and build-dependencies. Adding a fourth kind of dependency would be a substantial increase in complexity. This issue started out talking about using feature flags for this, and in our discussions today we felt like that approach would be much simpler and make more use of existing Cargo facilities. Crates today already use features for this, and then add docs.rs metadata telling docs.rs to use those features. And features can already control dependencies or features of dependencies. We're open to a new mechanism to automatically enable a feature flag on doc builds and doctests, so that Also, separately, this is something we'd consider to be a large feature, for which we'd like to see an RFC design rather than going directly to a PR. Doing design iteration in a PR is much higher bandwidth for us than reviewing an RFC. We're also open to just installing |
The feature approach is unfortunately not sustainable as soon as the crate is a dependency because it then requires for the crate using it as a dependency to handle the feature as well. So we need a way to have dependencies and features specific to a rustdoc run. As for how, I don't have a preference. |
It seems to me that this issue stems from the fact that documentation and standard builds have different sets of desirable default feature sets. For example, a crate might only want to use a minimal set of features for a dependency, but when building documentation for a crate higher in the dependency tree, we still want to have the "proper" documentation for the dependencies. However, currently there is only a single mechanism for specifying default features, with no way to distinguish between standard and doc builds. With that in mind, it seems at first glance to me that something like the following would be ideal: [features]
default = ["my", "default", "features"]
# Document all the default stuff, but also make sure that we document serde impls
doc-default = [ "default", "serde" ]
# If not specified, implicitly use default features, i.e.
doc-default = [ "default" ] I think Instead, we would need a separate Certainly the latter is a breaking change: having doc builds treat |
Having |
What is the advantage of having a doc-default feature, rather than just enabling dev-dependencies for all doc builds? doctests already have to install dev-dependencies, so this would just enable those for docs as well. |
Because I assume you don't need the latex proc-macro when running your tests? |
You do need it to build your docs, which you have to do to as part of As long as they're not pulled in for ordinary builds, I don't think we need to worry much about the weight of dev-dependencies. That doesn't seem worth making complexity tradeoffs, compared to just saying all doc/doctest builds pull in dev-dependencies. |
I don't immediately see how making doc builds use dev dependencies solves the problem here. Consider my motivating example for embed-doc-image: //! ![Ferris](ferris)
#![doc = embed_image!("ferris", "images/ferris.png")] Here we'll need the In addition to this, there may be a compile-time cost associated with invoking the macros as well. For example, with
So even if this would only happen in |
|
@Andlon in your case I think you could do this:
It's a bit ugly but I think that would work with Their is also the possibility to not expand the |
@Urgau: I agree that would be almost ideal if it worked as you would expect (see my above comment, which I slipped in just before you posted yours!).
This suggestion is interesting... I guess someone with knowledge of how this propagates through cargo/rustdoc would have to chime in to say if that is workable. |
You're right, that's weird. Is this a feature ? because I would consider this as a bug.
This would be more of |
This is a bug and the main motivation behind this issue. :) |
Sorry, but I believe this statement might confound this issue somewhat: even if |
You are right on that. This is why I was hoping for |
Yes but coupled with Btw I just locally experimented with some changes to cargo to build the dependencies when documenting with |
Yes, it's as expected and why I think @joshtriplett is suggesting to re-use |
I will also add that I think if |
It's not always an option unfortunately. For example, in |
@GuillaumeGomez: forgive me if I'm being too naive here, but wouldn't |
Okay, so if I take everything in account I propose having And then using [profile.doc]
features = ["feature", "automatically", "activated", "for", "doc"] Even trough I think that this would better suited for something like |
Sorry but I'm the one who's lost: how EDIT: if you're talking about |
@GuillaumeGomez: I think I didn't understand what you meant. But couldn't |
No because we need to do things in Being able to have doc specific features would be a big plus too.
It is when you're running |
Because enabling dev-deps for all dependencies will probably pull in all kinds of stuff that in aggregate is likely to have build issues. |
That does indeed seem like a bug. Unless I'm misunderstanding how |
@joshtriplett The problem with |
Changing
Maybe it would be possible to tie this to an edition change or some other opt-in to the breakage. One other big consideration is that |
To make this a little more precise, You can see the difference when inlining documentation, if you inline documentation from a dependency that does something like |
There was also some prior discussion of a different usecase of something like |
The problem is it does not. Just hit this issue with my [dev-dependencies]
docify = "0.2" //! ### Interned Example
#![cfg_attr(doc, doc = docify::embed_run!("tests/tests.rs", test_interned_showcase))]
Thus users of If we've decided adding |
I thought at first about using
[profile.doc]
, but it is doing nothing (as displayed by cargo itself) and there are no other way to do so.Then I looked a bit deeper and noticed that you can't set features under profiles, making this not the right approach.
Therefore, I suggest adding a new section simply called "rustdoc" which would allow for now:
The text was updated successfully, but these errors were encountered: