{"record":{"id":"2cd3a087c821732f","repo":"rust-lang/rust","slug":"diagnostic-error-detected","errorCode":null,"errorMessage":"diagnostic error detected","messagePattern":"diagnostic error detected","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/tools/rust-analyzer/crates/rust-analyzer/src/cli/diagnostics.rs","lineNumber":119,"sourceCode":"                    let end = line_index.line_col(range.range.end());\n                    bar.println(format!(\n                        \"at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}\",\n                        _vfs.file_path(file_id.file_id(db))\n                    ));\n                }\n\n                visited_files.insert(file_id);\n            }\n            bar.inc(1);\n        }\n        bar.finish_and_clear();\n\n        println!();\n        println!(\"diagnostic scan complete\");\n\n        if found_error {\n            println!();\n            anyhow::bail!(\"diagnostic error detected\")\n        }\n\n        Ok(())\n    }\n}\n\nfn all_modules(db: &dyn HirDatabase) -> Vec<Module> {\n    let mut worklist: Vec<_> =\n        Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();\n    let mut modules = Vec::new();\n\n    while let Some(module) = worklist.pop() {\n        modules.push(module);\n        worklist.extend(module.children(db));\n    }\n\n    modules\n}","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/diagnostics.rs#L101-L137","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["This is the intended behavior — read the printed diagnostics above the bail line and fix each error in the source.","Pass --severity to raise the threshold (e.g. only Error) if you want to ignore warnings.","If the diagnostic is spurious, check sysroot/proc-macro configuration and file/refresh the r-a issue rather than silencing the exit."],"exampleFix":"// before: blanket bail on any error diagnostic\nif found_error {\n    anyhow::bail!(\"diagnostic error detected\");\n}\n\n// after: include a count and the list of files for quicker triage\nif found_error {\n    anyhow::bail!(\n        \"diagnostic error detected: {} error(s) across {} file(s): {}\",\n        error_count, error_files.len(), error_files.join(\", \")\n    );\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check: run `cargo check` so you know the workspace compiles before\n// asking rust-analyzer to scan for diagnostics:\nfn workspace_compiles(path: &Path) -> bool {\n    std::process::Command::new(\"cargo\").arg(\"check\")\n        .current_dir(path).output().map(|o| o.status.success()).unwrap_or(false)\n}","typeGuard":"null","tryCatchPattern":"// In CI, treat the diagnostics subcommand's non-zero exit as the signal,\n// not as an infrastructure failure:\nlet status = std::process::Command::new(\"rust-analyzer\")\n    .args([\"diagnostics\", path.to_str().unwrap()]).status()?;\nif !status.success() {\n    eprintln!(\"rust-analyzer reported error-severity diagnostics; see output above\");\n}\n// Do NOT retry; fix the source diagnostics.","preventionTips":["Treat the non-zero exit as intended (it means real diagnostics exist), not a crash.","Use --severity to filter which diagnostics count as failures.","Investigate spurious errors as sysroot/proc-macro config issues, not by silencing the exit."],"tags":["rust-analyzer","cli","diagnostics","ci"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}