Sub topic 14
Documentation in CI and on docs.rs
This chapter assumes you can write /// and //! comments, write doc tests, and use intra-doc links. It also assumes you know the missing_docs lint and have run cargo doc and cargo test --doc locally. Here you move those checks into continuous integration (CI) so broken docs fail a build. You then see how docs.rs builds your crate after you publish it.
What CI should check
Docs can break in three ways, and each needs its own check.
| Failure | Example | Check |
|---|---|---|
| An example no longer compiles or its assertion fails | A function signature changed | `cargo test --doc` |
| A link or markup problem | `[`Parser`]` points to a renamed type | `cargo doc` with warnings denied |
| An item has no documentation | A new `pub fn` was added without a comment | `missing_docs` lint |
Rustdoc reports broken intra-doc links as warnings by default, so they never stop a build. CI turns those warnings into errors.
Denying documentation warnings
Rustdoc reads extra flags from the RUSTDOCFLAGS environment variable. Passing -D warnings turns every rustdoc warning into an error, including broken intra-doc links, unclosed HTML tags, and invalid code block attributes.
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-featuresUse --no-deps in CI. Without it, Cargo also documents every dependency, which is slow and can fail on warnings in code you don't control.
You can deny a single lint instead of all warnings:
RUSTDOCFLAGS="-D rustdoc::broken_intra_doc_links" cargo doc --no-depsRUSTDOCFLAGS also applies to the rustdoc runs that compile doc tests. That means -D warnings can fail a doc test because of an unused variable in an example. Fix the example, or add #![allow(unused)] as a hidden line. Rustdoc already wraps each example in #![allow(unused)] by default, so this is rare.
You can also enforce the lints in the crate itself so every developer sees them, not only CI:
//! Small utilities for working with numbers.
#![warn(missing_docs)]
#![warn(rustdoc::broken_intra_doc_links)]
#![warn(rustdoc::private_intra_doc_links)]
/// Adds two numbers.
pub fn add(a: i32, b: i32) -> i32 {
a + b
}With #![warn(...)] in the source, local builds show warnings and CI makes them fatal through -D warnings. If you wrote #![deny(missing_docs)] instead, a missing comment would break every local build, including work in progress. Prefer warn in the source and deny in CI.
A complete workflow
This GitHub Actions workflow runs all three checks. The same commands work in any CI system.
name: docs
on:
push:
branches: [main]
pull_request:
env:
RUSTFLAGS: "-D warnings"
RUSTDOCFLAGS: "-D warnings"
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Doc tests
run: cargo test --doc --all-features
- name: Build docs
run: cargo doc --no-deps --all-features
- name: Missing docs lint
run: cargo clippy --all-features -- -D missing_docsSome points about this workflow:
cargo test --doconly works on library targets. A binary-only crate has no doc tests to run. Put the logic in alib.rsif you want them.- If your main test job uses
cargo nextest, it will not run doc tests. Nextest does not support them, so keep the separatecargo test --docstep. --all-featuresmakes the doc build and doc tests cover feature-gated code. If your features conflict with each other, run a job per feature set instead.- Cache the build with an action such as
Swatinem/rust-cacheif the job is slow. Doc tests compile one program per example, so they dominate the time in crates with many examples.
Checking docs.rs conditions locally
docs.rs is the public documentation host for crates published on crates.io. When you run cargo publish, docs.rs queues a build and hosts the result at https://docs.rs/<crate>/<version>. Every published version keeps its own documentation.
The build differs from your CI in several ways:
- It uses a nightly toolchain, so unstable rustdoc features are available there.
- It has no network access while building. A
build.rsthat downloads files will fail. - It builds in a sandbox with limited time and memory.
- It builds for one default target,
x86_64-unknown-linux-gnu, unless you list others. - It does not run doc tests. It only builds HTML. Your CI is the only place where examples get executed.
By default, docs.rs builds with the crate's default features only. If most of your API sits behind optional features, the public docs would look incomplete. The [package.metadata.docs.rs] table in Cargo.toml changes this:
[package]
name = "mycrate"
version = "0.1.0"
edition = "2021"
[features]
json = []
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
targets = ["x86_64-unknown-linux-gnu"]
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(docsrs)'] }Here all-features = true documents every feature. The docsrs cfg is a conventional flag that only docs.rs builds set. The [lints.rust] entry tells the compiler that docsrs is an expected cfg name. Without it, recent compilers warn about an unknown cfg.
Showing feature requirements
With docsrs set, you can enable a nightly rustdoc feature that labels feature-gated items in the HTML, such as "Available on crate feature json only."
//! Small utilities for working with numbers.
#![cfg_attr(docsrs, feature(doc_cfg))]
/// Renders a number as a JSON string.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn to_json(n: i32) -> String {
n.to_string()
}cfg_attr(docsrs, ...) applies the attribute only when docsrs is set. On stable Rust, docsrs is not set, so the nightly-only feature(doc_cfg) is never seen and the crate still compiles.
To check this setup before publishing, use the same flags docs.rs uses:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features --openAdd this as a CI job on the nightly toolchain. A failure there means the docs.rs build would fail too.
Failure modes to watch for
- Doc build passes locally but fails on docs.rs. The usual causes are a
build.rsthat needs the network, a feature that needs a system library that docs.rs lacks, or an unstable feature used withoutcfg_attr(docsrs, ...). The nightly job above catches the last one. For the other two, makebuild.rsskip work whenDOCS_RSis set. docs.rs sets that environment variable during its builds. - Feature-gated doc tests silently not run. If CI omits
--all-features, examples inside#[cfg(feature = "json")]items are never compiled. Compare the number of doc tests that ran with your expectation. - Links that work locally but break in CI. An intra-doc link to a private item warns under
rustdoc::private_intra_doc_linkswhen the page is built without--document-private-items. Link only to public items from public docs. - A new rustdoc lint on a newer compiler. With
-D warningsonstable, a toolchain upgrade can fail the build without any code change. Pin the toolchain version, or keep-D warningsonly in CI and not inCargo.toml. - Yanked or broken release. A docs.rs build can fail after publishing, and you cannot republish the same version. Run the docs.rs-style check before
cargo publish, and usecargo publish --dry-runto verify packaging. The dry run does not run the docs.rs build.
Summary of the commands
| Goal | Command |
|---|---|
| Run all doc tests | `cargo test --doc --all-features` |
| Build docs and fail on any warning | `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features` |
| Fail on missing docs | `cargo clippy --all-features -- -D missing_docs` |
| Simulate docs.rs | `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features` |
Exercise
Your CI runs cargo test and cargo doc --no-deps, and both pass. A teammate renames Parser to Lexer, but a doc comment still contains [Parser]. Why does CI pass, and what is the smallest change that makes it fail?
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 cargo doc step. To deny only this problem, use RUSTDOCFLAGS="-D rustdoc::broken_intra_doc_links".
Exercise
Your crate has a json feature. The docs.rs page shows no JSON items, and the items you do see have no feature labels. Which Cargo.toml table do you edit, which two settings do you add, and which attribute marks to_json as needing the feature?
Show answer
Edit [package.metadata.docs.rs]. Add all-features = true so the feature is built, and rustdoc-args = ["--cfg", "docsrs"] so the docsrs cfg is set. Enable #![cfg_attr(docsrs, feature(doc_cfg))] in the crate root, and put #[cfg_attr(docsrs, doc(cfg(feature = "json")))] on to_json.
Exercise
A doc test in your crate fails on your laptop but never fails on docs.rs. Does that mean the example is fine? Explain.
Show answer
No. docs.rs only builds HTML and does not run doc tests, so it never reports example failures. Only your own cargo test --doc run, locally or in CI, executes the examples.