# SonarQube MSBuild scanner error in Git worktree

**URL:** <https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685>\
**Category:** SonarQube Server / Community Build\
**Tags:** msbuild, scanner\
**Created:** [September 18, 2018, 3:52pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685 "2018-09-18T15:52:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sly\_ratt](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@sly\_ratt](https://community.sonarsource.com/u/sly_ratt)\
**Post date:** [September 18, 2018, 3:52pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/1 "2018-09-18T15:52:11Z")

</div>

The scan fails if the source code of a git branch is checked out in a worktree. In that case “.git” is a file containing the location of the actual “.git” directory for that worktree.

SonarQube: 7.3.0.15553  
SonarQube Scanner MSBuild 3.2.0.1227  
Git plugin 1.4.1.1128

Scanner log error of Jenkins job:  
**ERROR: Error during SonarQube Scanner execution**  
**ERROR: Unable to open Git repository**  
**ERROR: Caused by: repository not found: F:\Sources\repositories\tools.git\worktrees\worktree-name**

Content of “F:\Sources\worktrees\worktree-name.git” file inside the worktree directory:

**gitdir: F:/Sources/repositories/tools/.git/worktrees/worktree-name**

The scanner has to resolve the content of the “.git” file to the actual “.git” directory.

We want to analyze multiple branches on the same machine, with worktrees we can checkout multiple branches at the same time from the same repository.  
The workaround of having multiple clones of the same Git repository is not an option because our source code is too large.

---

<div class="post-metadata">

**Author:** ![janos](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/janos/32/60_2.png) [@janos](https://community.sonarsource.com/u/janos)\
**Post date:** [September 24, 2018, 12:58pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/4 "2018-09-24T12:58:08Z")

</div>

Hi,

I took a closer look. For Git operations we’re using the JGit library, and this problem with worktrees is a [known issue](https://bugs.eclipse.org/bugs/show_bug.cgi?id=477475) in their bug tracking system. It seems a fix is in progress, with important activity this year. Once they release the fix, we can update our dependency. I’m afraid we can’t do more at this point.

I created a ticket to track this issue: [https://jira.sonarsource.com/browse/SONARSCGIT-31](https://jira.sonarsource.com/browse/SONARSCGIT-31)

Cheers,  
Janos

---

<div class="post-metadata">

**Author:** ![mrozekma](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mrozekma/32/23900_2.png) [@mrozekma](https://community.sonarsource.com/u/mrozekma)\
**Post date:** [July 13, 2021, 1:28am UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/5 "2021-07-13T01:28:00Z")

</div>

Is there no way to work around this in the meantime? JGit doesn’t seem in any hurry to fix this. You might be able to resolve the gitdir from the worktree yourself and pass that to JGit

---

<div class="post-metadata">

**Author:** ![arobinson](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/arobinson/32/29673_2.png) [@arobinson](https://community.sonarsource.com/u/arobinson)\
**Post date:** [May 25, 2022, 7:34pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/6 "2022-05-25T19:34:20Z")

</div>

This also is reproducible with the sonar VS code extension on a Mac. For a commercial product to let this go years with no solution is really poor customer service. Stop using jgit or at least provide a work-around that does not involve stopping using git worktrees.

```auto
[Info - 17:34:17.149] Analyzing 7 files...
Folder file:///Users/me/development/work/client/dev is now on branch dev_fixStuff
[Error - 17:34:17.534] Couldn't access repository for path /Users/me/development/work/client/dev
[Error - 17:34:17.535] org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: /Users/me/development/work/client/master/.git/worktrees/dev
at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:627)
at org.sonarsource.sonarlint.core.vcs.GitUtils.getRepositoryForDir(GitUtils.java:59)
at org.sonarsource.sonarlint.ls.folders.WorkspaceFolderBranchManager.lambda$didBranchNameChange$1(WorkspaceFolderBranchManager.java:80)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)

```

---

<div class="post-metadata">

**Author:** ![mrozekma](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mrozekma/32/23900_2.png) [@mrozekma](https://community.sonarsource.com/u/mrozekma)\
**Post date:** [October 5, 2022, 6:51pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/7 "2022-10-05T18:51:01Z")

</div>

It looks like this can be worked around by passing `-Dsonar.scm.disabled=true` to `sonarqube-scanner`, an option that appears to be entirely undocumented and I really wish I hadn’t had to find myself by tearing apart the source code.

---

<div class="post-metadata">

**Author:** ![vdr\_Suvelrathneswar](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/vdr_suvelrathneswar/32/42559_2.png) [@vdr\_Suvelrathneswar](https://community.sonarsource.com/u/vdr_Suvelrathneswar)\
**Post date:** [February 28, 2023, 8:00am UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/8 "2023-02-28T08:00:43Z")

</div>

Thank you. This worked for me, bypassed `java.lang.IllegalStateException: "Unable to open Git repository" `

---

<div class="post-metadata">

**Author:** ![arielman](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/arielman/32/8135_2.png) [@arielman](https://community.sonarsource.com/u/arielman)\
**Post date:** [November 11, 2024, 5:21pm UTC](https://community.sonarsource.com/t/sonarqube-msbuild-scanner-error-in-git-worktree/2685/9 "2024-11-11T17:21:40Z")

</div>

See update here:

> [@Dotnet sonarscanner fails when git repo is a worktree](https://community.sonarsource.com/t/dotnet-sonarscanner-fails-when-git-repo-is-a-worktree/118409/6):
>
> 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.
