Support for .NET File-based apps

Microsoft has a newer type of project you can do called File-based apps. They don’t have a .csproj file. These are very handy for very small minimal APIs with Native AOT publishing.

Currently if I run my build in an Azure DevOps pipeline with the following steps:

  - task: SonarQubePrepare@7
    displayName: SonarQube Prepare
    inputs:
      SonarQube: ${{ parameters.sonarqubeServiceConnection }}
      scannerMode: 'dotnet'
      projectKey: ${{ parameters.sonarqubeProjectKey }}

  - task: PowerShell@2
    displayName: Publish File-Based App
    inputs:
      targetType: inline
      pwsh: true
      script: |
        $ErrorActionPreference = 'Stop'

        & dotnet publish $(Build.SourcesDirectory)/Program.cs `
          -c "$(BuildConfiguration)" `
          -o "$(Build.StagingDirectory)\publish" `
          --no-restore `
          ${{ parameters.msBuildExtraParameters }}

        if ($LASTEXITCODE -ne 0) {
          exit $LASTEXITCODE
        }

  - task: SonarQubeAnalyze@7
    displayName: SonarQube Analyze

  - task: SonarQubePublish@7
    displayName: SonarQube Publish

I get this output from my publish step which does include SonarQube warnings indicating that the scanner is able to scan my .cs file(s).

D:\azagent01\_work\822\s\Program.cs(1,1): warning S3776: Refactor this top-level file to reduce its Cognitive Complexity from 52 to the 30 allowed.

D:\azagent01\_work\822\s\Program.cs(30,6): warning S1192: Define a constant instead of using this literal ‘Test’ 8 times.

Sonar: (Program.cs.csproj) Project processed successfully

Generating native code

Program.cs → D:\azagent01\_work\822\a\publish\

But then in the SonarQubeAnalyze step I get this output:

Starting: SonarQube Analyze

==============================================================================

Task : Run Code Analysis

Description : Run scanner and upload the results to the SonarQube Server.

Version : 7.4.2

Author : sonarsource

Help : This task is not needed for Maven and Gradle projects since the scanner should be run as part of the build.

[More Information]( Azure DevOps Extension | SonarQube Server | Sonar Documentation )

==============================================================================

D:\azagent01\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\7.4.2\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe end

SonarScanner for MSBuild 10.3

Using the .NET Framework version of the Scanner for MSBuild

Post-processing started.

##[error]12:42:17.299 No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.

12:42:17.299 No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.

12:42:17.299 Generation of the sonar-properties file failed. Unable to complete the analysis.

##[error]12:42:17.306 Post-processing failed. Exit code: 1

12:42:17.306 Post-processing failed. Exit code: 1

##[error][ERROR] SonarQube Server: Error while executing task Analyze: The process ‘D:\azagent01\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\7.4.2\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe’ failed with exit code 1

##[error]The process ‘D:\azagent01\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\7.4.2\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe’ failed with exit code 1

Finishing: SonarQube Analyze

So it seems that SonarQube is unable to analyze file-based apps even though it does log sonarqube warnings in the build output. Is it possible for you to support this scenario?

The compiler warnings likely come from analyzers running during publish, while the Analyze task has no MSBuild project to finish. Since file-based apps have no project file, the .NET scanner has nothing to import or collect results from. As a workaround, use a minimal analysis-only project that includes the source file if its directives allow it, and keep publish separate; the Scanner CLI may also be worth trying. A feature request for implicit file-based projects seems reasonable.

Hi @Keith.Banner, this is a known gap that we’re already tracking. So far there is no estimation for when will this be implemented, but I will let the team know that you are interested.