Setting Up Visual Studio 2015 CTP 6 for Diagnostic Analyzer Development
About Diagnostic Analyzers
Diagnostic analyzers are a new extension point in Visual Studio 2015, enabled by the new .NET Compiler Platform (codename: Roslyn). They are custom pieces of code, analyzing the source code, which run at compile time, but are also scheduled in the background inside Visual Studio when the source code changes. Results of the analysis are warnings and errors treated the same way as the ones built into the C# (or Visual Basic) compiler.
In the past such code analysis could only be added to Visual Studio using high profile extensions, such as ReSharper, CodeRush and JustCode which had to reimplement the compiler themselves. Each one of them included their own custom API for writing plugins. Such market fragmentation of course resulted in pretty small number of available plugins.
Thanks to Roslyn the price of entry for performing custom code analysis has significantly reduced, hence there are already many diagnostic analyzers available, both by Microsoft (FxCop Analyzers, Code Analysis for Azure) and open source initiatives (.NET Analyzers, Code Cracker, C# Essentials), although the final version of Visual Studio 2015 has not even been released yet. I can imagine a future, where library authors will include diagnostic analyzers as a guidance to their users and larger development companies will develop internal diagnostic analyzers to enforce their coding style.
Installing Required Software
I've already experimented with diagnostic analyzers in the first Visual Studio 2015 Preview, but since I'm going to speak about this subject at the upcoming local conference, I had to configure a working environment using the latest Visual Studio 2015 CTP 6. Although I knew exactly what I needed, it turned out quite a hassle to get everything up and running, so here's a complete list of required software. Just keep in mind that it is very likely going to change with the next release of Visual Studio 2015.
- Obviously you first need to install Visual Studio 2015 CTP6.
- To add support for building VSIX packages (installers for Visual Studio extensions) you will also need to install Microsoft Visual Studio 2015 SDK. Diagnostic analyzer template includes a VSIX project and will fail without this component installed. You will also have no simple way to debug your diagnostic analyzer without it.
- .NET Compiler SDK Templates for CTP 6 Visual Studio extension will add the Diagnostic with Code Fix (NuGet + VSIX) project template to Visual Studio which will create a simple working diagnostic analyzer to give you a jump start.
- Not strictly required but highly recommended is the .NET Compiler Platform Syntax Visualizer for CTP 6 Visual Studio extension which gives insight into the syntax tree of the code you are currently editing, as Roslyn compiler sees it. It will prove extremely useful when you start developing your own diagnostic analyzer.
Your First Diagnostic Analyzer
Once you have installed everything, you're ready to create your first diagnostic analyzer project. Just open Visual Studio 2015, create a new project and select Visual C# > Extensibility > Diagnostic with CodeFix (NuGet + VSIX) template in New Project dialog. There's a nice
ReadMe.txt file included in the generated solution. Just follow the instructions in the second paragraph to try out the generated code analyzer: make sure the VSIX project is set as startup project and press
F5 to start debugging.
This should open a new instance of Visual Studio 2015 with the code analyzer already deployed to it. Create a new class library project inside it and you should already see you code analyzer in action, suggesting you to make the type name all upper case: