-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
cargo doc: include all available features in generated docs. #8103
Comments
cargo doc
output.
As a workaround for now to include feature-specific items in docs you can use doc_cfg feature. Example: #[cfg(any(feature = "foo", doc))]
#[doc(cfg(feature = "foo"))]
pub mod foo; EDIT: Instead of enabling |
95fb4e0 Document cargo features (Martin Habovstiak) Pull request description: This documents cargo features in two ways: explictly in text and in code using `#[doc(cfg(...))]` attribute where possible. Notably, this is impossible for `serde` derives. The attribute is contitional and only activated for docs.rs or explicit local builds. This change also adds `package.metadata.docs.rs` field to `Cargo.toml` which instructs docs.rs to build with relevant features and with `docsrs` config activated enabling `#[doc(cfg(...))] attributes. I also took the opportunity to fix a few missing spaces in nearby code. Notes for reviewers: * To build and look at result locally run: `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --features std,secp-recovery,base64,rand,use-serde,bitcoinconsensus --open` (don't confuse `RUSTDOCFLAGS` with `RUSTFLAGS` - took me a while to figure that out 😞) * You should see needed features being called out - e.g. in `Script::verify` * More information and some screenshots: rust-lang/cargo#8103 (comment) * For an example why this matters see: Kixunil/loptos#1 😉 * Serde issue: serde-rs/serde#2063 ACKs for top commit: dr-orlovsky: ACK 95fb4e0 apoelstra: ACK 95fb4e0 Tree-SHA512: 4d6428bfa05cbeb2d8737f0239aa1836b5f36f4b040e1ac5e0862663c4ea783711d122182dd8313913fd593ab224915bd8def5e268b272215bee2c9856a27674
@nrabulinski I'm not understanding how to use this. I am trying to run this:
Where this is on one of the fields in my struct #[cfg_attr(docsrs, doc(cfg(feature = "unknown-fields")))]
#[cfg(feature = "unknown-fields")]
#[serde(flatten)]
pub other_fields: serde_json::Value, My ...
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
...
[features]
#default = ["unknown-fields"]
unknown-fields = [] The output I see is this:
|
@spikespaz I'd recommend checkout out argfile as its a fairly minimal crate that show cases that feature. Things to note:
|
I believe this issue is a duplicate of rust-lang/rust#96318, so I'm closing in favor of that. If there is something I missed in that analysis, feel free to speak up! |
In addition to the Those two pieces basically fulfill the spirit of my original post with this ticket! Love to see the ecosystem evolving like this. Now my only desire is that it could all work more seamlessly out of the box, but we'll get there eventually. ;-) |
Describe the problem you are trying to solve
Problem: I can't find the feature-gated API's I need from dependencies by viewing the local
cargo doc
docs. Instead I have to either do online searches or go view~/.cargo/registry/src/…/<CRATE>-<VERSION>/Cargo.toml
and even then sometimes guess which features are relevant.Describe the solution you'd like
Minimum Viable Solution:
When I open the crate-level docs using
cargo doc --open
, I would like it to have a section called "Features" on the top-level page, or linked from the side-bar to the left. That section lists the features that are currently enabled and all of the available disabled features, with clear markings for which state each feature is in: enabled or disabled.Ideal Solution:
In addition to the Minimum Viable Solution, the ideal doc improvement would:
Include in every module page all APIs gated behind disabled features, where it explicitly says for each one "this fn/struct/enum/trait is only available when the FEATURE is enabled. It is currently disabled." To avoid clutter all disabled APIs could be separated out into the bottom of module doc page in a "disabled features" section, or collapsed by default, or something like that.
Provide a page (or section) for every feature that links to every item that feature enables. Cross link everything. In the item documentation, always explicitly name any features that affect its availability (whether enabled or not) with cross links to the feature page.
Notes
Some dependencies, especially large frameworks, gate many APIs behind feature flags. For example I've had this issue with
tokio
andwasm-bindgen
. It's difficult to discover feature-gated APIs, especially because without explicit doc strings from the author, there's no indication which APIs are feature gated.Motivation for this feature request based on my work flow; I often have this flow:
cargo doc --open
,Also:
S
hotkey.cargo doc
are always immediately relevant, whereas sometimes google searches include irrelevant results, ads, etc. Finally, offline searches are almost always faster, which helps speed up the study/write/review cycle.The text was updated successfully, but these errors were encountered: