handle non-ruff files, update snapshots

This commit is contained in:
Brent Westbrook 2025-08-27 12:01:46 -04:00
parent 8f909ea0bb
commit 6a652d2ff3
3 changed files with 52 additions and 45 deletions

View file

@ -60,55 +60,62 @@ impl Serialize for SerializedMessages<'_> {
let mut fingerprints = HashSet::<u64>::with_capacity(self.diagnostics.len()); let mut fingerprints = HashSet::<u64>::with_capacity(self.diagnostics.len());
for diagnostic in self.diagnostics { for diagnostic in self.diagnostics {
let span = diagnostic.expect_primary_span(); if let Some(span) = diagnostic.primary_span() {
let file = span.file(); let file = span.file();
let filename = diagnostic.expect_ruff_filename(); let filename = file.path(self.resolver);
let (start_location, end_location) = if self.resolver.is_notebook(file) { let (start_location, end_location) = if self.resolver.is_notebook(file) {
// We can't give a reasonable location for the structured formats, // We can't give a reasonable location for the structured formats,
// so we show one that's clearly a fallback // so we show one that's clearly a fallback
Default::default() Default::default()
} else { } else {
( let diagnostic_source = file.diagnostic_source(self.resolver);
diagnostic.expect_ruff_start_location(), let source_code = diagnostic_source.as_source_code();
diagnostic.expect_ruff_end_location(), 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( let path = self.project_dir.as_ref().map_or_else(
|| file.relative_path(self.resolver).display().to_string(), || file.relative_path(self.resolver).display().to_string(),
|project_dir| relativize_path_to(&filename, project_dir), |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 // Make sure that we do not get a fingerprint that is already in use
// by adding in the previously generated one. // by adding in the previously generated one.
while fingerprints.contains(&message_fingerprint) { while fingerprints.contains(&message_fingerprint) {
message_fingerprint = fingerprint(diagnostic, &path, message_fingerprint); message_fingerprint = fingerprint(diagnostic, &path, message_fingerprint);
} }
fingerprints.insert(message_fingerprint); fingerprints.insert(message_fingerprint);
let description = diagnostic.body(); let description = diagnostic.body();
let check_name = diagnostic.secondary_code_or_id(); let check_name = diagnostic.secondary_code_or_id();
let value = json!({ let value = json!({
"check_name": check_name, "check_name": check_name,
// GitLab doesn't display the separate `check_name` field in a Code Quality report, // GitLab doesn't display the separate `check_name` field in a Code Quality report,
// so prepend it to the description too. // so prepend it to the description too.
"description": format!("{check_name}: {description}"), "description": format!("{check_name}: {description}"),
"severity": "major", "severity": "major",
"fingerprint": format!("{:x}", message_fingerprint), "fingerprint": format!("{:x}", message_fingerprint),
"location": { "location": {
"path": path, "path": path,
"positions": { "positions": {
"begin": start_location, "begin": start_location,
"end": end_location, "end": end_location,
},
}, },
}, });
});
s.serialize_element(&value)?; s.serialize_element(&value)?;
}
} }
s.end() s.end()

View file

@ -1,6 +1,6 @@
--- ---
source: crates/ruff_linter/src/message/gitlab.rs source: crates/ruff_db/src/diagnostic/render/gitlab.rs
expression: redact_fingerprint(&content) expression: env.render_diagnostics(&diagnostics)
--- ---
[ [
{ {

View file

@ -1,6 +1,6 @@
--- ---
source: crates/ruff_linter/src/message/gitlab.rs source: crates/ruff_db/src/diagnostic/render/gitlab.rs
expression: redact_fingerprint(&content) expression: env.render_diagnostics(&diagnostics)
--- ---
[ [
{ {