Apply custom sonar rules to SonarLint

Hi experts,

My configuration is:
Eclipse IDE for Java Developers (4.11.0)
SonarLint for Eclipse(4.3.0.12432)
Apache Maven 3.6.1
Java: 11.0.5

We plan to use SonarLint to detect and fix quality issues of our project. Our project is a multi-module Maven project, called alpha, and the IDE we use is Eclipse/IntelliJ.

We have created a plugin which includes a bunch of Sonar Java custom rules for alpha, called alpha-custom-java-rules, and right now I would like to apply these rules to SonarLint.

What I have done so far is that I have activated SonarLint support via changing pom.xml of alpha, and have used

-> mvn clean install -U

to do the whole compilation and packaging work.

<plugins>
	[...]
	<plugin>
	       <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
	       <artifactId>sonar-packaging-maven-plugin</artifactId>
	       <version>${sonar.packaging.maven.plugin.version}</version>
               <extensions>true</extensions>
	       <configuration>
		       <pluginKey>alpha-custom-java-rules</pluginKey>
		       <pluginName>Alpha Custom Java Rules</pluginName>
		       <pluginClass>com.sonarqube.java.JavaCustomRulesPlugin</pluginClass>
		       <sonarLintSupported>true</sonarLintSupported>
		       <sonarQubeMinVersion>5.6</sonarQubeMinVersion>
	       </configuration>
	</plugin>
	[...]
</plugins>

The following screenshot is the Sonarlint configuration UI in Eclipse

My question is: In Eclipse/IntelliJ, how can I verify if these custom rules take effect in SonarLint?

Your reply will be highly appreciated.

Hello,

Custom Java rules should be executed by SonarLint if you use SonarLint in connected mode with a SonarQube server that has your alpha-custom-java-rules plugin installed, and the custom rules don’t rely upon a custom sensor.

Hope this helps!

2 Likes

Thank you a lot for your answer.

Via checking the test connection from Eclipse, I have successfully bind the project alpha to the SonarQube server where the alpha-custom-java-rules installed. I think, at this point, I have established a connection between SonarQube server and SonarLint.

However, when I run the SonarLint to analyze a changed file, the rules defined in alpha-custom-java-rules haven’t applied

I also searched a rule defined in alpha-custom-java-rules

It says “when a project is connected to a SonarQube/SonarCloud server, configuration from the server applies”
but I didn’t manage to find any rules from the “`alpha-custom-java-rules” plugin.

Indeed, in connected mode, all analysis settings come from the project’s configuration on the server side.

This means that you will want to check the following points:

  • your alpha project exists on the server
  • the Quality Profile associated to the project on the server has the custom rule(s) enabled
  • relevant issues are found by the custom rule(s) on the server, if the project has already been analyzed
  • your Eclipse project is bound to the right project from the server

What can be misleading is that the “Rules Configuration” panel in Eclipse only shows rules available in the analyzers embedded in your version of SonarLint, and does not reflect server side settings.

1 Like

Hi, I have a problem similar to this topic but wanting to import plugin rules instead of custom rules.
I have 15 rules in a quality profile established on the SonarQube server, 12 being from the repository that SonarLint does find, but the other 3 from plugins that do not appear in Rule Configurations.
I have not understood this answer very well. Could you elaborate a bit more on the answer of why they do not appear or if I can put them in the configuration?
(I’m using Eclipse too)

Thanks for your attention and greetings.

Hello, thank you for your question.

I suggest that you open a new thread with as much information about your setup as possible - e.g the version of your SonarQube server and the list of plugins installed on it.

Please note that the “Rules Configuration” pane in Eclipse only reflects the local settings and not the ones that come from the server.

Hi there,

I’m reading this message and i would like to comment somthing about what you said: “in connected mode, all analysis settings comes from the project’s configuration on the server side”

Well, that’s actually not true. Properties “sonar.projectKey” and “sonar.projectName” are not being passed as analysis settings. I found especially frustrating the first one.

At runtime, if we call in a rule to JavaFileScannerContext.getProject (). Key (), the method returns “sonarlint” instead the real projectKey. So, in order to know the projectKey in the context of a rule execution the only solution we have nowadays is that the user MUST configure again and manually the sonar.projectKey in the SonarLint preferences / analysis properties page. This is an inconsistency because in connected mode SonarLint already konws the true sonar.projectKey and sonar.projectName but these properties are being ignored when analysis properties are being merged or being calculated (sonar.java.libraries, target / source versions, etc …).

And worse, even when i configure sonar.projectKey, invoking JavaFileScannerContext.getProject (). Key () still returns “sonarlint”.

I think there is no reason not to pass sonar.projectKey / sonar.projectName to the configuration object.

Greetings.

Hi Jesus,

I was not aware that SonarJava custom rule API would expose such information. Can you give us more insight of why you need the project key to write a custom rule?