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

diagnostic error detected

Error message

diagnostic error detected

What it means

Bailed by the rust-analyzer `diagnostics` CLI subcommand (flags::Diagnostics::run) after scanning all non-library modules if at least one diagnostic had Severity::Error. The subcommand's contract is to exit non-zero when errors are present; this bail implements that exit code.

Source

Thrown at src/tools/rust-analyzer/crates/rust-analyzer/src/cli/diagnostics.rs:119

                    let end = line_index.line_col(range.range.end());
                    bar.println(format!(
                        "at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}",
                        _vfs.file_path(file_id.file_id(db))
                    ));
                }

                visited_files.insert(file_id);
            }
            bar.inc(1);
        }
        bar.finish_and_clear();

        println!();
        println!("diagnostic scan complete");

        if found_error {
            println!();
            anyhow::bail!("diagnostic error detected")
        }

        Ok(())
    }
}

fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
    let mut worklist: Vec<_> =
        Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
    let mut modules = Vec::new();

    while let Some(module) = worklist.pop() {
        modules.push(module);
        worklist.extend(module.children(db));
    }

    modules
}

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. This is the intended behavior — read the printed diagnostics above the bail line and fix each error in the source.
  2. Pass --severity to raise the threshold (e.g. only Error) if you want to ignore warnings.
  3. If the diagnostic is spurious, check sysroot/proc-macro configuration and file/refresh the r-a issue rather than silencing the exit.

Example fix

// before: blanket bail on any error diagnostic
if found_error {
    anyhow::bail!("diagnostic error detected");
}

// after: include a count and the list of files for quicker triage
if found_error {
    anyhow::bail!(
        "diagnostic error detected: {} error(s) across {} file(s): {}",
        error_count, error_files.len(), error_files.join(", ")
    );
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check: run `cargo check` so you know the workspace compiles before
// asking rust-analyzer to scan for diagnostics:
fn workspace_compiles(path: &Path) -> bool {
    std::process::Command::new("cargo").arg("check")
        .current_dir(path).output().map(|o| o.status.success()).unwrap_or(false)
}

Type guard

null

Try / catch

// In CI, treat the diagnostics subcommand's non-zero exit as the signal,
// not as an infrastructure failure:
let status = std::process::Command::new("rust-analyzer")
    .args(["diagnostics", path.to_str().unwrap()]).status()?;
if !status.success() {
    eprintln!("rust-analyzer reported error-severity diagnostics; see output above");
}
// Do NOT retry; fix the source diagnostics.

Prevention

When it happens

Trigger: Running `rust-analyzer diagnostics <path>` (the CLI mode, not the server) on a workspace where rust-analyzer's analysis reports at least one error-severity diagnostic above the configured --severity threshold.

Common situations: Intentional use in CI to fail builds on diagnostics; running it on a broken checkout; proc-macro expansion producing an error rust-analyzer trusts; misconfigured sysroot/Cargo features that make real code look erroneous.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/2cd3a087c821732f. Report an issue: GitHub.