title: SonarScanner (C#) not honoring in-code StyleCop warning suppression
body: <p>I am trying to use SonarQube for static code analysis for my organization.
  All our C# projects already have StyleCop enabled which has helped us a lot in terms of code readability. Now we want to utilize SonarQube for static code analysis.</p>
  
  <p>I successfully hosted the SonarQube server locally following the guidelines provided <a href="https://docs.sonarqube.org/latest/setup/get-started-2-minutes/" rel="nofollow noreferrer">here</a>. I am able to run the analysis successfully and generate the sonarqube report when there are no warning-suppressions in the code.  </p>
  
  <p>ISSUE: Sonarqube does not consider in-code warning-suppressions and msbuild fails.</p>
  
  <p>I created a sample C# console application project to demo the problem I am facing.</p>
  
  <p>StyleCop is enabled (Installed nuget: StyleCop.Analyzers v1.1.118) and warnings are converted to errors.
  Below is the snippet of the ruleset </p>
  
  <pre><code>...
  ...
  ...
  &lt;Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"&gt;
  ...
  ...
  &lt;Rule Id="SA1307" Action="Error" /&gt;
  ...
  ...
  </code></pre>
  
  <p>The project has the below files:
  1. Program.cs</p>
  
  <pre><code>// &lt;copyright file="Program.cs" company="PlaceholderCompany"&gt;
  // Copyright (c) PlaceholderCompany. All rights reserved.
  // &lt;/copyright&gt;
  
  namespace StyleCopSonarQubeIssue
  {
      /// &lt;summary&gt;
      /// The program class.
      /// &lt;/summary&gt;
      internal class Program
      {
          /// &lt;summary&gt;
          /// Defines the entry point of the application.
          /// &lt;/summary&gt;
          /// &lt;param name="args"&gt;The arguments.&lt;/param&gt;
          public static void Main(string[] args)
          {
              // Method intentionally left empty.
          }
      }
  }
  
  </code></pre>
  
  <ol start="2">
  <li>SystemInfo.cs</li>
  </ol>
  
  <pre><code>// &lt;copyright file="SystemInfo.cs" company="PlaceholderCompany"&gt;
  // Copyright (c) PlaceholderCompany. All rights reserved.
  // &lt;/copyright&gt;
  
  namespace StyleCopSonarQubeIssue
  {
      using System.Runtime.InteropServices;
  
      /// &lt;summary&gt;
      /// The sytem info enum.
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Sequential)]
      public struct SystemInfo
      {
          /// &lt;summary&gt;
          /// Oem Id.
          /// &lt;/summary&gt;
          public uint dwOemId;
  
          /// &lt;summary&gt;
          /// Page size.
          /// &lt;/summary&gt;
          public uint dwPageSize;
      }
  }
  
  </code></pre>
  
  <ol start="3">
  <li>packages.config</li>
  </ol>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;packages&gt;
    &lt;package id="StyleCop.Analyzers" version="1.1.118" targetFramework="net452" developmentDependency="true" /&gt;
  &lt;/packages&gt;
  </code></pre>
  
  <ol start="4">
  <li>GlobalSuppressions.cs</li>
  </ol>
  
  <pre><code>[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Native code.", Scope = "member", Target = "~F:StyleCopSonarQubeIssue.SystemInfo.dwPageSize")]
  [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Native code.", Scope = "member", Target = "~F:StyleCopSonarQubeIssue.SystemInfo.dwOemId")]
  </code></pre>
  
  <ol start="5">
  <li>App.config (default - no changes)</li>
  </ol>
  
  <p>Build the project - <strong><em>SUCCESS</em></strong></p>
  
  <p>Follow the guidelines by SonarQube to analyze the above project.</p>
  
  <ol>
  <li><p>SonarScanner.MSBuild.exe begin /k:"StyleCopSonarQubeIssue" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="<strong><em></em></strong>"</p></li>
  <li><p>MsBuild.exe /t:Rebuild
  You will be below error after this statement:</p></li>
  </ol>
  
  <pre><code>Microsoft (R) Build Engine version 14.0.27530.0
  Copyright (C) Microsoft Corporation. All rights reserved.
  
  Build started 2019-07-22 11:17:25 AM.
  The target "RazorCoreCompile" listed in a BeforeTargets attribute at "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\.sonarqube\bin\targets\SonarQube.Integration.targets (453,49)" does not exist in the project, and will be ignored.
  Project "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj" on node 1 (Rebuild target(s)).
  CoreClean:
    Creating directory "obj\Debug\".
  GenerateBindingRedirects:
    No suggested binding redirects from ResolveAssemblyReferences.
  CreateProjectSpecificDirs:
    Creating directory "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\.sonarqube\conf\0".
  CoreCompile:
    C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Net.Http.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\StyleCopSonarQubeIssue.exe /ruleset:"C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\.sonarqube\conf\0\merged.ruleset" /errorlog:"C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\bin\Debug\StyleCopSonarQubeIssue.exe.RoslynCA.json" /subsystemversion:6.00 /target:exe /warnaserror- /utf8output /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\0\Google.Protobuf.dll /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\0\SonarAnalyzer.CSharp.dll /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\0\SonarAnalyzer.dll /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\1\Google.Protobuf.dll /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\1\SonarAnalyzer.dll /analyzer:C:\Users\bshah\AppData\Local\Temp\.sonarqube\resources\1\SonarAnalyzer.VisualBasic.dll /analyzer:..\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll /analyzer:..\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.dll /additionalfile:"C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\.sonarqube\conf\cs\SonarLint.xml" /additionalfile:"C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\.sonarqube\conf\0\ProjectOutFolderPath.txt" GlobalSuppressions.cs Program.cs Properties\AssemblyInfo.cs SystemInfo.cs "C:\Users\bshah\AppData\Local\Temp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
    Using shared compilation with compiler from directory: C:\Program Files (x86)\MSBuild\14.0\bin
  Program.cs(10,20): warning S1118: Add a 'protected' constructor or the 'static' keyword to the class declaration. [C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj]
  _CopyAppConfigFile:
    Copying file from "App.config" to "bin\Debug\StyleCopSonarQubeIssue.exe.config".
  C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(3813,5): error MSB3030: Could not copy the file "obj\Debug\StyleCopSonarQubeIssue.exe" because it was not found. [C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj]
  Done Building Project "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj" (Rebuild target(s)) -- FAILED.
  
  Build FAILED.
  
  "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj" (Rebuild target) (1) -&gt;
  (CoreCompile target) -&gt; 
    Program.cs(10,20): warning S1118: Add a 'protected' constructor or the 'static' keyword to the class declaration. [C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj]
  
  
  "C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj" (Rebuild target) (1) -&gt;
  (CopyFilesToOutputDirectory target) -&gt; 
    C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(3813,5): error MSB3030: Could not copy the file "obj\Debug\StyleCopSonarQubeIssue.exe" because it was not found. [C:\Users\bshah\Documents\Visual Studio 2015\Projects\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue\StyleCopSonarQubeIssue.csproj]
  
      1 Warning(s)
      1 Error(s)
  
  Time Elapsed 00:00:00.96
  </code></pre>
  
  <p>Check the detailed errors \bin\Debug*.exe.RoslynCA.json file and you will see that the warnings that are suppressed in GlobalSuppression.cs are not considered. So the C# compiler does not generate the .exe and msbuild fails.</p>
  
  <p>Any pointers???</p>