{"id":12122,"date":"2016-02-18T20:56:28","date_gmt":"2016-02-18T20:56:28","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/?p=12122"},"modified":"2020-03-05T06:12:03","modified_gmt":"2020-03-05T14:12:03","slug":"sonarqube-scanner-for-msbuild-v2-0-released-support-for-third-party-roslyn-analyzers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/sonarqube-scanner-for-msbuild-v2-0-released-support-for-third-party-roslyn-analyzers\/","title":{"rendered":"SonarQube Scanner for MSBuild v2.0 released: support for third-party Roslyn analyzers"},"content":{"rendered":"<p>We are pleased to announce that <a href=\"http:\/\/www.sonarsource.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">SonarSource<\/a> has officially released <a href=\"http:\/\/docs.sonarqube.org\/display\/SONAR\/Analyzing+with+SonarQube+Scanner+for+MSBuild\" target=\"_blank\" rel=\"noopener noreferrer\">version 2.0<\/a> of the <i>SonarQube Scanner for MSBuild<\/i> and <a href=\"http:\/\/docs.sonarqube.org\/display\/PLUG\/C%23+Plugin\" target=\"_blank\" rel=\"noopener noreferrer\">version 4.5<\/a> of the <i>SonarQube C# Plugin<\/i>. The release notes for the <a href=\"https:\/\/jira.sonarsource.com\/jira\/secure\/ReleaseNote.jspa?projectId=10941&amp;version=12858\" target=\"_blank\" rel=\"noopener noreferrer\">scanner<\/a> and <a href=\"https:\/\/jira.sonarsource.com\/jira\/secure\/ReleaseNote.jspa?projectId=10935&amp;version=12878\" target=\"_blank\" rel=\"noopener noreferrer\">plugin<\/a> list the bugs that were fixed, but the major change is that together these releases provide support for using third-party Roslyn analyzers with <i>SonarQube.<\/i><\/p>\n<p>A pre-release version of the <a href=\"https:\/\/github.com\/SonarSource-VisualStudio\/sonarqube-roslyn-sdk\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>SDK for SonarQube Roslyn Analyzer Plugins<\/em><\/a> is also available &#8211; more on that below.<\/p>\n<h3>Support for third-party C# Roslyn analyzers<\/h3>\n<p>The Roslyn framework makes it easy to write custom code analysis rules for C# and VB code. With these new releases, it is possible to run custom Roslyn analyzers for C# as part of the build and have the results uploaded to <i>SonarQube<\/i>.<\/p>\n<p>From an experience point of view this is straightforward:<\/p>\n<ol>\n<li>Authors of the Roslyn analyzers produce a SonarQube plug-in for their analyzers using the <i>SDK for SonarQube Roslyn Analyzer Plugins<\/i><\/li>\n<li>The <em>SonarQube<\/em> administrator installs this plug-in in the same way as any other SonarQube plug-in<\/li>\n<li>Build users don\u2019t have to do anything: the <em>SonarQube scanner for MSBuild<\/em> will automatically set up the required Roslyn analyzers and configure the rulesets, based on the Quality Profile for the <em>SonarQube<\/em> project.<\/li>\n<\/ol>\n<p>The blog announcing the release of the <a href=\"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2016\/01\/04\/sonarqube-scanner-for-msbuild-v1-1-released-static-analysis-now-executed-during-the-build\/\">previous version<\/a> gave an overview of the architecture. Here, I\u2019ll describe how the various pieces fit together from the view points of the two main actors involved \u2013 the <i>SonarQube<\/i> administrator, and the Roslyn analyzer author. I\u2019ll then describe in more detail what happens at runtime when an analysis build is performed.<\/p>\n<h4>SonarQube administrators<\/h4>\n<p>From the point of view of <i>SonarQube<\/i> administrators, the Roslyn-specific plugins are no different from any other <i>SonarQube<\/i> plugin. Administrator can choose and install the plugins they want to use, then select which rules to apply in each quality profile just as they normally would. They don\u2019t need to know anything about Roslyn, and they don\u2019t need to perform any additional manual configuration or setup on the\u00a0machines that will perform the analysis.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/Scanner-2.0-sq-admin.jpg\"><img decoding=\"async\" class=\"alignnone size-mediumlarge wp-image-12132\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/02\/Scanner-2.0-sq-admin-475x350.jpg\" alt=\"Scanner 2.0 sq admin\" width=\"475\" height=\"350\" \/><\/a><\/p>\n<p>The only caveat is the not unreasonable one that the machine on which the analysis is performed must support running Roslyn analyzers i.e. the build must be using MSBuild 14.0 or later.<\/p>\n<h4>Roslyn analyzer authors<\/h4>\n<p>Just as <i>SonarQube<\/i> administrators shouldn\u2019t need to know about Roslyn analyzers, we\u2019ve tried to limit the amount Roslyn analyzer authors need to know about creating <i>SonarQube<\/i> plugins &#8211; they shouldn\u2019t need to be Java coders or to understand the <i>SonarQube<\/i> extensibility model, which is where the SDK mentioned earlier comes in. The SDK provides tooling to automatically generate a <em>SonarQube<\/em> plugin that wraps a Roslyn analyzer.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/Scanner-2.0-roslyn-author.jpg\"><img decoding=\"async\" class=\"alignnone size-mediumlarge wp-image-12152\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/02\/Scanner-2.0-roslyn-author-500x224.jpg\" alt=\"Scanner 2.0 roslyn author\" width=\"500\" height=\"224\" \/><\/a><\/p>\n<p>At this point the SDK is very simple: it\u2019s a single exe that you point at an analyzer NuGet package, and it produces a <i>jar<\/i> file containing the <i>SonarQube<\/i> plugin. This is currently no way to customise the <em>jar<\/em>\u00a0that is produced. See the project <a href=\"https:\/\/github.com\/SonarSource-VisualStudio\/sonarqube-roslyn-sdk\/\">README<\/a> for more information.<\/p>\n<p>I\u2019ve described\u00a0an idealised view here in which the plugin\u00a0<em>jar<\/em>\u00a0is\u00a0created and published by the analyzer author. However, there is nothing to stop a\u00a0<em>SonarQube<\/em> administrator from using the SDK to create a plugin if one isn\u2019t already available for a particular analyzer.<\/p>\n<h4>Under the covers<\/h4>\n<p>At runtime, the scanner, C# plugin and generated plugins co-operate to setup, configure and execute the rules configured in the Quality Profile for the <i>SonarQube<\/i> project.<\/p>\n<p><i><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/Scanner-2.0-interaction1.jpg\"><img decoding=\"async\" class=\"alignnone size-mediumlarge wp-image-12162\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/02\/Scanner-2.0-interaction1-458x350.jpg\" alt=\"Scanner 2.0 interaction\" width=\"458\" height=\"350\" \/><\/a><\/i><\/p>\n<p><em>Runtime\u00a0component collaboration<\/em><\/p>\n<p>The sequence of actions at runtime is as follows:<\/p>\n<ul>\n<li>The scanner requests information about the analyzers and ruleset to use during the build<\/li>\n<li>The C# plugin inspects all of the installed plugins to work out which ones are for Roslyn analyzers. It collects metadata from these plugins, along with a ruleset it has generated by looking at the configured Quality Profile, and it returns the requested data to the scanner<\/li>\n<li>The scanner downloads the necessary analyzer dlls from the <i>SonarQube<\/i> server and configures MSBuild to use them and the supplied ruleset. As in previous versions, the scanner does not make permanent changes to the MSBuild projects. Instead, it intercedes during the build to dynamically change the ruleset and set of analyzers that are used, disregarding any that are specified in the project file.<\/li>\n<li>The compilation phase then executes as normal. The compiler calls the specified Roslyn analyzers and produces an error report containing analysis errors and warnings for all of the analyzers.<\/li>\n<li>Finally, the scanner passes this report to the C# plugin, which uploads the errors and warnings to <i>SonarQube<\/i> as issues.<\/li>\n<\/ul>\n<h3>Next steps<\/h3>\n<p>Now that the infrastructure work on the scanner and C# plugin has been completed, we can turn our attention to improving the authoring experience for creating the plugin <em>jars<\/em>. The current SDK preview (version 0.9) will generate a working plugin but has a number of rough edges and is missing a couple of important features, such as support for analyzers that require license acceptance and the ability to provide remediation costs for new diagnostic rules. We&#8217;ll address these in the version 1.0 release.<\/p>\n<p>In my previous post I mentioned that we are also working on improving the integration between <i>SonarQube<\/i> and the Visual Studio IDE. This work is progressing well and an initial version should be available in the near future.<\/p>\n<p>As ever, comments, feedback and suggestions are welcome.<\/p>\n<p>&nbsp;<\/p>\n<p><em>(Special thanks to Marcel and Markus for finding and diagnosing a couple of early issues with the SDK)<\/em><\/p>\n<p><em>For support, look on\u00a0StackOverflow for questions\u00a0tagged with <a href=\"http:\/\/stackoverflow.com\/questions\/tagged\/sonarqube-msbuild-runner\" target=\"_blank\" rel=\"noopener noreferrer\">sonarqube-msbuild-runner<\/a><\/em><\/p>\n<p><em>For bugs, go to the\u00a0<span style=\"color: #808080;\"><a style=\"color: #808080;\" href=\"https:\/\/groups.google.com\/forum\/#!forum\/sonarqube\">SonarQube Google Group<\/a>.<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are pleased to announce that SonarSource has officially released version 2.0 of the SonarQube Scanner for MSBuild and version 4.5 of the SonarQube C# Plugin. The release notes for the scanner and plugin list the bugs that were fixed, but the major change is that together these releases provide support for using third-party Roslyn [&hellip;]<\/p>\n","protected":false},"author":181,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,1,249],"tags":[],"class_list":["post-12122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-community","category-devops","category-open-source"],"acf":[],"blog_post_summary":"<p>We are pleased to announce that SonarSource has officially released version 2.0 of the SonarQube Scanner for MSBuild and version 4.5 of the SonarQube C# Plugin. The release notes for the scanner and plugin list the bugs that were fixed, but the major change is that together these releases provide support for using third-party Roslyn [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/12122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/181"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=12122"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/12122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=12122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=12122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=12122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}