Dotnet analysis fails on TeamCity - Error during SonarScanner execution: Not inside a Git work tree

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension): Community Edition, Version 9.9 (build 65466)
  • how is SonarQube deployed: installed from zip
  • what are you trying to achieve: run SonarQube .NET analysis
  • what have you tried so far to achieve this: it worked, stopped working after SonarScanner for .NET upgrade. Currently using sonar-scanner-4.8.0.2856 for .NET 5

I use SonarQube .NET Scanner on TeamCity. The analysis has 3 steps:

  1. begin the analysis:
    dotnet C:\SonarQubeDotNetScanner\SonarScanner.MSBuild.dll begin /k:“ProjectKey” /d:sonar.login=“3fbxxxxxxxxxxxxca9fe1e30e3681a2ad5dee”

  2. build the solution with MSBuild

  3. end the analysis:
    dotnet C:\SonarQubeDotNetScanner\SonarScanner.MSBuild.dll end /d:sonar.login=“3fbxxxxxxxxxxxxca9fe1e30e3681a2ad5dee”

So far, everything worked fine. Few weeks ago I updated SonarScanner for .NET to version 4.8.0.2856 and soon after the analysis end step in TeamCity fails with the following error message:

ERROR: Error during SonarScanner execution
ERROR: Not inside a Git work tree: C:\TeamCity\buildAgent\work\d2e70520a04078d9

It has always worked and we didn’t change anything related to git repo settings.

The fact is that the workspace folder on the machine doesn’t have .git folder inside, but it never has had it and it worked.

I found this thread: SonarQube Scanner fails with "Not inside a git worktree" error
and I tried to add sonar.projectBaseDir to the start step:

dotnet C:\SonarQubeDotNetScanner\SonarScanner.MSBuild.dll begin /k:“ProjectKey” /d:sonar.login=“3fbxxxxxxxxxxxxca9fe1e30e3681a2ad5dee” /d:sonar.projectBaseDir=%teamcity.build.checkoutDir%

but it doesn’t help, I’m getting the same error:

ERROR: Not inside a Git work tree: C:\TeamCity\buildAgent\work\d2e70520a04078d9

I tried adding this parameter /d:sonar.projectBaseDir=%teamcity.build.checkoutDir%* to the end step, as this step actually fails, but then I got the following error:

This setting is not valid in the "end" phase in this version of the C# plugin: sonar.projectBaseDir

Can you please help me here?

Why did you choose this version? The latest version is v5.13, with v4.8 being released all the way back in November 2019.

Hi Colin,
actually, the package I downloaded is called “sonar-scanner-msbuild-5.13.0.66756-net5.0”, so it is v5.13. What mislead me is that the folder inside is called “sonar-scanner-4.8.0.2856”. So this is the version I am using.

Thanks.

For the moment, I suggest removing this:

And focusing on this:

Now you mentioned upgrading the SonarScanner for .NET, did you also upgrade SonarQube lately? Or has it always been working before on SonarQube v9.9 specifically?

Actually, I’ve also upgraded SonarQube recently. The current version is Community EditionVersion 9.9 (build 65466)
However, after upgrading only SonarQube, it worked fine.

I also tried adding sonar.scm.provider="git" to the analysis start step:

dotnet C:\SonarQubeDotNetScanner\SonarScanner.MSBuild.dll begin /k:“ProjectKey” /d:sonar.login=“3fbxxxxxxxxxxxxca9fe1e30e3681a2ad5dee” /d:sonar.scm.provider=“git”

but it changes nothing.

The fact is that the code in the working directory does not have .git folder, but it never has.

Another update: I tried switching do .NET Global Tool, but still the same error during analysis end step:

ERROR: Error during SonarScanner execution
ERROR: Not inside a Git work tree: C:\TeamCity\buildAgent\work\d2e70520a04078d9

The only workaround I found is to disable SCM Sensor analysis:

with it disabled, the analysis works.

I wonder if a Git folder that you don’t know about is somehow in the repository.

Can you run an ls -la of the directory (C:\TeamCity\buildAgent\work\d2e70520a04078d9) before the analysis starts?

The machine is on Windows, but sending you the ls output:

