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.