Sonar-scanner-maven does not work inside git worktree


I use the git worktree feature to check out multiple branches of a git repository at the same time. Is sonar-scanner-maven supposed to work with worktrees and if yes, am i doing something wrong?

I want to run a sonar analysis inside one of my worktrees (since it is a worktree, it does not have a .git folder, but a .git file instead which references the actual .git folder).

Folder structure:

  • C:/my_project (Main git repo has checked out uninteresting branch, has .git folder)
  • C:/my_project_branch_foo (Worktree with the branch i want to analyse, has .git file)
  • C:/my_project_branch_foo/parent_project (Maven parent project, has pom.xml)

The .git file just contains this this line: gitdir: C:/my_project/.git/worktrees/my_project_branch_foo

When i run this command in the maven project folder:
mvn sonar:sonar "-Dsonar.projectKey=<key>" "<url>" "-Dsonar.login=<token>"
mvn sonar:sonar "-Dsonar.projectKey=<key>" "<url>" "-Dsonar.login=<token>" "-Dsonar.projectBaseDir=C:/my_project_branch_foo"

I get an error:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin: (default-cli) on project std-webapp: Unable to open Git repository: repository not found: C:\my_project\.git\worktrees\my_project_branch_foo

This gives the impression that sonar-scanner-maven at least partially supports git worktrees, C:\my_project\.git\worktrees\my_project_branch_foo does in fact exist and contains the Git metadata for my_project_branch_foo.


Analysis works on one branch at a time & is looking for a .git directory.


Ok thanks, that means that git worktrees are not supported currently. I could open up a suggestion thread for it then i guess?

1 Like


Sure. Or you could just flesh out the information requested in the Product Manager for a Day topic template here in this thread and we could move it over.


1 Like

- What are you trying to accomplish?

The git integration built into sonar-scanner currently assumes that there is a parent directory for the scanned project that has a .git directory. This is not the case if a branch is checked out as a git worktree, because then there there is .git file with a reference to the actual .git direcotry instead.

- Why does this matter to you?

It would be nice if SonarQube allowed me to analyse projects from git worktrees, because for SonarQube there should really be no semantic difference between analysing a normal local repo vs. a worktree.

- How would that look in SonarQube?

User no longer gets an error. Technically, depends how SonarQube accesses information from git. If it is by manually reading files from the .git folder, the path to those files needs to be resolved differently if inside a git worktree.

- How would we know it works well?

An automated test that checks out a sample repo and then creates a git worktree and calls sonar scanner on it would be ideal i guess.

- Why should it be a priority now?

Its probably inherently not very high priority, depending on how many people use the git worktree feature. But if sonar-scanner only reads from the .git folder, it should be a small, fairly straight-forward feature that can be implemented without any UI considerations.

1 Like