{"record":{"id":"d0d427079a3b52a5","repo":"BoundaryML/baml","slug":"colored-diagnostics-have-a-source-highlighter","errorCode":null,"errorMessage":"colored diagnostics have a source highlighter","messagePattern":"colored diagnostics have a source highlighter","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler_diagnostics/src/render.rs","lineNumber":331,"sourceCode":"    severity: Severity,\n) -> GraphicalReportHandler {\n    let mut theme = if color {\n        GraphicalTheme::unicode()\n    } else {\n        GraphicalTheme::unicode_nocolor()\n    };\n    if color {\n        theme.styles.highlights = annotation_styles(severity);\n    }\n    let mut handler = GraphicalReportHandler::new_themed(theme)\n        .with_links(false)\n        .with_urls(false)\n        .with_show_related_as_nested(true)\n        .with_break_words(false)\n        .with_context_lines(0);\n    if color {\n        handler = handler.with_syntax_highlighting(\n            highlighter.expect(\"colored diagnostics have a source highlighter\"),\n        );\n    } else {\n        handler = handler.without_syntax_highlighting();\n    }\n    handler\n}\n\nfn render_miette_with_handler(\n    diagnostic: &Diagnostic,\n    sources: &HashMap<FileId, String>,\n    file_paths: &HashMap<FileId, PathBuf>,\n    handler: &GraphicalReportHandler,\n    color: bool,\n    show_error_codes: bool,\n    message_highlighter: Option<&dyn DiagnosticMessageHighlighter>,\n) -> Result<String, DiagnosticMessageHighlightError> {\n    let diagnostic = build_rendered_diagnostic(\n        diagnostic,","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler_diagnostics/src/render.rs#L313-L349","documentation":"miette_handler configures the graphical report handler; when color is enabled it requires a source-code syntax highlighter and panics with this message if the Option<DiagnosticHighlighter> is None. The invariant is: colored output always comes with a highlighter (callers like render_miette and render_diagnostics_with_highlighters supply one when color=true).","triggerScenarios":"Calling miette_handler (directly or through render_miette / render_diagnostics_with_highlighters) with color=true but highlighter=None — e.g. a code change that drops the highlighter argument or a call site that forgets to thread the user-configured highlighter through.","commonSituations":"Only when hacking on the crate: adding a new render entry point that enables color but forgets to pass the DiagnosticHighlighter, or a config path where theme/color is resolved to true without loading the highlighter.","solutions":["Pass a DiagnosticHighlighter whenever color is enabled (the same value the standard render entry points use).","Guard the call site: only request colored output when a highlighter is available, otherwise fall back to no-color.","If you want color without semantic highlighting, change miette_handler to use a default syntax highlighting theme instead of expect()."],"exampleFix":"// before\nif color {\n    handler = handler.with_syntax_highlighting(\n        highlighter.expect(\"colored diagnostics have a source highlighter\"),\n    );\n}\n// after\nif color {\n    match highlighter {\n        Some(h) => handler = handler.with_syntax_highlighting(h),\n        None => { color = false; handler = handler.without_syntax_highlighting(); }\n    }\n}","handlingStrategy":"validation","validationCode":"if color && highlighter.is_none() {\n    // disable color or supply a DiagnosticHighlighter before rendering\n    color = false;\n}","typeGuard":"fn highlighter_ready(color: bool, h: &Option<DiagnosticHighlighter>) -> bool {\n    !color || h.is_some()\n}","tryCatchPattern":"let out = std::panic::catch_unwind(|| render_diagnostic(&diag, &sources, &cfg, Some(highlighter)));\nif out.is_err() { /* retry without color */ }","preventionTips":["Always thread the configured DiagnosticHighlighter through every colored render entry point.","Before enabling color, confirm a highlighter is available; otherwise fall back to no-color.","Add a unit test asserting colored rendering always receives Some(highlighter)."],"tags":["rust","rendering","color","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}