Directory: C:\TeamCity\buildAgent\work\d2e70520a04078d9

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        26-4-2023     04:00                .config
d-----        26-4-2023     04:00                .github
d-----        26-4-2023     04:00                .husky
d-----        26-4-2023     04:00                .rider-run
d-----        26-4-2023     07:18                .sonarqube
d-----        26-4-2023     04:00                .vscode
d-----        26-4-2023     07:26                ActiveDirectoryTester
d-----        26-4-2023     04:00                Assemblies
d-----        26-4-2023     04:00                Content
d-----        26-4-2023     07:22                MM.Administrator
d-----        26-4-2023     04:07                MM.Authenticator
d-----        26-4-2023     04:01                MM.Common
d-----        26-4-2023     04:03                MM.Common.Clients
d-----        26-4-2023     04:01                MM.Common.Resources
d-----        26-4-2023     04:04                MM.Common.Security
d-----        26-4-2023     04:01                MM.Core
d-----        26-4-2023     04:04                MM.DataAccessLayer
d-----        26-4-2023     04:07                MM.DbTools
d-----        26-4-2023     04:04                MM.DocumentsEngine
d-----        26-4-2023     04:02                MM.DocumentSystem
d-----        26-4-2023     04:02                MM.DomainObjects
d-----        26-4-2023     04:03                MM.OfflinePublication.Client
d-----        26-4-2023     04:04                MM.Server.Common
d-----        26-4-2023     04:08                MM.Server.Web
d-----        26-4-2023     04:01                MM.Validators
d-----        26-4-2023     04:17                MM.Web
d-----        26-4-2023     04:08                MM.Web.Api
d-----        26-4-2023     04:08                MM.Web.Api.Client
d-----        26-4-2023     04:08                MM.Web.Api.Common
d-----        26-4-2023     04:23                MM.Web.Saas
d-----        26-4-2023     04:05                MM.WebAccess.Search
d-----        26-4-2023     04:07                MM.WebEdit
d-----        26-4-2023     07:20                MM.WPF.Common
d-----        26-4-2023     07:21                MM.WPF.Controls
d-----        26-4-2023     04:02                MM.WPF.Core
d-----        26-4-2023     04:03                MM.WPF.Resources
d-----        26-4-2023     04:00                MigrationBackup
d-----        26-4-2023     04:00                MM_packages
d-----        26-4-2023     04:01                NSspi
d-----        26-4-2023     04:07                OfficeAutomationTester
d-----        26-4-2023     04:00                packages
d-----        26-4-2023     04:00                SyncClient
d-----        26-4-2023     04:00                Tests
d-----        26-4-2023     04:00                Tools
-a----        26-4-2023     04:00             66 .csharpierrc.json
-a----        26-4-2023     04:00           8516 .editorconfig
-a----        26-4-2023     04:00            483 .gitattributes
-a----        26-4-2023     04:00           3067 .gitignore
-a----        26-4-2023     04:00            124 build_js.bat
-a----        26-4-2023     04:00          22822 Directory.Packages.props
-a----        26-4-2023     04:00            623 full.msbuild
-a----        26-4-2023     04:00           1300 License.txt
-a----        26-4-2023     04:00              0 licenses.licx
-a----        26-4-2023     04:00         201250 MM.sln
-a----        26-4-2023     04:00          12148 MM.sln.DotSettings
-a----        26-4-2023     04:00           1041 NuGet.Config
-a----        26-4-2023     04:00            102 package-lock.json
-a----        26-4-2023     04:00             38 README.md
-a----        26-4-2023     04:00          12770 release_script.build
-a----        26-4-2023     04:00          23093 release_scriptDev2.build
-a----        26-4-2023     04:00            623 uitests.msbuild

So I’m a little confused, your code is hosted on GitHub, you have a .gitignore and .gitattributes file… but this isn’t a Git repository?

1 Like

This was actually a very good remark :slightly_smiling_face: It IS a git repository, but TeamCity didn’t create the .git folder when checking out the repository. I found out that it was related to checkout settings in TeamCity: msbuild - How do I get TeamCity to create the .git directory when cloning a repo for build? - Stack Overflow

After changing it, the .git folder is back and the analysis seems to work again. Thanks for that tip!

Anyway, we have never had this .git folder there, so something must have changed in recent SonarQube/SonarScanner versions that it started to be a problem for the analysis.
I noticed that now, the automatic issues assignation works while it didn’t work before. Apparently, when it was enabled in SonarQube, but there was no .git folder, it was simply ignored and not performed. Now when it’s enabled, but .git folder is not present, it results in an error during analysis.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.