Your project contains only TEST-code for language C# and no MAIN-code for any language

Hi brand new to SonarCloud, thank you for reading. What brings me here today is the following message on our summary page where I see a warning at the upper right:

:warning: The last analysis has a warning. See details

I click on the “See details”

Last analysis has warnings

Your project contains only TEST-code for language C# and no MAIN-code for any language, so only TEST-code related results are imported. Many of our rules (e.g. vulnerabilities) are raised only on MAIN-code. Read more about how the SonarScanner for .NET detects test projects: Analysis of product projects vs. test projects · SonarSource/sonar-scanner-msbuild Wiki · GitHub

I appreciate how informative and comprehensive this Github wiki page is, but I’m not gonna sugar coat or attempt to preserve any pride here, I don’t follow. It’s a ton of context, none of which I’m able to connect the dots with my error. I hope to see something on this page about TEST-code or MAIN-code – nothing.

We run our CICD out of Bitbucket Pipelines, this is what I have stubbed out which a former engineer had working with another project.

script:
    - cd src/projectName
    - dotnet restore
    - dotnet build --configuration=Release --no-restore
    - |
    dotnet sonarscanner begin \
    /k:"${BITBUCKET_REPO_SLUG}" \
    /d:sonar.login="${SONAR_TOKEN}" \
    /o:"extendedstay-website" \
    /d:"sonar.host.url=https://sonarcloud.io" \
    /d:sonar.cs.opencover.reportsPaths="test-results/**/coverage.opencover.xml"
    - dotnet test --configuration=Release --no-restore
    - dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"

Our project lives in src/projectName where I’ve updated the respective .csproj file with the following XML (there’s more here, omitting for this community post):

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <SonarQubeTestProject>false</SonarQubeTestProject>
    </PropertyGroup>
</Project>

I expect SonarCloud to test code in here, clearly it isn’t.

Is there a troubleshooting guide I can follow? Possible to access paid support? I’m not sure where to go next, any help provided will be greatly appreciated.

Hi Casey

Thanks for sharing the issue you are having here. To get a bit more context is this a solution (.sln) that contains both test projects and application code? From your description, it looks like it might be just a single project but that doesn’t really make sense if you are running tests, which should be in a separate project. If there are multiple projects within a solution you need to make sure the SonarQubeTestProject attribute is just set on the projects containing the application code.

Good luck

Tom

Hi Tom,

Massively appreciate the response. Output from the dotnet sln Example.Project.sln list command

Project(s)
----------
Example.Project.Trigger/Example.Project.Trigger.csproj
Example.Project.Processor/Example.Project.Processor.csproj
Example.Project.Trigger.Tests/Example.Project.Trigger.Tests.csproj
Example.Project.Processor.Tests/Example.Project.Processor.Tests.csproj
Example.Project.Common/Example.Project.Common.csproj

NON-Test Porjects

With .csproj files NOT ending in *.Tests.csproj, to the <PropertyGroup>, I’ve added <SonarQubeTestProject>false</SonarQubeTestProject>

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <SonarQubeTestProject>false</SonarQubeTestProject>
</PropertyGroup>

The pipline runs, but continue to see the warning in the SonarCloud web console.

:warning: The last analysis has a warning.See details

Test Projects

With .csproj files ending in *.Tests.csproj, to the <PropertyGroup>, when I explicitly exclude by adding <SonarQubeExclude>true</SonarQubeExclude>

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>

I observe the following in my pipeline indicating there are no analysable projects.

13:12:13.44  The exclude flag has been set so the project will not be analyzed. Project file: /opt/atlassian/pipelines/agent/build/src/Example.Project/Example.Project.Processor.Tests/Example.Project.Processor.Tests.csproj
13:12:13.44  The exclude flag has been set so the project will not be analyzed. Project file: /opt/atlassian/pipelines/agent/build/src/Example.Project/Example.Project.Trigger.Tests/Example.Project.Trigger.Tests.csproj
13:12:13.44  No analysable projects were found. SonarCloud analysis will not be performed. Check the build summary report for details.

When I remove <SonarQubeExclude>true</SonarQubeExclude> from the *.Tests.csproj, I’m back to this in the SonarCloud web console

:warning: The last analysis has a warning.See details

Thanks again for your help.

Hi Casey

That all looks ok to me and you shouldn’t need to exclude the test projects. I think it’s time to look at some logs. Can you do a build with verbose logs turned on? You can do this by adding /d:“sonar.verbose=true” to your begin step. I will send you a DM that you can reply to with the logs attached.

Cheers

Tom

1 Like

Hi Tom, was able to output verbose I received your email and responded with zipped logging attached. Please let me know if you don’t receive.

Thanks Casey, logs received, thanks

I think the problem is that your build is happening before the begin step. If you refer to this page there are examples of the commands with testing and code coverage included. With .NET, the analysis happens during the build and the begin step sets it up. The way you have it setup the analysis will only run on the test command which presumably isn’t recompiling the application code.

Sorry I should have spotted this first time around!

Tom

1 Like

Disco :notes:

Moving the dotnet build command after dotnet sonarscanner begin was it! Can’t overstate how much I appreciate your help, thank you again!

script:
  - cd src/exampleProject
  - dotnet restore
  - |
    dotnet sonarscanner begin \
    /k:"${BITBUCKET_REPO_SLUG}" \
    /d:sonar.login="${SONAR_TOKEN}" \
    /d:"sonar.verbose=true" \
    /o:"extendedstay-website" \
    /d:"sonar.host.url=https://sonarcloud.io" \
    /d:sonar.cs.opencover.reportsPaths="test-results/**/coverage.opencover.xml"
  - dotnet build --configuration=Release --no-restore
  - dotnet test --configuration=Release --no-restore
  - dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
1 Like

You’re welcome :slight_smile:

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