Wintellect Blogs

Wintellect.Analyzers for VS 2015 RC and Fixing Analyzer Install Problems with the Default Template Projects

4 May , 2015  

Now that Visual Studio 2015 RC is fresh off the build machines and available for everyone, I’ve updated my Wintellect.Analyzers project ( with full RC support. Go forth and add them to your project so you can get the benefit of advanced compiler analysis and code fixes. To add the analyzers, hit up your Package Manager Console with the following command:

  1. Install-Package Wintellect.Analyzers Prerelease

I guess you could go through the fancy new NuGet window in Visual Studio, but you want to be hard core and use PowerShell in VS!

If you’re interested in writing your own analyzers, as I have discussed previously, you’re immediately going to run into errors and warnings with projects based off the default Roslyn template when you try to install your analyzer masterpiece. Here’s what you’ll see:

Attempting to gather dependencies information for package ‘Analyzer1.1.0.0’ with respect to project targeting ‘.NETPortable, Version=v4.5, Profile=Profile7’
Attempting to resolve dependencies for package ‘Analyzer1.1.0.0’ with DependencyBehavior ‘Lowest’
Resolving actions to install package ‘Analyzer1.1.0.0’
Resolved actions to install package ‘Analyzer1.1.0.0’
Adding package ‘Analyzer1’ to folder ‘c:\junk\code\Analyzer1\packages’
Added package ‘Analyzer1’ to folder ‘c:\junk\code\Analyzer1\packages’
Added package ‘Analyzer1’ to ‘packages.config’
Executing script file ‘c:\junk\code\Analyzer1\packages\Analyzer1.\tools\install.ps1’
Get-ChildItem : Cannot find path ‘C:\junk\code\Analyzer1\packages\Analyzer1.\tools\analyzers\C#’ because it does not exist.
At C:\junk\code\Analyzer1\packages\Analyzer1.\tools\install.ps1:18 char:31
+ … alyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\junk\code\An…ls\analyzers\C#:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Successfully installed ‘Analyzer1’ to Analyzer1.

When a developer is installing your analyzer NuGet package and you get all those errors, that’s not giving them a great sense of confidence. The good news is that it’s relatively easy to fix. Right now the Roslyn templates are not open sourced. I hope they will be before RTM so we can fix this issue. The good news is that it’s not too hard to fix it right now.

Analyzers can be language neutral, or specific to C# or VB. The template assumes you are writing a language neutral analyzer AND language specific analyzers, which is fine, but I’m willing to bet that you are writing a language specific analyzer.

When you open your analyzers .NUSPEC file, you’ll see the following in the files section:

    <file src=*.dll target=tools\analyzers\ exclude=**\Microsoft.CodeAnalysis.*;**\System.Collections.Immutable.*;**\System.Reflection.Metadata.* />
    <file src=tools\*.ps1 target=tools\ />

The first file line is saying that your analyzer will be installed into <packages>\<your analyzer>\tools\analyzers and that’s where language neutral analyzers reside. If you are writing a C# specific analyzer, you want to change the file element to be the following:

    <file src=*.dll target=tools\analyzers\C# exclude=**\Microsoft.CodeAnalysis.*;**\System.Collections.Immutable.*;**\System.Reflection.Metadata.* />
    <file src=tools\*.ps1 target=tools\ />

For a VB specific analyzer, change the C# to VB and you are off to the races. That’s a pretty simple fix! Another alternative would be to change Install.ps1 and Uninstall.ps1 to do a Test-Path to see if there’s any language neutral or language specific analyzers.