Scanning fails with the following error when the git repo being scanned is a git worktree:
ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: Unable to open Git repository
at org.sonar.scm.git.JGitUtils.buildRepository(JGitUtils.java:41)
at org.sonar.scm.git.IncludedFilesRepository.indexFiles(IncludedFilesRepository.java:50)
at org.sonar.scm.git.IncludedFilesRepository.<init>(IncludedFilesRepository.java:41)
at org.sonar.scm.git.GitIgnoreCommand.init(GitIgnoreCommand.java:37)
at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.index(ProjectFileIndexer.java:114)
at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:363)
at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137)
at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at jdk.proxy1/jdk.proxy1.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
at org.sonarsource.scanner.cli.Main.execute(Main.java:126)
at org.sonarsource.scanner.cli.Main.execute(Main.java:81)
at org.sonarsource.scanner.cli.Main.main(Main.java:62)
Caused by: org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: /home/paul/dev/myproject/main/.git/worktrees/mybranch
at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:627)
at org.sonar.scm.git.JGitUtils.buildRepository(JGitUtils.java:34)
... 22 more
ERROR:
The SonarScanner did not complete successfully
12:22:40.428 Post-processing failed. Exit code: 1
Scanning the same repo works fine when git worktrees are not used.
I believe this is a problem with the underlying jgit library you have chosen to use:
This forum discussion seems to have gotten some traction back in 2019:
Its now 2024 and sonarscanner still can’t handle code in a git worktree.
$ dotnet tool list -g dotnet-sonarscanner
Package Id Version Commands
---------------------------------------------------------
dotnet-sonarscanner 6.2.0 dotnet-sonarscanner
While we don’t always use JGit to blame files, we do use it to index files. And if JGit isn’t supporting worktrees… I’m not sure we can do much about it.
Likely it will get updated as part of our normal dependency update process. That being said, I think we’ve already done that for v10.8 and JGit wasn’t on the list. I’ll flag this for attention.
The issue is in the Scanner Engine (a part of the Scanner that is dynamically provisioned from SonarQube during each analysis). So this is not related to the version of the SonarScanner for Gradle.
To get the fix, you will have to update to SonarQube 10.8 (the release is planned for early December if I am not mistaken).
I got the same issue when running the gradle sonar task with the exact same problem.
I am working on a git worktree and not a git repo, but when switching to a normal clone the task is running, so I think that under the hood the plugin is using the old JGit dependency.
Should I open a new ticket or there is an option to notify the relevant team?
The Sonar Gradle plugin is provisionning/bootstrapping the Scanner Engine from your SonarQube server (you can think of it like the Gradle wrapper gradlew, that is a thin layer capable of downloading the actual Gradle executable).
In short, the Sonar Gradle plugin does not have any issues. The only solution is to update SonarQube once it is released.