Custom rules not shown after restarting the SonarQube instance

  • What language are you writing rules for?

A: Java

  • What have you tried, and what’s your challenge / stumbling block

A: I followed the custom rules documentation to test my custom rule but can not see any custom rules in repository section.

I have tried 3 options to generate the jar

  1. using the original example code from you guys, which completely ignored the whole process of MyFirstCustomCheck.java bcs there are already 8 custom rules
  2. following all the guidelines in the markdown, using MyFirstCustomCheck, MyFirstCustomCheckTest… etc
  3. developing my own rule

None of these above worked.

Please share the relevant code snippet, along with any error messages you’re encountering:

Option 1 and option 2 are totally the same with the example, so I provide my env parameters:

macOS Monterey 12.7.1 (21G920)

Java: amazon-corretto-17(17.0.10.7.1)

IntelliJ IDEA 2024.1.4 (Ultimate Edition) Build #IU-241.18034.62, built on June 21, 2024

maven: IntelliJ bundled version(3.9.6)

sonar-java project commit SHA for option 1: efc35ae2589aba610ae81586a315e9c2b914a4e7(branch: master)

SonarQube: Community Edition v9.9.6 (build 92038)

Using sonar.jdbc.url to connect to Postgres SQL 16.2:

sonar.jdbc.url=jdbc:postgresql://localhost:5433/sonarqube
sonar.web.host=192.168.0.39

Reproducible video for option 1: Screen Recording 2024-07-05 at 5.44.23 PM.mov - Google Drive

I also noticed that MyJavaFileCheckRegistrarTest in custom rules documentation is not the same with the current file(commit SHA: efc35ae2589aba610ae81586a315e9c2b914a4e7)

Hey there.

If you have 1 basic custom rule that you’ve implemented that isn’t showing up on your instance, the best troubleshooting step would be to share your project in this community for somebody to review.

Thanks for the reply, but my option 1 just totally used the official project, using mvn command to generate the jar directly.

There are 8 custom rules in MyJavaFileCheckRegistrarTest already:

    assertThat(context.mainRuleKeys).extracting(RuleKey::toString).containsExactly(
      "mycompany-java:SpringControllerRequestMappingEntity",
      "mycompany-java:AvoidAnnotation",
      "mycompany-java:AvoidBrandInMethodNames",
      "mycompany-java:AvoidMethodDeclaration",
      "mycompany-java:AvoidSuperClass",
      "mycompany-java:AvoidTreeList",
      "mycompany-java:AvoidMethodWithSameTypeInArgument",
      "mycompany-java:SecurityAnnotationMandatory");

I think I SHOULD SEE THE OFFICIAL 8 CUSTOM RULES appearing in the repository section (without adding my codes) then I could move forward to add my custom rule, correct me if I am wrong, thanks.

My steps(like the video I provided earlier):

  1. download official java project for custom rules
  2. ignore all the custom rules implementation steps in custom rules documentation
  3. in master branch, commit efc35ae2589aba610ae81586a315e9c2b914a4e7(like I mentioned earlier in original post), execute mvn clean install -f pom_SQ_9_9_LTS.xml in /sonar-java/docs/java-custom-rules-example by IntelliJ
  4. move the generated jar (java-custom-rules-example-1.0.0-SNAPSHOT.jar) into $SONAR_HOME/extensions/plugins
  5. restart SonarQube instance (v9.9.6 (build 92038) for -f pom_SQ_9_9_LTS.xml)
  6. just like the demo video presented, I didn’t see any custom rules in the repository section, no MyCompany Custom Repository, and of course no The Fellowship's custom rules (because I didn’t modify anything yet)

Thanks for the update.

Indeed, I tried to do the same, and found this in my web.log file.

2024.07.08 16:30:18 WARN web[][o.s.s.p.PluginRequirementsValidator] Plugin Java Custom Rules [javacustom] is ignored because the version 7.28.0.33738 of required plugin [java] is not installed

I guess we changed the version of the plugin to fix the build… but obviously it broke the example.

I’ll flag this for attention. In the meantime, if you revert the changes to the pom you see in the aforementioned PR, you should be able to proceed.

1 Like

Thanks for the suggestion, to revert the PR helped but caused other problems:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/jon/Documents/Programming/javaProject/sonar-java/docs/java-custom-rules-example/src/test/java/org/sonar/samples/java/MyJavaFileCheckRegistrarTest.java:[9,38] cannot find symbol
  symbol:   class TestCheckRegistrarContext
  location: package org.sonar.java.checks.verifier
[ERROR] /Users/jon/Documents/Programming/javaProject/sonar-java/docs/java-custom-rules-example/src/test/java/org/sonar/samples/java/MyJavaFileCheckRegistrarTest.java:[17,5] cannot find symbol
  symbol:   class TestCheckRegistrarContext
  location: class org.sonar.samples.java.MyJavaFileCheckRegistrarTest
[ERROR] /Users/jon/Documents/Programming/javaProject/sonar-java/docs/java-custom-rules-example/src/test/java/org/sonar/samples/java/MyJavaFileCheckRegistrarTest.java:[17,45] cannot find symbol
  symbol:   class TestCheckRegistrarContext
  location: class org.sonar.samples.java.MyJavaFileCheckRegistrarTest
[INFO] 3 errors 
[INFO] -------------------------------------------------------------

As I mentioned in original post, MyJavaFileCheckRegistrarTest in custom rules documentation is different from the current file(commit SHA: efc35ae2589aba610ae81586a315e9c2b914a4e7), I don’t know what’s wrong but I think the inconsistency between the docs and your code is related to those 3 errors.

After reverting MyJavaFileCheckRegistrarTest to the commit 77cdf191c283123e14f727ed2902552817da11f2 version, I can finally generate the right jar for custom rules, so I hope you can review my inference above to fix those 3 errors for other newbies who try to generate the jar in /sonar-java/docs/java-custom-rules-example.

Anyways, I can develop custom rules as expected now, I appreciate the replies.

Hello there, thanks for raising this issue.

Indeed, parts of the code and the documentation of our custom plugin example inside sonar-java are outdated and not in sync with the runtime of SQ 9.9.
We will update the pom.xml files of the sample project to be compatible with a higher version of SQ.

1 Like