Dotnet sonarscanner fails when git repo is a worktree

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:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=477475

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

Hey there.

Can you confirm what version of SonarQube you’re using?

  • Enterprise Edition
  • Version 9.9.1 (build 69595)

The error shown above seems local though. Here is the complete output from the post-processing step:

dotnet sonarscanner end -d:sonar.login="<redacted>"
SonarScanner for MSBuild 6.2
Using the .NET Core version of the Scanner for MSBuild
Post-processing started.
Calling the SonarScanner CLI...
INFO: Scanner configuration file: /home/paul/.dotnet/tools/.store/dotnet-sonarscanner/6.2.0/dotnet-sonarscanner/6.2.0/tools/netcoreapp3.1/any/sonar-scanner-5.0.1.3006/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/paul/dev/sonar-test/sonar-worktree/.sonarqube/out/sonar-project.properties
INFO: SonarScanner 5.0.1.3006
INFO: Java 21.0.1 BellSoft (64-bit)
INFO: Linux 5.15.150.1-microsoft-standard-WSL2 amd64
INFO: User cache: /home/paul/.sonar/cache
INFO: Analyzing on SonarQube server 9.9.1.69595
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=395ms
INFO: Server id: <redacted>
INFO: User cache: /home/paul/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=145ms
INFO: Load/download plugins (done) | time=313ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Process project properties (done) | time=18ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=49ms
INFO: Project key: <redacted>
INFO: Base dir: /home/paul/dev/sonar-test/sonar-worktree
INFO: Working dir: /home/paul/dev/sonar-test/sonar-worktree/.sonarqube/out/.sonar
INFO: Load project settings for component key: '<redacted>'
INFO: Load project settings for component key: '<redacted>' (done) | time=125ms
INFO: Load project branches
INFO: Load project branches (done) | time=246ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=5ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=221ms
INFO: Load active rules
INFO: Load active rules (done) | time=1622ms
INFO: Load analysis cache
INFO: Load analysis cache (4.5 kB) | time=68ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=140ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: portal-aggregator/portal/src/main/webapp/FusionCharts/src/main/webapp/FusionCharts/*, src/main/webapp/FusionCharts/firebug-lite.js, src/main/webapp/newLookUi/css/font-awesome.css, src/main/webapp/angular/assets/css/font-awesome.css
INFO:   Excluded sources for coverage: test/**, tools/**, **/obj/**, **/bin/**
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 5.714s
INFO: Final Memory: 24M/88M
INFO: ------------------------------------------------------------------------
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/sonar-test/main/.git/worktrees/sonar-worktree
        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:51:35.825  Post-processing failed. Exit code: 1

My workspace directory structure is:

  • /home/paul/dev/sonar-test/main
    ** the cloned git repo, on the main branch.
  • /home/paul/dev/sonar-test/sonar-worktree
    ** a git worktree of the cloned git repo, on the sonar-worktree branch which is identical to main.

This is how I setup all of my cloned repos and their worktrees - I have a separate worktree for each branch.

Note that the RepositoryNotFoundException from JGit has confused where the working dir is:

Correct: /home/paul/dev/sonar-test/sonar-worktree

Incorrect: /home/paul/dev/sonar-test/main/.git/worktrees/sonar-worktree

Thanks.

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.

But on the JGit side there does be some movement on this: https://eclipse.gerrithub.io/c/eclipse-jgit/jgit/+/1194900

If this does land in a JGit release, SonarQube will eventually move to this version (sooner if we have a good reason to, like maybe this!)

For now, if you’re blocked from analyzing, setting sonar.scm.disabled=true should help you move forward.

Hi @Colin
I see JGit added support for worktree in version 7 which was released at September:

  • Add worktrees read support
    when can we expect Sonar to adopt it and release version which support it?

Hey @arielman

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.

Hi Colin
Can you please make the effort to push it also for 10.8 version? it’s critical feature for many users.

Hi,

I have created a ticket to update JGit before the 10.8 release:
https://sonarsource.atlassian.net/browse/SONAR-23721

Thanks

Hi @Julien_HENRY

I saw the ticket is now close, can you tell me in which version of the gradle plugin we should expect this changes to take effect?

Thanks
Jonathan

Hi @jonathan_gafner ,

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).

Hi @Julien_HENRY

Thanks for the quick response.

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?

Thanks again

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.

@Julien_HENRY is it official that this feature will be part of 10.8 version? when this version planned official release date?

Hi @arielman,

The Fix versions in the Jira ticket^ are as official as it gets.

10.8 was released yesterday.

 
HTH,
Ann