rust-lang/rust-analyzer · error

diagnostic error detected

Error message

diagnostic error detected

What it means

The `rust-analyzer diagnostics` CLI scans the project for unsound/diagnostic findings (mismatched args, no-such-field, etc.). It collects a boolean `found_error` while printing each diagnostic, and if any error-level diagnostic was seen, it finishes by bailing with "diagnostic error detected" so the CLI exits non-zero. This is an aggregate signal — the specific findings are printed above it in stdout.

Source

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

                    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 e8f7e90aa3)

Solutions

  1. Read the diagnostics printed before the bail; fix each reported error in the source.
  2. Run `cargo check` on the target to confirm/see the underlying compiler errors.
  3. If scanning intentionally-broken fixtures, expect non-zero exit and treat it as the success signal.
  4. Re-run after fixes; the exit code 0 means the scan found no error-level diagnostics.

Example fix

// before
rust-analyzer diagnostics ./crates/broken  # exits non-zero
// after fixing reported diagnostics in the source
rust-analyzer diagnostics ./crates/broken  # exits 0
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check the code compiles before running the diagnostics CLI
let status = Command::new("cargo").args(["check", "--workspace"]).status()?;
if !status.success() {
    // fix compile errors first; diagnostics CLI will exit non-zero
}

Try / catch

let status = Command::new("rust-analyzer")
    .args(["diagnostics", path])
    .status()?;
if !status.success() {
    // parse the diagnostics printed on stdout above the bail message
    // and fix each reported finding, or treat as expected for fixtures
}

Prevention

When it happens

Trigger: Running `rust-analyzer diagnostics <path>` on code containing error-level rust-analyzer diagnostics (e.g. mismatched-arg-count, missing fields, type errors) so `found_error` becomes true at scan end.

Common situations: CI job running the diagnostics CLI on a crate with real compile-level errors; using the tool to smoke-test a project that is actually broken; running against generated code that doesn't compile.

Related errors


AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03). Data as JSON: /api/errors/005a86e0b4c2ff85. Report an issue: GitHub.