1
votes

To clarify: I'm using Visual Studio 2013 Ultimate, Update 2

I'm trying to create a diagnostic analyzer using Roslyn.

The sample code for a diagnostic analyzer (located here declares such an analyzer in this way:

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class DiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer

However when I try to recreate this I cannot find the class DiagnosticAnalyzer in Microsoft.CodeAnalysis.Diagnostics (however IDiagnosticAnalyzer does exist in the same namespace).

Some samples here on StackOverflow instead use (such as this one)

[DiagnosticAnalyzer]
[ExportDiagnosticAnalyzer(DiagnosticId, LanguageNames.CSharp)]
internal class DiagnosticAnalyzer : ISyntaxTreeAnalyzer

However in this case I cannot find the ExportDiagnosticAnalyzer attribute. It seems I'm missing an assembly. Using NuGet I have installed the following packages:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Bcl.Immutable" version="1.1.20-beta" targetFramework="net45" />
  <package id="Microsoft.Bcl.Metadata" version="1.0.12-alpha" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.Common" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.CSharp" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.VisualBasic" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="0.7.4091001-beta" targetFramework="net45" />
  <package id="Microsoft.CodeAnalysis.Workspaces.Common" version="0.7.4091001-beta" targetFramework="net45" />
</packages>

Which as far as I could find, also by inspecting packages.config from the samples, should be everything I need.

When I inspect my C# project's references I have referenced the following non-standard assemblies:

  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeAnalysis.CSharp.Desktop
  • Microsoft.CodeAnalysis.CSharp.Workspaces
  • Microsoft.CodeAnalysis.Desktop
  • Microsoft.CodeAnalysisWorkspaces
  • Microsoft.CSharp

My question is, what am I missing that I can't find all the classes I need to create a diagnostic analyzer?

2
Are you using VS 14 or VS 2013? - JoshVarty

2 Answers

5
votes

So a month or two ago we completely changed our API for doing analyzers. (Don't worry, it was worth the trouble: the new API is a lot nicer.) When you're looking at the current source code in CodePlex, you're seeing stuff so new that we haven't even shipped it yet on NuGet. I strongly recommend you use a Visual Studio "14" CTP and download the templates to match it, which will set you up in a place that should work. Trying to use current NuGet packages against old previews is going to result in sadness.

ExportDiagnosticAnalyzer is obsolete, the [DiagnosticAnalyzer] attribute is sufficient now.

1
votes

VS 2013 no longer receives updates for interfacing between Visual Studio and Roslyn. It's still possible to use VS 2013, but you'll have to use older versions of the NuGet package.

Install-Package Microsoft.CodeAnalysis -Version 0.6.4033103-beta -Pre

If you use the above, you're at a number of disadvantages. The appropriate documentation/samples may no longer exist, and the Roslyn Reference source likely differs in subtle ways.

As Jason suggested, it might be easier to simply move to VS 14. Of course this likely prevents you from using your diagnostics within your organization until VS 14 releases (date still publicly unknown).