Unused imports are not detected by sonarqube in a Java project.
I found a weird bug where adding a line of Java code makes sonarqube unable to detect unused imports.
How to reproduce:
Start sonarqube
docker run --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:9.6-community
Clone a java spring-boot project containing unused imports
I found the problem but no solution. The problem is lombok’s @Data annotation.
My java class HomeMessage.java contains @Data annotation for generating getters and setters. Because in HomeController I’m calling the method .getMessage(), which is generated by lombok, the unused imports are not detected.
I also tried to set only @Getter@Setter instead of @Data but no luck.
Thanks for the message and sorry for the late reply. I’m currently investigating the issue. There is indeed strange behavior in our Lombok filter. I already have a couple of ideas so will keep you updated.
Thanks for the feedback. I was able to reproduce such behavior. It looks like after we enabled the batch mode by default The Unused Import rule stopped working with the Lombok. So here is the ticket to handle this issue: https://sonarsource.atlassian.net/browse/SONARJAVA-4313
For now, you can try disabling the batch mode and check if issues appear. You can do it by setting the property sonar.java.fileByFile=true. Note that this solution is ad-hoc and might influence the performance of the analysis.
What do you mean by “doesn’t work”? Is the issue still there, or analysis is failing?
Anyway, the ticket is now in our backlog and we will work on this sometime in the future. As mentioned before setting this property is not a solution and should be avoided unless absolutely necessary.
The issue is still there. There is a slight improvement when using fileByFile, but only a few unused imports are detected in the project. More than 90% of the unused imports are still not detected.