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
- Drop target filters that removed all doc-able targets, or run `cargo doc --open` with no extra filters.
- Ensure the package has a library target (src/lib.rs) so docs are produced.
- If documenting a binary, pass `--document-private-items` (or use -Z rustdoc flags as appropriate).
- 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
- Run `cargo doc` once without --open to confirm output exists.
- Ensure the package has a library target before --open.
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
- argument for --color must be auto, always, or never, but fou
- subcommand is required, add a subcommand to the command alia
- alias {} has unresolvable recursive definition: {} -> {}
- invalid character `+` in dependency name: `+{toolchain}`
- feature `{feature}` must be qualified by the dependency it's
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/6dd2be052399d1fd.json.
Report an issue: GitHub.