Importing mypy report into SonarCloud from pre-commit

I am having issues getting sonar to import the results from mypy into sonar I found this which describes the format but not how to get the format out of mypy

My .pre-commit-config.yaml:

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.5.1
    hooks:
      - id: mypy
        exclude: ^tests/
        additional_dependencies:
          - lxml
          - types-pyyaml
          - types-setuptools
          - pydantic<2
          - types-requests

The mypy configuration section of pyproject.toml (I have the xml as I was desperately going through all formats)

[tool.mypy]
exclude = ["tests"]
python_version = "3.11"
namespace_packages = true
strict = true
warn_unused_configs = true
show_error_codes = true
explicit_package_bases = true
plugins = [ "pydantic.mypy",]
junit_xml = "mypy.xml"
  • ALM: GitHub

  • CI system used: GitHub Actions

  • Scanner command used when applicable (private details masked)

  • Languages of the repository: python

  • Error observed (wrap logs/code around with triple quotes ``` for proper formatting)

    • The mypy file is not ingested and shown as an issue in sonar.
  • Steps to reproduce

  • Run pre-commit run --all-files and see the errors logged to stdout and not be able to ingest those errors into sonar

Hey there.

You should be able to pipe these all to a file. It would look something like this.

Hi Colin,

I have gotten it running with a combination of the following. To the command that runs pre-commit in GitHub Actions: pre-commit run --all-files --color never then to the .pre-commit-config.yaml I added the following:

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.5.1
    hooks:
      - id: mypy
        additional_dependencies:
          - lxml
          - types-pyyaml
          - types-setuptools
          - pydantic<2
          - types-requests
        log_file: "mypy_log.txt"

There are actually two jobs one for ruff and one for mypy and they upload a “mypy_log.txt” and a “ruff_log.txt” those are uploaded with the actions/upload-artifact@v3 and then downloaded with the actions/download-artifact@v3 in another workflow for just sonarcloud that runs and I see it finding the files but I do not see it actually importing any issues:

the job that runs the sonarcloud GitHub Action “job”:

  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest
    if: always()
    needs:
      - static-analysis
      - unit-testing
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 

      - name: Download code coverage artifact
        uses: actions/download-artifact@v3
        with:
          name: coverage.xml

      - name: Download ruff report artifact
        uses: actions/download-artifact@v3
        with:
          name: ruff_log.txt

      - name: Download mypy report artifact
        uses: actions/download-artifact@v3
        with:
          name: mypy_log.txt

      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
INFO: Sensor Python Sensor [python]
INFO: Using cached data to retrieve global symbols.
INFO: Cached information of global symbols will be used for 148 out of 148 main files. Global symbols will be recomputed for the remaining files.
INFO: Fully optimized analysis can be performed for 259 out of 259 files.
INFO: Partially optimized analysis can be performed for 259 out of 259 files.
INFO: Starting global symbols computation
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Starting rules execution
INFO: 259 source files to be analyzed
INFO: 259/259 source files have been analyzed
INFO: The Python analyzer was able to leverage cached data from previous analyses for 259 out of 259 files. These files were not parsed.
INFO: Sensor Python Sensor [python] (done) | time=1616ms
INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: Python test coverage
INFO: Parsing report '/github/workspace/coverage.xml'
ERROR: Cannot resolve the file path '<file-path>.py' of the coverage report, the file does not exist in all 'source'.
ERROR: Cannot resolve 3 file paths, ignoring coverage measures for those files
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=285ms
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=55ms
INFO: Sensor Import of Mypy issues [python]
INFO: Sensor Import of Mypy issues [python] (done) | time=103ms
INFO: Sensor Import of Ruff issues [python]
INFO: Importing /github/workspace/ruff_log.txt
ERROR: No issues information will be saved as the report file '/github/workspace/ruff_log.txt' can't be read. ParseException: null
INFO: Sensor Import of Ruff issues [python] (done) | time=10ms

The two files look very close to what you suggest ending in:

ruff_log.txt: Found 251 errors.
mypy_log.txt: Found 353 errors in 106 files (checked 109 source files)

so there are errors that should be loaded but I cannot see them anywhere in sonarcloud.

The ERROR: No issues information will be saved as the report file '/github/workspace/ruff_log.txt' can't be read. ParseException: null tells me that there is a reaason the ruff log might not be parsed but I should at least be seeing the mypy issues.

The ruff format should look like this.

I don’t think we’ve done a good job documenting the expected formats for these reports, so I’ll ping our Docs team.

Hi Colin (it won’t let me tag you)

I have gotten the mypy side of it working and now I am just working to update to get the ruff stuff. I am working on getting that file generated right now I am using the following:

This is a section of our GitHub actions:

      # The --exit-zero is because the pre-commit run catches the errors this is only for report generation
      - name: Generate Ruff report
        if: always()
        run: |
          pip install ruff
          ruff --exit-zero --output-file ruff_report.json --output-format json ./<namespace> ./tests

Is this the same general command you used to generate the file you sent me.

The issue could be related to this one that I opened in ruff

Error from the sonarcloud GitHub Action Job with obvious redaction of namespaces, module names and file name, I also updated the warning to put each file on a separate line as it was a giant blob and a bit unreadable

INFO: Importing /github/workspace/ruff_report.json
WARN: Failed to resolve 27 file path(s) in Ruff report. No issues imported related to file(s): /home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file0>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file1>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file2>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file3>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file4>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file5>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file6>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file7>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file8>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file9>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file10>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file11>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file12>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file13>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file14>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file15>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file16>.py;
/home/runner/work/<library-name>/<library-name>/<namespace>/<module-name>/<file16>.py;
/home/runner/work/<library-name>/<library-name>/tests/conftest.py;
/home/runner/work/<library-name>/<library-name>/tests/<namespace>/<module-name>/<file17>.py;...
INFO: Sensor Import of Ruff issues [python] (done) | time=19ms

Hello @matroscoe,

Thank you for reporting this issue and for the detailed explanation! There is an issue as we try to resolve two different absolute paths. I have created this ticket in order to track our progress on this task.

Sorry for the inconvenience.

David

Not a problem, I have also opened a ticket with ruff as well, it would be ideal if it could deal with both but having both sides of the problem deal with it would be amazing! Feature Request: Ability to set relative python file paths in the reports · Issue #7551 · astral-sh/ruff · GitHub