Sonarscanner not analyzing netcoreapp2.2

I am trying sonarqube on my aspnetcore2.2 project.

The project uses MSbuild version - 16.1.76.45076.

When I run the official dotnet-sonarscanner tool - version 4.6.2,
only CSS code is getting analyzed and not the C#, Javascript and other codes from the project.

I have tried the following,

Using Different MSBuilds:

I have tested with different MSBuild versions as there was a change in MSBuild from aspnetcore2.1 to aspnetcore2.2

Versions tried

  • MSBuild - 16.1.76.45076
  • MSBuild - 15.1.0.0

Both returned with the same result of not analyzing any files other than CSS files.


Using Different Sonarscanner tools:

I also went ahead and checked with different sonarscanner tools

Sonarscanner tools used

  • dotnet-sonarscanner - 4.6.2
  • dotnet-sonarscanner - 4.6.0
  • SonarScanner.MSBuild - 4.6.2 for aspnetcore2.0+
  • SonarScanner.MSBuild - for aspnetcore 2.1.3
  • .NETFramework4.6

All the above resulted in the same result of no files other than CSS files being analyzed


Verification of Project Settings in Sonarqube:

To check if the server-side is setup properly, I tested on different frameworks of aspnetcore

Tested frameworks

  • aspnetcore 2.0
  • aspnetcore 2.1
  • aspnetcore 2.2

Only in aspnetcore2.2, I am facing this problem.

aspnetcore2.0, aspnetcore2.1 were analyzed for all the files C#, HTML, Javascript and CSS

These were also tested with different MSBuild versions and sonarscanner tools.


CLI Command tried

These are the CLI used for the replication of the problem

All the below commands didn’t return an error.

Only the CSS files were getting analyzed for aspnetcore2.2 projects

The commands were tried in different combination as described above to replicate version problems

dotnet sonarscanner begin /k:"<projectname>" /d:sonar.host.url="<sonarqserver>" /d:sonar.login="<usertoken>"
dotnet build
dotnet sonarscanner end /d:sonar.login="<usertoken>"
dotnet <pathto Sonarscanner.MSBuild.dll> begin /k:"<projectname>" /d:sonar.host.url="<sonarqserver>" /d:sonar.login="<usertoken>"
dotnet build
dotnet <pathto Sonarscanner.MSBuild.dll> end /d:sonar.login="<usertoken>"
dotnet sonarscanner begin /k:"<projectname>" /d:sonar.host.url="<sonarqserver>" /d:sonar.login="<usertoken>"
<pathto MSBuildversion.exe> build
dotnet sonarscanner end /d:sonar.login="<usertoken>"

The expected result is

  • Analysis of C#, Javascript, CSS and HTML code

The actual result is

  • Only analysis of CSS files

Edit 1

