From bc6e105c1877bec057b58a65fff99d43cc4beb9e Mon Sep 17 00:00:00 2001 From: cristian64 Date: Sun, 3 Aug 2025 14:37:01 +0200 Subject: [PATCH] Include column numbers in GitLab output format. (#19708) --- .../snapshots/lint__output_format_gitlab.snap | 48 +++++++++++++------ crates/ruff_linter/src/message/gitlab.rs | 26 +++++----- ...inter__message__gitlab__tests__output.snap | 48 +++++++++++++------ ...message__gitlab__tests__syntax_errors.snap | 32 +++++++++---- 4 files changed, 100 insertions(+), 54 deletions(-) diff --git a/crates/ruff/tests/snapshots/lint__output_format_gitlab.snap b/crates/ruff/tests/snapshots/lint__output_format_gitlab.snap index e25a54bfc3..304d2a748f 100644 --- a/crates/ruff/tests/snapshots/lint__output_format_gitlab.snap +++ b/crates/ruff/tests/snapshots/lint__output_format_gitlab.snap @@ -22,11 +22,17 @@ exit_code: 1 "description": "`os` imported but unused", "fingerprint": "4dbad37161e65c72", "location": { - "lines": { - "begin": 1, - "end": 1 - }, - "path": "input.py" + "path": "input.py", + "positions": { + "begin": { + "column": 8, + "line": 1 + }, + "end": { + "column": 10, + "line": 1 + } + } }, "severity": "major" }, @@ -35,11 +41,17 @@ exit_code: 1 "description": "Undefined name `y`", "fingerprint": "7af59862a085230", "location": { - "lines": { - "begin": 2, - "end": 2 - }, - "path": "input.py" + "path": "input.py", + "positions": { + "begin": { + "column": 5, + "line": 2 + }, + "end": { + "column": 6, + "line": 2 + } + } }, "severity": "major" }, @@ -48,11 +60,17 @@ exit_code: 1 "description": "Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)", "fingerprint": "e558cec859bb66e8", "location": { - "lines": { - "begin": 3, - "end": 3 - }, - "path": "input.py" + "path": "input.py", + "positions": { + "begin": { + "column": 1, + "line": 3 + }, + "end": { + "column": 6, + "line": 3 + } + } }, "severity": "major" } diff --git a/crates/ruff_linter/src/message/gitlab.rs b/crates/ruff_linter/src/message/gitlab.rs index 1fe27051b1..9dc5423734 100644 --- a/crates/ruff_linter/src/message/gitlab.rs +++ b/crates/ruff_linter/src/message/gitlab.rs @@ -61,22 +61,17 @@ impl Serialize for SerializedMessages<'_> { let mut fingerprints = HashSet::::with_capacity(self.diagnostics.len()); for diagnostic in self.diagnostics { - let start_location = diagnostic.expect_ruff_start_location(); - let end_location = diagnostic.expect_ruff_end_location(); - let filename = diagnostic.expect_ruff_filename(); - let lines = if self.context.is_notebook(&filename) { + + let (start_location, end_location) = if self.context.is_notebook(&filename) { // We can't give a reasonable location for the structured formats, // so we show one that's clearly a fallback - json!({ - "begin": 1, - "end": 1 - }) + Default::default() } else { - json!({ - "begin": start_location.line, - "end": end_location.line - }) + ( + diagnostic.expect_ruff_start_location(), + diagnostic.expect_ruff_end_location(), + ) }; let path = self.project_dir.as_ref().map_or_else( @@ -111,8 +106,11 @@ impl Serialize for SerializedMessages<'_> { "fingerprint": format!("{:x}", message_fingerprint), "location": { "path": path, - "lines": lines - } + "positions": { + "begin": start_location, + "end": end_location, + }, + }, }); s.serialize_element(&value)?; diff --git a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap b/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap index 438a434b6a..28565a1553 100644 --- a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap +++ b/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__output.snap @@ -8,11 +8,17 @@ expression: redact_fingerprint(&content) "description": "`os` imported but unused", "fingerprint": "", "location": { - "lines": { - "begin": 1, - "end": 1 - }, - "path": "fib.py" + "path": "fib.py", + "positions": { + "begin": { + "column": 8, + "line": 1 + }, + "end": { + "column": 10, + "line": 1 + } + } }, "severity": "major" }, @@ -21,11 +27,17 @@ expression: redact_fingerprint(&content) "description": "Local variable `x` is assigned to but never used", "fingerprint": "", "location": { - "lines": { - "begin": 6, - "end": 6 - }, - "path": "fib.py" + "path": "fib.py", + "positions": { + "begin": { + "column": 5, + "line": 6 + }, + "end": { + "column": 6, + "line": 6 + } + } }, "severity": "major" }, @@ -34,11 +46,17 @@ expression: redact_fingerprint(&content) "description": "Undefined name `a`", "fingerprint": "", "location": { - "lines": { - "begin": 1, - "end": 1 - }, - "path": "undef.py" + "path": "undef.py", + "positions": { + "begin": { + "column": 4, + "line": 1 + }, + "end": { + "column": 5, + "line": 1 + } + } }, "severity": "major" } diff --git a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap b/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap index a9ff0a9311..b8440fae2c 100644 --- a/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap +++ b/crates/ruff_linter/src/message/snapshots/ruff_linter__message__gitlab__tests__syntax_errors.snap @@ -8,11 +8,17 @@ expression: redact_fingerprint(&content) "description": "Expected one or more symbol names after import", "fingerprint": "", "location": { - "lines": { - "begin": 1, - "end": 2 - }, - "path": "syntax_errors.py" + "path": "syntax_errors.py", + "positions": { + "begin": { + "column": 15, + "line": 1 + }, + "end": { + "column": 1, + "line": 2 + } + } }, "severity": "major" }, @@ -21,11 +27,17 @@ expression: redact_fingerprint(&content) "description": "Expected ')', found newline", "fingerprint": "", "location": { - "lines": { - "begin": 3, - "end": 4 - }, - "path": "syntax_errors.py" + "path": "syntax_errors.py", + "positions": { + "begin": { + "column": 12, + "line": 3 + }, + "end": { + "column": 1, + "line": 4 + } + } }, "severity": "major" }