[ERROR] Unable to parse source file: A switch expression should have a default case

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    • sonarqube:9.8.0-community
  • what are you trying to achieve
    • analyzing java code from maven plugin
  • what have you tried so far to achieve this

On a multi-module maven project, if an enum is on a module and a switch expression is on a different module, sonar-maven-plugin throws an error:

mvn -q sonar:sonar 
[ERROR] Unable to parse source file : 'module2/src/main/java/org/example/module2/Foo1.java'
[ERROR] Parse error at line 8 column 17: A switch expression should have a default case

Github project to replicate: GitHub - froque/sonar-switch-parse-error

It does not happen if running maven with compile or package phase also:

mvn -q clean package sonar:sonar
1 Like

Hey @froque,
Thank you for submitting such a detailed reproducer, can you confirm if you use the sonar.jdkHome property pointing to a JDK 17 in your analysis?

I don’t use jdkHome but it does not seem affect the outcome

/opt/maven/apache-maven-3.8.7/bin/mvn -q sonar:sonar \
    -Dsonar.java.jdkHome=/usr/lib/jvm/java-17-openjdk-amd64/ \ 
    -Dsonar.url=http://localhost:9000 \
    -Dsonar.login=admin \
    -Dsonar.password=admin
[ERROR] Unable to parse source file : 'module2/src/main/java/org/example/module2/Foo1.java'
[ERROR] Parse error at line 8 column 17: A switch expression should have a default case

Thanks for clarifying, I will try to reproduce and get back to you

Hey @froque,
I think I managed to reproduce the issue you brought up.
It looks like in both cases the file cannot be parsed however but, for some reason, the error is no longer logged when we build and analyze in the same command.

Can you confirm that the parsing fails in both cases by checking whether the file displayed in SonarQube has syntax highlighting or not?

Cheers,

Dorian

When building and analyzing SonarQube has syntax highlighting for Foo1 class.

When only analyzing SonarQube does not have syntax highlighting for Foo1 class.

This has come back to light trying to discover why a unused import was not being detected. It is the same problem with maven and multi-modules.

I wasted some more time on this.

With maven 3, running mvn compile sonar:sonar works because of this hack on the maven-compile-plugin maven-compiler-plugin/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java at 3ea742c701d27a5393b62b459b22d7d99bee9694 · apache/maven-compiler-plugin · GitHub

We decided to migrate to maven 4 (alpha) that has better support for multi-modules projects. It still requires the mvn sonar:sonar to be done after an mvn package for the dependencies jar files to be present on disk.

1 Like