From 6a652d2ff3387fd33830bc8026b2ecb92da860f1 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 27 Aug 2025 12:01:46 -0400 Subject: [PATCH] handle non-ruff files, update snapshots --- .../ruff_db/src/diagnostic/render/gitlab.rs | 89 ++++++++++--------- ...ostic__render__gitlab__tests__output.snap} | 4 +- ...render__gitlab__tests__syntax_errors.snap} | 4 +- 3 files changed, 52 insertions(+), 45 deletions(-) rename crates/{ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap => ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__output.snap} (91%) rename crates/{ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap => ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__syntax_errors.snap} (88%) diff --git a/crates/ruff_db/src/diagnostic/render/gitlab.rs b/crates/ruff_db/src/diagnostic/render/gitlab.rs index 5853c98601..bfb159fa04 100644 --- a/crates/ruff_db/src/diagnostic/render/gitlab.rs +++ b/crates/ruff_db/src/diagnostic/render/gitlab.rs @@ -60,55 +60,62 @@ impl Serialize for SerializedMessages<'_> { let mut fingerprints = HashSet::::with_capacity(self.diagnostics.len()); for diagnostic in self.diagnostics { - let span = diagnostic.expect_primary_span(); - let file = span.file(); - let filename = diagnostic.expect_ruff_filename(); + if let Some(span) = diagnostic.primary_span() { + let file = span.file(); + let filename = file.path(self.resolver); - let (start_location, end_location) = if self.resolver.is_notebook(file) { - // We can't give a reasonable location for the structured formats, - // so we show one that's clearly a fallback - Default::default() - } else { - ( - diagnostic.expect_ruff_start_location(), - diagnostic.expect_ruff_end_location(), - ) - }; + let (start_location, end_location) = if self.resolver.is_notebook(file) { + // We can't give a reasonable location for the structured formats, + // so we show one that's clearly a fallback + Default::default() + } else { + let diagnostic_source = file.diagnostic_source(self.resolver); + let source_code = diagnostic_source.as_source_code(); + span.range() + .map(|range| { + ( + source_code.line_column(range.start()), + source_code.line_column(range.end()), + ) + }) + .unwrap_or_default() + }; - let path = self.project_dir.as_ref().map_or_else( - || file.relative_path(self.resolver).display().to_string(), - |project_dir| relativize_path_to(&filename, project_dir), - ); + let path = self.project_dir.as_ref().map_or_else( + || file.relative_path(self.resolver).display().to_string(), + |project_dir| relativize_path_to(filename, project_dir), + ); - let mut message_fingerprint = fingerprint(diagnostic, &path, 0); + let mut message_fingerprint = fingerprint(diagnostic, &path, 0); - // Make sure that we do not get a fingerprint that is already in use - // by adding in the previously generated one. - while fingerprints.contains(&message_fingerprint) { - message_fingerprint = fingerprint(diagnostic, &path, message_fingerprint); - } - fingerprints.insert(message_fingerprint); + // Make sure that we do not get a fingerprint that is already in use + // by adding in the previously generated one. + while fingerprints.contains(&message_fingerprint) { + message_fingerprint = fingerprint(diagnostic, &path, message_fingerprint); + } + fingerprints.insert(message_fingerprint); - let description = diagnostic.body(); - let check_name = diagnostic.secondary_code_or_id(); + let description = diagnostic.body(); + let check_name = diagnostic.secondary_code_or_id(); - let value = json!({ - "check_name": check_name, - // GitLab doesn't display the separate `check_name` field in a Code Quality report, - // so prepend it to the description too. - "description": format!("{check_name}: {description}"), - "severity": "major", - "fingerprint": format!("{:x}", message_fingerprint), - "location": { - "path": path, - "positions": { - "begin": start_location, - "end": end_location, + let value = json!({ + "check_name": check_name, + // GitLab doesn't display the separate `check_name` field in a Code Quality report, + // so prepend it to the description too. + "description": format!("{check_name}: {description}"), + "severity": "major", + "fingerprint": format!("{:x}", message_fingerprint), + "location": { + "path": path, + "positions": { + "begin": start_location, + "end": end_location, + }, }, - }, - }); + }); - s.serialize_element(&value)?; + s.serialize_element(&value)?; + } } s.end() diff --git a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap b/crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__output.snap similarity index 91% rename from crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap rename to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__output.snap index c106eb70ba..b4d38416c5 100644 --- a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap +++ b/crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__output.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_linter/src/message/gitlab.rs -expression: redact_fingerprint(&content) +source: crates/ruff_db/src/diagnostic/render/gitlab.rs +expression: env.render_diagnostics(&diagnostics) --- [ { diff --git a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap b/crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__syntax_errors.snap similarity index 88% rename from crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap rename to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__syntax_errors.snap index 7979aa977a..66c77b6604 100644 --- a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap +++ b/crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__gitlab__tests__syntax_errors.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_linter/src/message/gitlab.rs -expression: redact_fingerprint(&content) +source: crates/ruff_db/src/diagnostic/render/gitlab.rs +expression: env.render_diagnostics(&diagnostics) --- [ {