Incremental PR analysis

I’m trying to add support for incremental PR analysis in a language plugin. It looks like it was implemented in the Kotlin plugin in this commit

It is using SensorContext#canSkipUnchangedFiles, whose Javadoc says:

In some situations, there is no need to analyze unchanged files, as reported by InputFile.status() as InputFile.Status.SAME, and analyzers can skip reporting any data for those files. Plugins should be prepared for the flag to be always false in the future.
Since 9.4

The first part of the doc makes me think that if this flag is true, and InputFile.Status is SAME, then I can skip the analysis of this file. But what does the second part means ? Is that deprecated ?

I’ve changed a couple lines in the main sensor of my plugin to display the file status, and canSkipUnchangedFiles. The file status seems to match the files modified in the Git branch. Would that work with a different SCM ? Or does that rely on the Git plugin to flag some files to be unchanged ?

It also says that the method was introduced in 9.4. In a more general way, what is the minimum SonarQube version that is required ? And any requirement on sonar-scanner version ?


Hi @gquerret,
Thanks for your question. My understanding is that the status() method is not considered reliable enough for incremental analysis purposes. The change status of a file should be decided according to the analyzer’s focus (different hash, different effective AST, dependence on a file that has changed, …).

Essentially, the most future-proof thing to do to check a file status is to rely on your own method and not the status method(). Similar modifications are underway or have been applied in our own analyzers.



I think the main question for a plugin developer is to know which files have changed since the previous analysis. Based on the list of changed files, it is possible to limit the scope of the analysis. As an example, the language I’m working with has the concept of include files (with include parameters), so if an include file is flagged as modified, I have to scan all files referencing this include file. This is the responsibility of the plugin, as the rules will be different per language.
What can’t be done by the plugin (at least not easily) is to know what has changed, as that will depend on the SCM and the previous baseline. Having this information in InputFile.status() would be really useful.

Does that also work only in the Developer Edition with branch support ? Or also with the community edition when scanning the main branch ?

Hi @gquerret,

Indeed, the ideal way to determine if a file has changed should be to rely on InputFile.status() and I think you can rely on it for now without fearing a big change in the future.

Can I ask you, in which version of the API did you see the comment?

Hi @Dorian_Burihabwa,

Can I ask you, in which version of the API did you see the comment?

In sonar-plugin-api

Thanks for the info.

As I said before, it is unlikely that canSkipUnchangedFiles changes its behavior in the near future. It would probably be preceded by a big change in the products, a deprecation, and a replacement by a new API.
However, because it is mostly used in the context of PR analysis, you should not expect to rely on this method for non-commercial editions of SonarQube.

For inputFile.status(), the method should work with all the SCMs supported by sonar.

As far as the version of the scanner is concerned, I am not aware of any constraints. I would advise staying on the latest version (they don’t change too much anyway). But as long as they can connect to a version of SonarQube that implements the API (9.4+), the users should be good to go.

Hope this helps,