Search code examples
pythonsonarqube

How to analyze only .py files in SonarQube properties file?


I am using SonarQube for my work project. We have a repository with a lot of files and folders in our project. I want to filter out only the python files (.py files).

My goal is to analyze only the .py files in the folder repo/src.

I tried using the wildcards, but to no avail.

Here are the two things I tried :

sonar.sources=repo/src/**/*.py
sonar.sources=repo/src/**.py

The option of just passing the whole folder where the code is located (repo/src/) works, but the plan my work pays for SonarQube doesn't allow us to analyze the whole project, hence why I want to analyze only python files.

What am I doing wrong? Can someone let me know what I am misunderstanding here.


Solution

  • Try specifying sonar.exclusions in addition to sonar.sources.

    Before, there was sonar.language, which you could set to py for Python:

    Set the language of the source code to analyze. Browse the Plugin Library page to get the list of all available languages. If not set, a multi-language analysis will be triggered

    But that has long been deprecated and now does nothing in latest versions. From a thread on the Sonarqube forums, What is the alternative to the sonar.language property in the current version of sonarqube?:

    The alternative is to just not use it.

    ...

    Now if, on the other hand, you want to prevent other languages in the project from being analyzed, then you should do one of these things:

    • configure sonar.sources not to include those files, assuming this can be done conveniently
    • set exclusions
    • as a last-ditch hack, there’s also the option of configuring language file suffixes at the project level to not recognize the other languages’ files (e.g. set the Java file suffix to .foo)

    A reply on that same thread said that this worked for them:

    so in my case the exclusions property looks like this

    sonar.exclusions=**/*.java,**/*.jar
    

    So, first, change sonar.sources to specify directories.

    Comma-separated paths to directories containing main source files.

    sonar.sources=repo/src
    

    Then specify sonar.exclusions for the file extensions to ignore.

    We have the same setup in our projects:

    sonar.sources=app
    sonar.exclusions=**/*.html,**/*.js
    

    You can check from the sonar scanner logs if some files were excluded:

    INFO: Indexing files...
    INFO: Project configuration:
    INFO:   Excluded sources: **/*.html,**/*.js
    INFO:   Excluded sources for coverage: tests/*
    INFO: 266 files indexed
    INFO: 24 files ignored because of inclusion/exclusion patterns
    INFO: 1 file ignored because of scm ignore settings
    ...
    

    (We are using SonarQube server 9.5.0.56709, SonarScanner 4.3.0.2102)