ExcludeTest from project breaks dotnet build when source generation is used

  • CI system used: Azure DevOps
  • Languages of the repository: c#, .net 8 solution
  • OS: windows / ubuntu (problem on both)

Hello,

I have a problem with excluding test projects from the analysis when c# source generation is used in a test project.

This is what I do in the SonarQube Cloud prepare step:

- task: SonarCloudPrepare@3
  displayName: "SonarQube Cloud Prepare"
  inputs:
    SonarCloud: SonarCloud
    organization: XXX
    scannerMode: 'dotnet'
    projectKey: some_key
    projectName: SonarCloudTest
    projectVersion: "$(Build.BuildNumber)"
    extraProperties: |
      sonar.projectBaseDir=$(Pipeline.Workspace)/s/src/Services/ScExampleService
      sonar.dotnet.excludeTestProjects=true

Notice that I do not want to scan the test projects (followed: https://docs.sonarsource.com/sonarcloud/advanced-setup/ci-based-analysis/sonarscanner-for-dotnet/using/)

If you then have a Test project with c# code like:

internal static partial class StringExtensions
{
    public static string RemoveWhitespaces(this string s)
    {
        return WhitespaceRegex().Replace(s, string.Empty);
    }

    [GeneratedRegex(@"\s")]
    private static partial Regex WhitespaceRegex();
}

Pipeline step for building:

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '$(Pipeline.Workspace)/s/src/Services/ScExampleService/**/*.csproj'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '$(Pipeline.Workspace)/s/src/Services/ScExampleService/**/*.csproj'
    arguments: '--configuration Release'

Then the build will fail on:
##[error]src/Services/ScExampleService/ScExampleService.Test/StringExtensions.cs(13,34): Error CS8795: Partial method ‘StringExtensions.WhitespaceRegex()’ must have an implementation part because it has accessibility modifiers.

To ensure that SC identifies the project as a test project, then I have inserted
“<SonarQubeTestProject>true</SonarQubeTestProject>” in the csproj file

csproj file for the test project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
      <AssemblyName>ScExampleService.Test</AssemblyName>
      <RootNamespace>ScExampleService.Test</RootNamespace>
      <SonarQubeTestProject>true</SonarQubeTestProject>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
      <PackageReference Include="Microsoft.NET.Test.Sdk" />
      <PackageReference Include="xunit" />
      <PackageReference Include="xunit.runner.visualstudio" >
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      </PackageReference>
  </ItemGroup>
</Project>

If I do not use the setting:
sonar.dotnet.excludeTestProjects=true
Then it builds fine

Thank you
Mikkel

Hi @Mikkel, thanks for raising this issue.

Are you able to enable verbose mode and share the logs?

For example:

- task: SonarCloudPrepare@3
  displayName: "SonarQube Cloud Prepare"
  inputs:
    SonarCloud: SonarCloud
    organization: XXX
    scannerMode: 'dotnet'
    projectKey: some_key
    projectName: SonarCloudTest
    projectVersion: "$(Build.BuildNumber)"
    extraProperties: |
      sonar.projectBaseDir=$(Pipeline.Workspace)/s/src/Services/ScExampleService
      sonar.dotnet.excludeTestProjects=true
      sonar.verbose=true

Thanks

Hello @alexander.meseldzija

Sure,

Logs from the prepare step:
Prepare_test_exclusion.txt (16.3 KB)

Logs from the build step:
dotnet_build_test_exclusion.txt (5.8 KB)

I have xxx’ed out the organization name.

Regards,
Mikkel

Hi there, @Mikkel

This seems to be a bug indeed in how this particular feature interacts with source generators. Thanks for reporting it!

In the meantime, I can suggest you use a different way to exclude this:

  1. Remove the sonar.dotnet.excludeTestProjects=true from your pipeline
  2. Remove the <SonarQubeTestProject>true</SonarQubeTestProject> tag from your test project
  3. Add a <SonarQubeExclude>true</SonarQubeExclude> tag to your test project

This will achieve the same effect without errors.

Denis

1 Like

Hi,

Thank you for the alternative way, I will use that

Thanks
Mikkel

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