Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn Analyzers with Jenkins

We're using SonarLint and StyleCop analyzers within Visual Studio 2015. These work great whilst developing, but was wondering if its possible to run these during a Jenkins build?

Ive seen SonarLint.Runner, so I'm assuming this is possible.

like image 442
ScottD Avatar asked Oct 30 '22 06:10

ScottD


1 Answers

I'm not closely familiar with Jenkins, but you could always install the analyzers into the project as NuGet packages and have them generate warnings and errors as a part of the MsBuild build:

  1. Install the following 2 NuGet packages into the project you want to analyze: SonarAnalyzer.CSharp and StyleCop.Analyzers.

Project with analyzers installed

  1. Optionally configure the severity of individual rules and treat warnings as errors if you want your build to fail in case of warnings.

  2. Using MSBuild to compile a project configured like that will result in analyzers warnings and errors surfacing the same way as compiler errors and warnings. You should be able to treat them identically in Jenkins, as well:

    "D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj" (default target) (2) ->
    (CoreCompile target) ->
      Class1.cs(13,17): warning CS0219: The variable 'a' is assigned but its value is never used [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1652: Enable XML documentation output [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,23): warning SA1002: Semicolons must not be preceded by a space. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(2,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(3,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(4,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(5,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(1,1): warning SA1652: Enable XML documentation output [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(5,77): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(17,76): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(18,74): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(28,22): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(32,84): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,19): warning S1854: Remove this useless assignment to local variable "a". [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,17): warning S1481: Remove this unused "a" local variable. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
    
        18 Warning(s)
        0 Error(s)
    
like image 198
Damir Arh Avatar answered Nov 15 '22 06:11

Damir Arh