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 AS build
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 \
    / \
    /d:sonar.login=%SONAR_TOKEN% \
  • 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.