Answering the following questions

  • Are the expected files listed in
    .sonarqube\out\sonar-project.properties?
  • Add the property /d:sonar.verbose=true to the begin step and run the
    analysis steps again. Then have a look at the output of the end step
    for warnings (try searching the file name of one the files that
    should have been analyzed, and for “C#”)
  • which version and edition of SonarQube are you using?
  • which version of the SonarC# plugin?

  • Yes, they are present in the .sonarqube\out\sonar-project.properties

  • /d:sonar.verbose=true to the beginning step was tested and went through the analysis, these are the key points observation from the output

  • The indexing of the file was done and all the files including the .cs, .cshtml (Razor Pages), minified js, minified CSS, CSS and others were picked up during it.

  • During Indexing, the .cshtml files were recognized as ‘web’ and all the other C# code as ‘cs’ language

  • On the Run Sensors Stage, Only the following Sensors were used

DEBUG: Sensors : SonarCSS Metrics -> SonarCSS Rules -> JaCoCo XML Report Importer -> C# Properties -> JavaXmlSensor -> XML Sensor.

This starts the CSS Analysis, by default, it is using node, -v, v8.12.0. The following node processed is used for .cshtml files too.

  • From the rest of the Sensors, got the following output

For XML, the file was indexed during the beginning of Sonar scanner.
This file is our own file of unrelated format analysis, not a part of sonarqube

Sensor JavaXmlSensor [java]
2 source files to be analyzed
'AnalysisFile.xml' generated metadata as test  with charset 'UTF-8'
'wwwroot/XML/AnalysisFile_16-04-2019.xml' generated metadata as test  with charset 'UTF-8'
Sensor JavaXmlSensor [java] (done) | time=1284ms

For C#

Sensor C# Properties [csharp]
Analyzer working directory '<ProjectDir>\.sonarqube\out\0\output-cs' contains 5 .pb file(s)
Found Roslyn issues report
Sensor C# Properties [csharp] (done) | time=15ms
  1. Run sensors runs again with lowercase of the project-key. This time no file to analyze comes up in SensorC# as below
Sensor C# [csharp]
No files to analyze. Skip Sensor.
Sensor C# [csharp] (done) | time=0ms
  1. Run sensors runs on the project, These time the following output comes out as below
Java CPD Block Indexer' skipped because there is no related file in current project
Sensor Zero Coverage Sensor (done) | time=0ms
  1. After this, a list of .cs and .cshtml files gets the following
<filename>.cshtml generated metadata as test  with charset 'UTF-8'
<filename>.cs generated metadata as test  with charset 'UTF-8'
  1. At the very end following WARN: are given before posting the analysis
WARN: Missing blame information for the following files
WARN:   * SonarQube.Analysis.xml
WARN: This may lead to missing/broken features in SonarQube
  1. And the job is posted
  • I am using Sonarqube 7.9 Community Edition

  • I am using the default plugin which gets downloaded with Sonarqube 7.9 Server - SonarC# - 7.15 (I am not sure if this is the same version getting installed as default in Sonarqube 7.9 Server

  • I tried the following steps for dotnetcore2.1, the C# and other files were analyzed

  • Also tried the following without Inhouse XML Analysis file if there was something breaking in analysis due to it.

The issue still persists


(note: this question is duplicated in the Scanner for MSBuild repo and on StackOverflow)

@armourshield your latest comment on the issue in the S4MSB repo said:

I wanted to test the environment if it is right with the same tools
Created new projects for dotnetcore2.1 and dotnetcore2.2
When running the sonarq analysis it ran perfectly analyzing all the files

Does this mean that you have a working environment in which the netcoreapp2.2 app is being correctly analyzed?

If so, what is different between your working an non-working environments?

  • Yes, new netcoreapp2.2 is being analyzed.

The difference is in the new created project there is no TestCases.
In our project, we have Nunit 3, TestCases which uses mocking.

After going through the output of sonarqube, found this .sonarqube\out\0\ProjectInfo.xml

    <?xml version="1.0" encoding="utf-8"?>
<ProjectInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">
  <ProjectName>project</ProjectName>
  <ProjectLanguage>C#</ProjectLanguage>
  <ProjectType>Test</ProjectType>
  <ProjectGuid>ad649366-fea7-4513-91e3-90deb56bd30c</ProjectGuid>
  <FullPath>project.csproj</FullPath>
  <IsExcluded>false</IsExcluded>
  <AnalysisResults>
    <AnalysisResult Id="FilesToAnalyze" Location=".sonarqube\out\0\FilesToAnalyze.txt" />
  </AnalysisResults>
  <AnalysisSettings>
    <Property Name="sonar.cs.roslyn.reportFilePath">project\bin\Debug\netcoreapp2.2\project.dll.RoslynCA.json|project\bin\Debug\netcoreapp2.2\project.Views.dll.RoslynCA.json</Property>
    <Property Name="sonar.cs.analyzer.projectOutPath">project\.sonarqube\out\0</Property>
  </AnalysisSettings>
  <Configuration>Debug</Configuration>
  <Platform>AnyCPU</Platform>
  <TargetFramework>netcoreapp2.2</TargetFramework>
</ProjectInfo>

The whole project is getting recognized as a Test Project even when it is not mentioned

The above can be traced back to .sonarqube\conf\sonar-project.properties, here the sonar.souces is not taking the cs files, the files are going to .sonar.tests. As seen below

    sonar.projectKey=exp_test
sonar.working.directory=D:\\Work\\Experiment\\project\\.sonarqube\\out\\.sonar
sonar.projectBaseDir=project

AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.projectKey=exp_test:AD649366-FEA7-4513-91E3-90DEB56BD30C
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.projectName=project
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.projectBaseDir=project
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.sourceEncoding=utf-8
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.sources=
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.tests=\
"project\\Areas\\Identity\\file.cs",\
"project\\Areas\\Identity\\Pages\\Account\\file.cshtml.cs",\
"project\\Areas\\Identity\\Pages\\Account\\file.cshtml.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Controllers\\file.cs",\
"project\\Data\\file.cs",\
"project\\Data\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\Models\\file.cs",\
"project\\file.cs",\
"project\\REST\\Controllers\\file.cs",\
"project\\REST\\Controllers\\file.cs",\
"project\\REST\\Controllers\\file.cs",\
"project\\REST\\UnitTests\\file.cs",\
"project\\REST\\ViewModels\\file.cs",\
"project\\REST\\ViewModels\\file.cs",\
"project\\REST\\ViewModels\\file.cs",\
"project\\file.cs",\
"project\\ViewComponents\\file.cs",\
"project\\ViewModels\\file.cs",\
"project\\ViewModels\\file.cs",\
"project\\ViewModels\\file.cs",\
"project\\ViewModels\\file.cs",\
"project\\ViewModels\\file.cs",\
"project\\obj\\Debug\\netcoreapp2.2\\project.file.cs",\
"project\\obj\\Debug\\netcoreapp2.2\\project.file.cs",\
"project\\Areas\\Identity\\Pages\\Account\\file.cshtml",\
"project\\Areas\\Identity\\Pages\\Account\\file.cshtml",\
"project\\Areas\\Identity\\Pages\\Account\\_file.cshtml",\
"project\\Areas\\Identity\\Pages\\_file.cshtml",\
"project\\Areas\\Identity\\Pages\\_file.cshtml",\
"project\\Areas\\Identity\\Pages\\_file.cshtml",\
"project\\Views\\Account\\file.cshtml",\
"project\\Views\\Audit\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Home\\file.cshtml",\
"project\\Views\\Incident\\file.cshtml",\
"project\\Views\\Incident\\file.cshtml",\
"project\\Views\\ProfileUpload\\file.cshtml",\
"project\\Views\\ProfileUpload\\file.cshtml",\
"project\\Views\\Shared\\Components\\Profile\\file.cshtml",\
"project\\Views\\Shared\\file.cshtml",\
"project\\Views\\Shared\\_file.cshtml",\
"project\\Views\\Shared\\_file.cshtml",\
"project\\Views\\Shared\\_file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\UserManagement\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\XMlParse\\file.cshtml",\
"project\\Views\\_file.cshtml",\
"project\\Views\\_file.cshtml",\
"project\\wwwroot\\css\\css-chart.css",\
"project\\wwwroot\\css\\site.css",\
"project\\wwwroot\\css\\site.min.css",\
"project\\wwwroot\\css\\stylesheet.css",\
"project\\wwwroot\\favicon.ico",\
"project\\wwwroot\\images\\favicon.ico",\
"project\\wwwroot\\images\\favicon.png",\
"project\\wwwroot\\images\\landing-page-background.jpg",\
"project\\wwwroot\\images\\no-user.jpg",\
"project\\wwwroot\\images\\user.png",\
"project\\wwwroot\\images\\yondrlogo.png",\
"project\\wwwroot\\images\\yondrlogoblack.png",\
"project\\wwwroot\\images\\yongroup-main.jpg",\
"project\\wwwroot\\js\\bootstrap-material-design.min.js",\
"project\\wwwroot\\js\\chartist.min.js",\
"project\\wwwroot\\js\\jquery-ui.min.js",\
"project\\wwwroot\\js\\jquery.dataTables.min.js",\
"project\\wwwroot\\js\\jquery.min.js",\
"project\\wwwroot\\js\\material-dashboard.min.js",\
"project\\wwwroot\\js\\perfect-scrollbar.jquery.min.js",\
"project\\wwwroot\\js\\popper.min.js",\
"project\\wwwroot\\js\\site.js",\
"project\\wwwroot\\js\\site.min.js",\
"project\\wwwroot\\lib\\bootstrap\\.bower.json",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-grid.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-grid.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-grid.min.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-grid.min.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-reboot.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-reboot.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-reboot.min.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-reboot.min.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-theme.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-theme.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-theme.min.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap-theme.min.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap.min.css",\
"project\\wwwroot\\lib\\bootstrap\\dist\\css\\bootstrap.min.css.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\fonts\\glyphicons-halflings-regular.eot",\
"project\\wwwroot\\lib\\bootstrap\\dist\\fonts\\glyphicons-halflings-regular.svg",\
"project\\wwwroot\\lib\\bootstrap\\dist\\fonts\\glyphicons-halflings-regular.ttf",\
"project\\wwwroot\\lib\\bootstrap\\dist\\fonts\\glyphicons-halflings-regular.woff",\
"project\\wwwroot\\lib\\bootstrap\\dist\\fonts\\glyphicons-halflings-regular.woff2",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.bundle.js",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.bundle.js.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.bundle.min.js",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.bundle.min.js.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.js",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.js.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.min.js",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\bootstrap.min.js.map",\
"project\\wwwroot\\lib\\bootstrap\\dist\\js\\npm.js",\
"project\\wwwroot\\lib\\bootstrap\\LICENSE",\
"project\\wwwroot\\lib\\jquery-validation-unobtrusive\\.bower.json",\
"project\\wwwroot\\lib\\jquery-validation-unobtrusive\\jquery.validate.unobtrusive.js",\
"project\\wwwroot\\lib\\jquery-validation-unobtrusive\\jquery.validate.unobtrusive.min.js",\
"project\\wwwroot\\lib\\jquery-validation-unobtrusive\\LICENSE.txt",\
"project\\wwwroot\\lib\\jquery-validation\\.bower.json",\
"project\\wwwroot\\lib\\jquery-validation\\dist\\additional-methods.js",\
"project\\wwwroot\\lib\\jquery-validation\\dist\\additional-methods.min.js",\
"project\\wwwroot\\lib\\jquery-validation\\dist\\jquery.validate.js",\
"project\\wwwroot\\lib\\jquery-validation\\dist\\jquery.validate.min.js",\
"project\\wwwroot\\lib\\jquery-validation\\LICENSE.md",\
"project\\wwwroot\\lib\\jquery\\.bower.json",\
"project\\wwwroot\\lib\\jquery\\dist\\jquery.js",\
"project\\wwwroot\\lib\\jquery\\dist\\jquery.min.js",\
"project\\wwwroot\\lib\\jquery\\dist\\jquery.min.map",\
"project\\wwwroot\\lib\\jquery\\LICENSE.txt",\
"project\\wwwroot\\swagger-ui\\style.css",\
"project\\nlog.config",\
"project\\appsettings.Development.json",\
"project\\appsettings.json",\
"project\\Connected Services\\Application Insights\\ConnectedService.json",\
"project\\Properties\\launchSettings.json",\
"project\\internalLog.txt"

AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.cs.roslyn.reportFilePath=project\\bin\\Debug\\netcoreapp2.2\\project.dll.RoslynCA.json
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.cs.analyzer.projectOutPath=project\\.sonarqube\\out\\0
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.cs.analyzer.projectOutPaths=\
"project\\.sonarqube\\out\\0"
AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.cs.roslyn.reportFilePaths=\
"project\\bin\\Debug\\netcoreapp2.2\\project.dll.RoslynCA.json",\
"project\\bin\\Debug\\netcoreapp2.2\\project.Views.dll.RoslynCA.json"

AD649366-FEA7-4513-91E3-90DEB56BD30C.sonar.working.directory=project\\.sonarqube\\out\\.sonar\\mod0
sonar.host.url=sonarqurl
sonar.visualstudio.enable=false

sonar.modules=AD649366-FEA7-4513-91E3-90DEB56BD30C

Here the source files are going to tests rather than sources, for new project it goes to sources.

But with the current project it goes to tests

Ok, that makes sense. Test code is not analyzed by the Scanner for MSBuild, and the whole project is categorised as being either test code or product code (rather than file by file). The Detection of Test Projects section in the docs explains how the scanner decides whether a project is a test project or not, and how to explicitly mark a project as being a test/non-test project.

So with the following code in .csproj as false demarks the project as Test project.

```
<!-- in .csproj -->
<PropertyGroup>
<!-- Mark the project as being a test project -->
<SonarQubeTestProject>false</SonarQubeTestProject>
</PropertyGroup>
```

Will test it out now, Thank you

It worked, the C# sharp and the project got analyzed, Thank you for helping out

But I am just curious on why the sonarscanner behaved such a way? When the project completely had all the components from MVC, Nunit Cases to RestAPI still it was recognizing it as a Test Project only?

No ProjectGroup were added before

Does this mean the dotnet framework projects needs to be decoupled in the sense MVC separate csproj and Nunit separate csproj, then which helps sonarscanner to identify it is a test project or not a test project

Essentially, yes. The presence of references to a test framework is taken as an indicator that the project contains test code. We can’t easily tell if the project contains a mix of test and non-test code so we make the assumption that all of the code is test code (although it is valid to have test and non-test code in the same project the most common approach is to have the test code in a separate assembly, so this assumption is ok in most cases).