[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

We are using GitHub - SonarSource/sonar-scanner-cli: Scanner CLI for SonarQube and SonarCloud and having the same error. Would you have a hint how to fix the problem using the CLI?

how can I reproduce your problem ?

Thanks Filipe, unfortunately, I don’t have a public, minimal example at hand, but I would assume an exhaustive switch expression over an enum combined with a pipeline that first builds the project using gradle (gradleExecuteBuild - Project "Piper": Continuous Delivery for the SAP Ecosystem) and then triggers sonar scan (sonarExecuteScan - Project "Piper": Continuous Delivery for the SAP Ecosystem) in a preceding step would be an approach.

I tried to reproduce it with GitHub - froque/sonar-switch-parse-error and GitHub - SonarSource/sonar-scanner-cli: Scanner CLI for SonarQube and SonarCloud and could not. Can not help you if I can not reproduce it

public class DummyClass {

	public static DummyEnum test(final DummyEnum dummy) {
		return switch (dummy) {
			case VAL1 -> DummyEnum.VAL1;
			case VAL2 -> DummyEnum.VAL2;
		};
	}
}

This class leads to the following log:

error sonarExecuteScan - ERROR: Unable to parse source file : 'DummyClass.java'
error sonarExecuteScan - ERROR: Parse error at line 11 column 17: A switch expression should have a default case

Interestingly,

public enum DummyEnum {

	VAL1, VAL2;

	public static String test(final DummyEnum dummy) {
		return switch (dummy) {
			case VAL1 -> "val1";
			case VAL2 -> "val2";
		};
	}
}

does not lead to the error.

I am using
Java 17, Gradle 8
SonarQube 9.9.4
Scanner CLI 5

Could you share your build script using the scanner cli?

docker run -d --rm \
    --name sonarqube \
    -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true \
    -p 9000:9000 \
    sonarqube:9.8.0-community

git clone git@github.com:froque/sonar-switch-parse-error.git
cd sonar-switch-parse-error
/opt/maven/apache-maven-3.8.7/bin/mvn -q clean package

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip
unzip sonar-scanner-cli-5.0.1.3006-linux.zip

sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner \
    -Dsonar.host.url=http://localhost:9000 \
    -Dsonar.login=admin \
    -Dsonar.password=admin \
    -Dsonar.projectKey=sonar-switch-parse-error \
    -Dsonar.java.binaries="*/target/*" 

I tried

./gradlew build
cd ..
sonar-scanner-5.0.1.3006-macosx/bin/sonar-scanner 
    -Dsonar.host.url=http://localhost:9000 \
    -Dsonar.login=admin \
    -Dsonar.password=admin \
    -Dsonar.projectKey=sonar-switch-parse-error \
    -Dsonar.sources=my-service/ \
    -Dsonar.java.binaries="my-service/*/build/*"

and get the same errors in the log. So the culprit seems to be related to the interplay of gradle and sonar scanner.

Please note that the project is a multi-module gradle project.