"Analyzer with Code Fix" project template is broken

I found a couple of repositories with working unit tests:

  • https://github.com/dotnet/samples/tree/master/csharp/roslyn-sdk/Tutorials/MakeConst
  • https://github.com/dotnet/roslyn/tree/master/src/Analyzers/CSharp

All of them seem to be using "manual" approach with helper code included in the project. While they don't shed any light on what's going on with project templates and helper code bundled into "beta" nugets, at least they provide a working starting point.

UPDATE: Microsoft has updated its documentation; "Build your first analyzer and code fix" tutorial now has a note in Prerequisites section explaining that there's a bug in the template that should be fixed in Visual Studio v16.7. The note also provides steps for fixing generated projects until then.

UPDATE 2: Followed the steps in the note. In fact, for test sources they are the same as provided by AndrewSilver in his answer. Got the same result, the second test fails. Oh well, back to square one.

UPDATE 3: Ok, I think I got it. Short version: add the following line to your analyzer's Initialize method:


Long version

Spent some time debugging the library. The main culprit appeared to be this line that would declare your expected diagnostic message a subject to exclusion and discard it from the list of expected results, thus failing the test due to mismatch between actual (1) and expected (now 0) diagnostics. Was about to file an issue but decided to check if anything similar already exists. It does, indeed, and it points to another lengthy thread with this solution.

By the way, project's README file provides a few more usage examples.

The roslyn (mstest) packages have moved to a different package repository:

Your nuget.config should look like:

<?xml version="1.0" encoding="utf-8"?>
  <!-- Only specify feed for Arcade SDK (see https://github.com/Microsoft/msbuild/issues/2982) -->
    <clear />
    <add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
    <add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
    <add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <clear />

See for example https://github.com/dotnet/roslyn-analyzers/blob/master/NuGet.config

I've run into the same issue recently. Thank you for mentioning changed nuget URL. After changing it in VS settings, I've been able to compile project by removing .CodeFix part in here

using Verify = Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest.CodeFixVerifier<

and after that by replacing VerifyCSharpDiagnosticAsync with VerifyAnalyzerAsync in the first unit test and VerifyCSharpFixAsync with VerifyCodeFixAsync in the second one.

Unfortunately, one of the unit tests is not working yet. I would appreciate any help with this issue.