- 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