diff --git a/DISCLOSURE.md b/DISCLOSURE.md index 25a8f30..a1cfe02 100644 --- a/DISCLOSURE.md +++ b/DISCLOSURE.md @@ -51,6 +51,7 @@ The automated suite covers: - use of unsaved in-memory text and suppression of stale results; - JSON schema, metadata, severity, URL, and line-range validation; - syntax-error location, stale-warning removal, and correction suppression; +- whole-document analysis errors when VSG cannot provide a safe location; - default severity demotion and opt-in strict severity mapping; - safe project-configuration generation without overwriting existing files; - hover capability suppression for multi-language-server editor setups; diff --git a/README.md b/README.md index 8d0a063..94e9bc1 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,10 @@ format the current in-memory buffer through the same VSG fix-all path used by If VSG encounters a syntax error, the bridge replaces stale style findings with one error on the reported line and temporarily withholds formatting and correction actions. Normal diagnostics and actions return after parsing -succeeds again. +succeeds again. When VSG cannot determine a trustworthy location, the bridge +reports one whole-document `analysis-failed` error instead. Its wording says +that VSG could not analyze the document, because unsupported valid VHDL can also +cause a parser failure. Style diagnostics use editor-friendly levels by default: VSG errors appear as LSP warnings, and VSG warnings appear as information. This keeps lint findings diff --git a/doc/architecture/vsg-interface.md b/doc/architecture/vsg-interface.md index e7c5745..fafb4e5 100644 --- a/doc/architecture/vsg-interface.md +++ b/doc/architecture/vsg-interface.md @@ -160,9 +160,12 @@ dangling rule references, malformed URLs, or inconsistent metadata are recoverable tool failures: the bridge warns, clears stale style findings and correction actions, and remains available for the next document update. A located `syntax` error is published as a non-fixable LSP error on the reported -line; other incomplete failures publish an empty set. Complete analysis restores -normal style diagnostics and correction actions. One bad response must never -crash the language server or corrupt results from a newer document version. +line. If a structured incomplete analysis has no trustworthy location, the +bridge publishes one non-fixable `analysis-failed` error covering the document. +This error reports VSG's inability to analyze the input rather than asserting +that the input is invalid VHDL. Complete analysis restores normal style +diagnostics and correction actions. One bad response must never crash the +language server or corrupt results from a newer document version. ## Correction granularity diff --git a/src/diagnostics.rs b/src/diagnostics.rs index dac0142..24db73f 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -229,13 +229,14 @@ pub(crate) fn parse_diagnostics( let (detail, diagnostics) = match error { Some((detail, error)) => ( detail, - syntax_diagnostic(error, document_text) - .into_iter() - .collect(), + vec![analysis_failure_diagnostic(error, document_text)], ), None => ( "VSG diagnostic analysis did not complete".to_owned(), - Vec::new(), + vec![generic_analysis_failure_diagnostic( + "VSG diagnostic analysis did not complete", + document_text, + )], ), }; return Ok(incomplete(detail, diagnostics)); @@ -300,24 +301,42 @@ fn incomplete(message: String, diagnostics: Vec) -> DiagnosticRespon } } -/// Converts a structured parser failure into a non-fixable LSP error. -fn syntax_diagnostic(error: &VsgError, document_text: &str) -> Option { - if error.kind != "syntax" { - return None; - } - let location = error.location.as_ref()?; - if location.line == 0 || location.column == Some(0) { - return None; +/// Converts an incomplete analysis into a precise or whole-file non-fixable error. +fn analysis_failure_diagnostic(error: &VsgError, document_text: &str) -> Diagnostic { + if error.kind == "syntax" { + if let Some(location) = error + .location + .as_ref() + .filter(|location| location.line > 0 && location.column != Some(0)) + { + let metrics = LineMetrics::new(document_text); + return Diagnostic { + range: metrics.range_for_vsg_lines(location.line, location.line), + severity: Some(DiagnosticSeverity::ERROR), + code: Some(NumberOrString::String("syntax".to_owned())), + source: Some(DIAGNOSTIC_SOURCE.to_owned()), + message: error.message.clone(), + ..Diagnostic::default() + }; + } } + + generic_analysis_failure_diagnostic(&error.message, document_text) +} + +/// Covers the complete document when VSG cannot provide a trustworthy location. +fn generic_analysis_failure_diagnostic(message: &str, document_text: &str) -> Diagnostic { let metrics = LineMetrics::new(document_text); - Some(Diagnostic { - range: metrics.range_for_vsg_lines(location.line, location.line), + Diagnostic { + range: metrics.range_for_vsg_lines(1, u32::MAX), severity: Some(DiagnosticSeverity::ERROR), - code: Some(NumberOrString::String("syntax".to_owned())), + code: Some(NumberOrString::String("analysis-failed".to_owned())), source: Some(DIAGNOSTIC_SOURCE.to_owned()), - message: error.message.clone(), + message: format!( + "VSG could not analyze this document: {message}. This may indicate a syntax error or unsupported VHDL." + ), ..Diagnostic::default() - }) + } } /// Legacy version-one documents predate the explicit completion marker. @@ -623,6 +642,43 @@ mod tests { ); } + /// An unlocated analysis failure remains visible as a whole-document error. + #[test] + fn converts_unlocated_analysis_failure() { + let output = json!({ + "schema": "vsg.diagnostics", + "schema_version": 1, + "complete": false, + "tool": {"name": "vsg", "version": "3.35.0"}, + "source": {"name": "stdin"}, + "rules": {}, + "diagnostics": [], + "errors": [{"kind": "analysis", "message": "parser stopped"}], + }) + .to_string(); + + let DiagnosticResponse::Incomplete { diagnostics, .. } = + parse_diagnostics(&output, "first\nsecond", false) + .expect("compatible incomplete response") + else { + panic!("expected incomplete response") + }; + assert_eq!(diagnostics.len(), 1); + assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::ERROR)); + assert_eq!( + diagnostics[0].range, + Range::new(Position::new(0, 0), Position::new(1, 6)) + ); + assert_eq!( + diagnostics[0].code, + Some(NumberOrString::String("analysis-failed".to_owned())) + ); + assert!(diagnostics[0] + .message + .contains("may indicate a syntax error")); + assert!(diagnostics[0].data.is_none()); + } + /// A located syntax failure becomes an editor diagnostic without correction data. #[test] fn converts_located_syntax_failure() {