Skip to content

Enrich messages of SARIF results #13179

@RussellLuo

Description

@RussellLuo

Given the following code snippet:

# test.py
import os

def greet():
    print('hello {name}'.format())

This is the default output:

$ ruff check test.py
test.py:1:8: F401 [*] `os` imported but unused
  |
1 | import os
  |        ^^ F401
2 | 
3 | def greet():
  |
  = help: Remove unused import: `os`

test.py:4:11: F524 `.format` call is missing argument(s) for placeholder(s): name
  |
3 | def greet():
4 |     print('hello {name}'.format())
  |           ^^^^^^^^^^^^^^^^^^^^^^^ F524
  |

Found 2 errors.
[*] 1 fixable with the `--fix` option.

If we try to get the SARIF output:

ruff check --output-format=sarif test.py

As shown below, the result messages (e.g. UnusedImport and StringDotFormatMissingArguments) are too short to be informative:

{
  "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
  "runs": [
    {
      "results": [
        {
          "level": "error",
          "locations": [
            {
              "physicalLocation": {
                "artifactLocation": {...},
                "region": {
                  "endColumn": 10,
                  "endLine": 1,
                  "startColumn": 8,
                  "startLine": 1
                }
              }
            }
          ],
          "message": {
            "text": "UnusedImport"
          },
          "ruleId": "F401"
        },
        {
          "level": "error",
          "locations": [
            {
              "physicalLocation": {
                "artifactLocation": {...},
                "region": {
                  "endColumn": 34,
                  "endLine": 4,
                  "startColumn": 11,
                  "startLine": 4
                }
              }
            }
          ],
          "message": {
            "text": "StringDotFormatMissingArguments"
          },
          "ruleId": "F524"
        }
      ],
      "tool": {...}
    }
  ],
  "version": "2.1.0"
}

Also per the SARIF example, the message is as informative as Ruff's default (non-SARIF) one:

"message": {
  "text": "'x' is assigned a value but never used."
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions