rust-lang/cargo · error · anyhow::Error

cannot open specified crate's documentation: no documentatio

Error message

cannot open specified crate's documentation: no documentation generated

What it means

Thrown in ops::doc (src/ops/cargo_doc.rs:65-70) when --open was requested but compilation.root_crate_names is empty, i.e. no documentation output was produced to open. Cargo then cannot find a root crate name to build the doc path.

Source

Thrown at src/ops/cargo_doc.rs:67

    /// Whether to attempt to open the browser after compiling the docs
    pub open_result: bool,
    /// Same as `rustdoc --output-format`
    pub output_format: OutputFormat,
    /// Options to pass through to the compiler
    pub compile_opts: ops::CompileOptions,
}

/// Main method for `cargo doc`.
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
    let compilation = ops::compile(ws, &options.compile_opts)?;

    if ws.gctx().cli_unstable().rustdoc_mergeable_info {
        merge_cross_crate_info(ws, &compilation)?;
    }

    if options.open_result {
        let name = &compilation.root_crate_names.get(0).ok_or_else(|| {
            anyhow::anyhow!(
                "cannot open specified crate's documentation: no documentation generated"
            )
        })?;
        let kind = options.compile_opts.build_config.single_requested_kind()?;

        let path = path_by_output_format(&compilation, &kind, &name, &options.output_format);

        if path.exists() {
            util::open::open(&path, ws.gctx())?;
        }
    } else if ws.gctx().shell().verbosity() == Verbosity::Verbose {
        for name in &compilation.root_crate_names {
            for kind in &options.compile_opts.build_config.requested_kinds {
                let path =
                    path_by_output_format(&compilation, &kind, &name, &options.output_format);
                if path.exists() {
                    let mut shell = ws.gctx().shell();
                    let link = shell.err_file_hyperlink(&path);

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Drop target filters that removed all doc-able targets, or run `cargo doc --open` with no extra filters.
  2. Ensure the package has a library target (src/lib.rs) so docs are produced.
  3. If documenting a binary, pass `--document-private-items` (or use -Z rustdoc flags as appropriate).
  4. First run `cargo doc` (without --open) and confirm output appears under target/doc.

Example fix

# before
cargo doc --open --bins        # nothing to open

# after
cargo doc --open               # document the library and open it
Defensive patterns

Strategy: validation

Validate before calling

// Only request --open when there is at least one documented root target.
fn has_doc_root(pkg: &cargo::core::Package) -> bool {
    pkg.targets().iter().any(|t| t.is_lib() || t.is_bin())
}
// if !members.any(has_doc_root) { skip --open }

Type guard

fn can_open_docs(compilation: &ops::Compilation) -> bool {
    !compilation.root_crate_names.is_empty()
}

Try / catch

match ops::doc(ws, &opts) {
    Err(e) if e.to_string().contains("no documentation generated") => {
        eprintln!("nothing to open; drop target filters or add a [lib]");
        return Err(e);
    }
    r => r,
}

Prevention

When it happens

Trigger: `cargo doc --open` when there are no targets that generate documentation (e.g. a package whose targets were all filtered out, no lib and doc disabled for bins, or a prior compile step produced no root names). The get(0) returns None and ok_or_else fires.

Common situations: Running `cargo doc --open` on a binary-only package with `document-private-items` off / no doc output. A combination of target filters that exclude everything. Doc generation skipped due to a separate error that left root_crate_names empty.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/6dd2be052399d1fd.json. Report an issue: GitHub.