Error with Sonar Scanner for Dotnet : NoWorkTreeException


I am using the Sonar scanner CLI tool for Dotnet for analysis and getting the following error while performing the analysis

	ERROR: Error during SonarScanner execution
org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.sonarsource.scm.git.IncludedFilesRepository.<init>(
	at org.sonarsource.scm.git.GitIgnoreCommand.init(
	at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.index(
	at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(
	at org.sonar.core.platform.ComponentContainer.startComponents(
	at org.sonar.core.platform.ComponentContainer.execute(
	at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(
	at org.sonar.core.platform.ComponentContainer.startComponents(
	at org.sonar.core.platform.ComponentContainer.execute(
	at org.sonar.batch.bootstrapper.Batch.doExecute(
	at org.sonar.batch.bootstrapper.Batch.execute(
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.base/java.lang.reflect.Method.invoke(
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(
	at com.sun.proxy.$Proxy0.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(
	at org.sonarsource.scanner.api.EmbeddedScanner.execute(
	at org.sonarsource.scanner.cli.Main.execute(
	at org.sonarsource.scanner.cli.Main.execute(
	at org.sonarsource.scanner.cli.Main.main(

I have the .git folder along with the source folder. Below is the folder structure of the project and I am performing the analysis from /

│   ├───src
│   │   ├───serviceA
│   │   └───serviceB
│   └───tests
│       ├───serviceA
│       └───serviceB

Below is an outline of the Dockerfile used.

FROM AS base

FROM AS build

#Install open jdk 11 for SonarCloud analysis tool
RUN apt-get update && \
	apt-get install apt-utils -y && \
    apt-get install -y openjdk-11-jdk && \
    apt-get install -y ant && \
    apt-get clean;
RUN apt-get update && \
    apt-get install ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f;

ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/

COPY .git .
COPY api-services/src/serviceA/ api-services/src/serviceA/
COPY api-services/tests/serviceA/ api-services/tests/serviceA/

# SonarCloud analysis and publish
RUN dotnet tool install --global dotnet-sonarscanner

RUN dotnet restore "/api-services/src/serviceA/serviceA.csproj"

RUN /root/.dotnet/tools/dotnet-sonarscanner begin /o:"org" /k:"key" /d:sonar.scm.provider="git" /d:sonar.login="xxxx" <other analysis parameters>

RUN dotnet build "/api-services/src/serviceA/serviceA.csproj" -c Release -o /app/build
RUN dotnet test "/api-services/tests/serviceA/serviceA.test.csproj"
RUN /root/.dotnet/tools/dotnet-sonarscanner end /d:sonar.login="xxxx"

FROM build AS publish
RUN dotnet publish "/api-services/src/serviceA/serviceA.csproj" -c Release -o /app/publish

  • SonarScanner Version used -

Appreciate any assistance !

It will be great if you could provide an update on the above issue. Thanks.

Hi @VVM and welcome to the community.

Can you test executing the dotnet begin step from the folder where your .git is located ?