Sonarscan during Docker build fails to find local application data directory

  • version used: dotnet-sonarscanner v5.3.1
  • error observed:
Unhandled exception. System.IO.IOException: Cannot find local application data directory.
   at SonarScanner.MSBuild.Common.MsBuildPathSettings.GetImportBeforePaths()
   at SonarScanner.MSBuild.PreProcessor.TargetsInstaller.InternalCopyTargetsFile()
   at SonarScanner.MSBuild.PreProcessor.TargetsInstaller.InstallLoaderTargets(String workDirectory)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.InstallLoaderTargets(ProcessedArgs args)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.DoExecute(ProcessedArgs localSettings)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.Execute(String[] args)
   at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
   at SonarScanner.MSBuild.BootstrapperClass.Execute()
   at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
   at SonarScanner.MSBuild.Program.Execute(String[] args)
   at SonarScanner.MSBuild.Program.Main(String[] args)
   at SonarScanner.MSBuild.Program.<Main>(String[] args)
  • steps to reproduce:
    • Create a dockerfile that contains’:
FROM mcr.microsoft.com/dotnet/sdk:5.0-nanoserver-2004 AS build
WORKDIR /src
ARG RUN_STATIC_CODE_ANALYSIS=false
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_ORGANIZATION="test-organization"
ARG SONAR_TOKEN="test-token"
ARG SONAR_PROJECT_KEY="test-project-key"
ARG SONAR_PROJECT_NAME="test-project-name"

# Install Sonar Scanner and Coverlet
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet tool install --global coverlet.console
RUN setx PATH "C:\Users\ContainerUser\.dotnet\tools;%PATH%"

RUN dotnet sonarscanner begin \
    /k:%SONAR_PROJECT_KEY% \
    /n:%SONAR_PROJECT_NAME% \
    /o:%SONAR_ORGANIZATION% \
    /d:sonar.host.url=%SONAR_HOST_URL% \
    /d:sonar.login=%SONAR_TOKEN% \
    /d:sonar.cs.opencover.reportsPaths=/coverage.opencover.xml
  • Build the dockerfile

TL;DR Don’t use nanoserver. For DotNet 5, there’s a new servercore based image that will work correctly.

The reason that this is failing is because nanoserver system calls return empty strings for all system paths. The call is separate from the environment variables, which actually has the local application data path even in nanoserver. There’s a GitHub issue for this problem that could make Sonar Scanner for MSBuild work correctly, but it hasn’t seen any traction, so the best course of action is to use a servercore based image instead of the nanoserver images, which are unfortunately the default.

Thanks. I’m aware of the GitHub issue as I’ve commented there, as did you I noticed :wink:

I’ll see if I can get it working with the servercore based image.

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