SonarScanner for .NET
Since version 5.0, the SonarScanner for MSBuild is now the SonarScanner for .NET. The documentation is updated with the new name. Artifacts and links retain the old name for now.
The SonarScanner for .NET is the recommended way to launch an analysis for projects using the
dotnet build tools. It is the result of a collaboration between SonarSource and Microsoft.
It supports .NET Core on every platform (Windows, macOS, Linux).
- Java 17 or later
- The SDK corresponding to your build system:
The flavor used to compile the Scanner for .NET (either .NET Framework, .NET Core or .NET) is independent of the .NET version the project you want to analyze has been built with. Concretely, you can analyze .NET Core code with the .NET Framework version of the Scanner. It's only relevant depending on your OS, and on the versions of .NET SDKs that are installed on your build machine.
- Expand the downloaded file into the directory of your choice. We'll refer to it as
$install_directoryin the following steps.
- On Windows, you might need to unblock the ZIP file first (right-click File > Properties > Unblock).
- On Linux/OSX you may need to set execute permissions on the files in
- Uncomment, and update the global settings to point to SonarCloud by editing
$install_directory/SonarQube.Analysis.xml. Values set in this file will be applied to all analyses of all projects unless overwritten locally.
Consider setting file system permissions to restrict access to this file:
$install_directoryto your PATH environment variable.
dotnet tool install --global dotnet-sonarscanner --version x.x.x
The --version argument is optional. If it is omitted, the latest version will be installed. A complete list of releases is available on the NuGet page
.NET Core Global Tool is available from .NET Core 3.1+.
You can invoke the Scanner using arguments with both dash (-) or forward-slash (/) separators. For example:
SonarScanner.MSBuild.exe begin /k:"project-key" or
SonarScanner.MSBuild.exe begin -k:"project-key"
There are two versions of the SonarScanner for .NET. In the following commands, you need to pass an authentication token using the
The first version is based on the "classic" .NET Framework. To use it, execute the following commands from the root folder of your project:
Note: On macOS or Linux, you can also use
mono <path to SonarScanner.MSBuild.exe>.
The second version is based on .NET Core which has a very similar usage:
The .NET Core version can also be used as a .NET Core Global Tool. After installing the Scanner as a global tool as described above, it can be invoked as follows:
In summary, the invocation of the SonarScanner for .NET will depend on the scanner flavor:
|.NET Core Global Tool|
|.NET Core 3.1+|
|.NET Framework 4.6.2+|
- The .NET Core version of the scanner does not support TFS XAML builds and automatic finding/conversion of Code Coverage files. Apart from that, all versions of the Scanner have the same capabilities and command-line arguments.
begin step is executed when you add the
begin command-line argument. It hooks into the build pipeline, downloads SonarCloud quality profiles and settings, and prepares your project for analysis.
- [required] Specifies the key of the analyzed project in SonarCloud.
- [optional] Specifies the name of the analyzed project in SonarCloud.
- Adding this argument will overwrite the project name in SonarCloud if it already exists.
- [recommended] Specifies the version of your project.
- [required] Specifies the name of the target organization in SonarCloud.
/d:sonar.token=<token> or <username>
- [recommended] Specifies the authentication token or username used to authenticate with to SonarCloud.
- If this argument is added to the begin step, it must also be added to the end step.
- [optional] Sets the logging verbosity to detailed.
- Add this argument before sending logs for troubleshooting.
- [optional] Excludes Test Projects from analysis.
- Add this argument to improve build performance when issues should not be detected in Test Projects.
- [optional] Specifies an additional SonarCloud analysis parameter.
- You can add this argument multiple times.
For detailed information about all available parameters, see Analysis Parameters.
The "begin" step will modify your build like this:
- the active
CodeAnalysisRuleSetwill be updated to match the SonarCloud quality profile
WarningsAsErrorswill be turned off
If your build process cannot tolerate these changes we recommend creating a second build job for SonarCloud analysis.
end steps, you need to build your project, execute tests and generate code coverage data. This part is specific to your needs, and it is not detailed here. See .NET Test coverage for more information.
The rules configured in your SonarCloud Quality Profile are run during the build, and it is expected that analyzing with SonarCloud can increase build duration from 4 to 8 times. The impact on duration will vary by project and by what rules are enabled; some rules are simple to execute and others take additional time to have the impact and precision expected of them
The end step is executed when you add the "end" command-line argument. It cleans the
MSBuild/dotnet build hooks, collects the analysis data generated by the build, the test results, and the code coverage, and then uploads everything to SonarCloud. There are only two additional arguments that are allowed for the end step.
- This argument is required if it was added to the begin step.
- This argument is required if it was added to the begin step and you are not using an authentication token.
- MSBuild versions older than 14 are not supported.
- Web Application projects are supported. Legacy Web Site projects are not.
- Projects targeting multiple frameworks and using preprocessor directives could have slightly inaccurate metrics (lines of code, complexity, etc.) because the metrics are calculated only from the first of the built targets.
In an Azure DevOps / TFS environment, test files are automatically retrieved as follows:
- A search is done for
.trxfiles in any
TestResultsfolder located under
- If no .trx files are found there, then a fallback search is performed under
.trx files have been found, their
.coverage counterparts are retrieved and converted to
.coveragexml files for upload to SonarCloud.
As stated above, this will work only with the .NET Framework version of the scanner.
See Test Coverage for more information.
Some project types, such as Microsoft Fakes, are automatically excluded from the analysis. To manually exclude a different type of project from the analysis, place the following in its
The Sonar Scanner for .NET requires your project to be built with MSBuild 14.0. We recommend installing Visual Studio 2015 update 3 or later on the analysis machine to benefit from the integration and features of the Visual Studio ecosystem (VSTest, MSTest unit tests, etc.).
Projects targeting older versions of the .NET Framework can be built using MSBuild 14.0 by setting the "TargetFrameworkVersion" MSBuild property as documented by Microsoft:
For example, if you want to build a .NET 3.5 project, but you are using a newer MSBuild version:
If you do not want to switch your production build to MSBuild 14.0, you can set up a separate build dedicated to the SonarCloud analysis.
You can read a full description of that subject on our wiki here.
Some analysis parameters can be set for a single MSBuild project by adding them to its
For newer SDK-style projects (used by .NET Core, .NET 5, and later), the SonarScanner for .NET will analyze all file types supported by the available language plugins unless explicitly excluded.
For older-style projects, the scanner will only analyze files listed in the
.vbproj project file. Usually, this means that only C# and VB files will be analyzed. To enable the analysis of other types of files, include them in the project file.
More specifically, any files included by an element of one of the
ItemTypes in this list will be analyzed automatically. For example, the following line in your
<Content Include="foo\bar\*.js" />
will enable the analysis of all JS files in the directory
Content is one of the
ItemTypes whose includes are automatically analyzed.
You can also add
ItemTypes to the default list by following the directions here.
You can check which files the scanner will analyze by looking in the file .sonarqube\out\sonar-project.properties after MSBuild has finished.
On build machines that connect to the Internet through a proxy server you might experience difficulties connecting to SonarQube. To instruct the Java VM to use specific proxy settings use the following value:
Where yourProxyHost and yourProxyPort are the hostname and the port of your proxy server. There are additional proxy settings for HTTPS, authentication, and exclusions that could be passed to the Java VM. For more information, see the following article: https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html.
You also need to set the appropriate proxy environment variables used by .NET.
NO_PROXY are all supported. You can find more details here.
I have multiple builds in the same pipeline, each of them getting analyzed even if the Run Code Analysis has already been executed.
We don't uninstall the global
ImportBefore targets to support concurrent analyses on the same machine. The main effect is that if you build a solution where a .sonarqube folder is located nearby, then the
sonar-dotnet analyzer will be executed along with your build task.
To avoid that, you can disable the targets file by adding a build parameter: