Sub topic 14
Documentation in CI and on docs.rs
This chapter assumes you can write doc comments and doc tests, and that you have seen missing_docs and rustdoc lints. Here you will run those checks automatically and see how your docs appear on docs.rs.
What CI should check
Doc problems are easy to miss locally. Three checks catch most of them:
cargo test --docruns every doc test, so stale examples fail.cargo doc --no-depsbuilds the HTML. Rustdoc lints such as broken intra-doc links appear as warnings.- Setting
RUSTDOCFLAGS="-D warnings"turns those warnings into errors, so the build fails.
The -D warnings flag is what makes the check enforce anything. Without it, a broken link prints a warning and CI stays green.
A GitHub Actions workflow
This workflow runs both checks. Adapt it to your CI system. The commands are what matter.
name: docs
on:
push:
branches: [main]
pull_request:
env:
RUSTDOCFLAGS: "-D warnings"
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Doc tests
run: cargo test --doc --all-features
- name: Build docs
run: cargo doc --no-deps --all-featuresUse --no-deps so that warnings from dependencies do not fail your build, and so the build is faster. Use --all-features so that feature-gated items are documented and tested too.
Enforcing missing documentation
Pair CI with the lints from the previous chapter. Put this at the top of lib.rs:
#![warn(missing_docs)]
#![warn(rustdoc::broken_intra_doc_links)]Locally these are only warnings, so they do not interrupt your work. In CI, RUSTDOCFLAGS="-D warnings" upgrades the rustdoc lints to errors. missing_docs is a compiler lint, not a rustdoc lint, so it needs RUSTFLAGS="-D warnings" or cargo clippy -- -D warnings to fail the build. A common mistake is to set only RUSTDOCFLAGS and wonder why undocumented items still pass.
How docs.rs builds your crate
When you publish to crates.io, docs.rs builds your documentation automatically. It uses a nightly toolchain, runs in a sandbox without network access, and builds with the default features only. It does not run your doc tests. It only builds the HTML.
You can change how it builds with metadata in Cargo.toml:
[package]
name = "mycrate"
version = "0.1.0"
edition = "2021"
[features]
serde = []
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]docs.rs passes --cfg docsrs to rustdoc, and the rustdoc-args line above does the same when you build locally with a matching command. You can use that flag to show which feature an item needs. These attributes work on nightly only, so they are enabled only when docsrs is set:
#![cfg_attr(docsrs, feature(doc_cfg))]
/// Serializes a value. Requires the `serde` feature.
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub fn to_json(n: i32) -> String {
n.to_string()
}Stable builds ignore the cfg_attr lines, so your crate still compiles on stable.
Checking the docs.rs build locally
To catch docs.rs-only problems before publishing, build with the same flag on nightly:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-featuresExercise
Your CI runs cargo doc --no-deps and passes, but a doc comment contains a broken intra-doc link. What is the likely cause, and what is the fix?
Show answer
Rustdoc reports a broken intra-doc link as a warning, and warnings do not fail the build. Set RUSTDOCFLAGS="-D warnings" for the doc build step so the warning becomes an error.