Paket


Analyzers

Roslyn the new open source compiler for C# and Visual Basic introduced the possibility to add new warnings and errors to the build via Analyzers.

Visual Studio 2015 or Visual Studio Code present them visually and also allow Code Fixes: Automated actions to fix a specific warning or error.

A CodeFix appearing in Visual Studio

While analyzers can be installed from the Visual Studio extension gallery, for them to be used in command line builds and shared by all developers they must be installed as NuGet packages for each project.

Each package can either contain only analyzers or analyzers along with other files, allowing a library to include automated migration from now-Obsolete constructs or warnings on incorrect usage.

Referencing analyzers

When a NuGet package containing analyzers is added to a paket.references file, the analyzers will be installed in the corresponding projects automatically by Paket.

Analyzer node in Visual Studio project

Analyzers are only supported by roslyn based compilers, in other versions the nodes added by Paket will be ignored.

Conventions for NuGet packages

Analyzers created by the "Analyzer with Code Fix" template are currently installed via Install.ps1 in Visual Studio that Paket doesn't plan support (See the FAQ for more details)

For them to work in Paket they need to be placed in specific folders inside the NuGet package.

The structure look like :

1: 
2: 
3: 
4: 
5: 
6: 
7: 
|- analyzers
|  |- dotnet
|  |  |- cs
|  |  |  |- My.CSharpAnalyzer.dll
|  |  |- vb
|  |  |  |- My.VisualBasicAnalyzer.dll
|  |  |- My.SharedAnalyzer.dll

The general structure is :

/analyzers/dotnet/language/my_analyzer.dll

This structure is already the one generated by the "Analyzer with Code Fix" template and conform to the documented convention for analyzers to be installed by NuGet in project.json based projects.

If the language is absent from the path the analyzer will be installed in any supported project type (.csproj, .vbproj and .fsproj) if it is present the analyzer will only be installed in the corresponding project type :

  • cs -> .csproj
  • vb -> .vbproj
  • fs -> .fsproj

Remarks:

  • There is currently no way to create analyzers for F#.
  • The dotnet part of the path is in fact the host platform that is targeted. The only one currently in existence is the Roslyn compiler and it require the dotnet framework.
  • The Roslyn developers maintains some analyzers under the roslyn-analyzers project but others have started to appear like Refactoring Essentials or Code Cracker.
Fork me on GitHub