SonarQube Extension for Azure DevOps does not delete SonarQube directories on prepare step

  • SonarQube Version: 8.9.9 Enterprise Edition
  • SonarQube Azure DevOps Extension Version: 5.4.0
  • SonarScanner for MSBuild Version: 5.5.3
  • Problem: SonarQube Prepare step (Version 5) is not deleting any previous SonarQube directories in pipeline directory
  • Current workaround: Having to delete SonarQube directory in pipeline (for example “D:\_work\1\.sonarqube”) before SonarQube prepare step

Output of prepare step:

SonarScanner for MSBuild 5.5.3
Using the .NET Framework version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
12:34:18.797 Updating build integration targets...
##[error]12:34:18.797 Failed to create an empty directory 'D:\\_work\\1\\.sonarqube\out'. Please check that there are no open or read-only files in the directory and that you have the necessary read/write permissions.
Detailed error message: Could not find a part of the path "D__work_1_s_Product_ObstructedString.ucfg".
12:34:18.797 Failed to create an empty directory 'D:\_work\1\.sonarqube\out'. Please check that there are no open or read-only files in the directory and that you have the necessary read/write permissions.
Detailed error message: Could not find a part of the path "D__work_1_s_Product_ObstructedString.ucfg"
##[error]12:34:18.797 Pre-processing failed. Exit code: 1
12:34:18.797 Pre-processing failed. Exit code: 1
##[error]The process 'D:\\_work\\_tasks\\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\\5.4.0\\classic-sonar-scanner-msbuild\\SonarScanner.MSBuild.exe' failed with exit code 1

We only have this problem since activating C/C++ analyzing with SonarQube Enterprise Edition.

Hi @echalone,

Could you share the verbose logs for the prepare step please/

In Azure DevOps you would add sonar.verbose=true to your pipeline e.g.

- task: SonarCloudPrepare@1
    inputs:
      SonarCloud: 'sonarcloud'
      organization: 'foo'
      scannerMode: 'MSBuild'
      projectKey: 'foo_sonar-scanning-someconsoleapp'
      projectName: 'sonar-scanning-someconsoleapp'
      extraProperties: |
        sonar.verbose=true

Log follows… we have more than one C/C++ project that’s getting analyzed, as context. The directories aren’t locked by any process and I can delete them without problems via a powershell buildstep just prior, so they are definitely not locked beforehand.

##[section]Starting: Prepare the SonarQube analysis
==============================================================================
Task         : Prepare Analysis Configuration
Description  : Prepare SonarQube analysis configuration
Version      : 5.4.0
Author       : sonarsource
Help         : Version: 5.4.0. [More Information](http://redirect.sonarsource.com/doc/install-configure-scanner-tfs-ts.html)
==============================================================================
[command]D:\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\5.4.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe begin /k:MyProject
SonarScanner for MSBuild 5.5.3
Using the .NET Framework version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
10:27:17.33  10:27:17.33  Loading analysis properties from D:\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\5.4.0\classic-sonar-scanner-msbuild\SonarQube.Analysis.xml
10:27:17.33  10:27:17.33  sonar.verbose=true was specified - setting the log verbosity to 'Debug'
10:27:17.33  Updating build integration targets...
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\4.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\10.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\11.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\12.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\14.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\15.0\Microsoft.Common.targets\ImportBefore
10:27:17.345  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\Build\AppData\Local\Microsoft\MSBuild\Current\Microsoft.Common.targets\ImportBefore
10:27:17.361  Installed SonarQube.Integration.targets to D:\_work\1\.sonarqube\bin\targets
10:27:17.361  Creating config and output folders...
10:27:17.361  Creating directory: D:\_work\1\.sonarqube\conf
10:27:17.361  Removing the existing directory: D:\_work\1\.sonarqube\out
##[error]10:27:17.361  Failed to create an empty directory 'D:\_work\1\.sonarqube\out'. Please check that there are no open or read-only files in the directory and that you have the necessary read/write permissions.
  Detailed error message: Could not find a part of the path "D__work_1_s_Product_MyProject_wwwroot_lib_jquery_validation_unobtrusive_jquery_validate_unobtrusive_js_288_26_FE_3751_ada_min_max_min_min_max.ucfg"
10:27:17.361  Failed to create an empty directory 'D:\_work\1\.sonarqube\out'. Please check that there are no open or read-only files in the directory and that you have the necessary read/write permissions.
  Detailed error message: Could not find a part of the path "D__work_1_s_Product_MyProject_wwwroot_lib_jquery_validation_unobtrusive_jquery_validate_unobtrusive_js_288_26_FE_3751_ada_min_max_min_min_max.ucfg"
##[error]10:27:17.361  Pre-processing failed. Exit code: 1
10:27:17.361  Pre-processing failed. Exit code: 1
##[error]The process 'D:\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\5.4.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe' failed with exit code 1
##[section]Finishing: Prepare the SonarQube analysis

Are you analyzing a mixture of C# and C/C++ files? If you aren’t analyzing C# files then you don’t need to use the SonarScanner for MSBuild - you can just use the Scanner CLI.

I’m confused by the path in the error message:

It looks like the path name has been mangled, with \ being replaced by _. I’m not sure if this is just an issue the logged output, or whether it’s a problem with the code that is doing the deletion. The SonarScanner for MSBuild is just calling System.IO.Directory.Delete on the top-level .sonarqube directory.

That was the output of the logger.
Yes we’re analyzing C# and C/C++, that’s why we need the MSBuild SonarScanner.

This should be fixed according to this post: Add support for analysis on Windows path longer than 260 characters · Issue #939 · SonarSource/sonar-scanner-msbuild · GitHub, although I’m experiencing it as well.
I’ve managed to circumvent it by having the job clean the workspace, but this slows the build down by 1m30s which is a lot.

  workspace:
    clean: all

What worked best for me was adding the following task, combined with setting LongPathsEnabled in Windows, as well as ‘Enable Win32 long paths’ in the Local Computer Policy.

    - task: PowerShell@2
      displayName: 'Clean SonarQube folder in workspace'
      inputs:
        targetType: inline
        script: |
          Write-Output "Checking that path exists: $(Pipeline.Workspace)\.sonarqube\out"
          if (Test-Path "$(Pipeline.Workspace)\.sonarqube\out") {
            Write-Output "Removing: $(Pipeline.Workspace)\.sonarqube\out"
            Remove-Item "$(Pipeline.Workspace)\.sonarqube\out\*" -Force -Recurse
            Remove-Item "$(Pipeline.Workspace)\.sonarqube\out" -Force
          }

Hey, thanks for your reply @pko!
We’re now also manually deleting this folder on each build which solves the problem. Not very clean and I wish it’d be solved by SonarQube themselves, but at least there’s a solution.
It even began screaming after SonarQube files in the user profile’s temp folder at the end, very weird. But once the folder is gone this problem too is gone.