I'm writing an Analyzer and a CodeFixProvider, and am at the stage of writing some tests to confirm it's working, and to also help me debug it while I'm building it out further.
I've used the template from Visual Studio where it creates you various projects: Analyzer, CodeFix, Package, VSIX and Unit Tests, so everything should be hooked up properly because I haven't changed any of that setup.
When I'm writing tests and calling VerifyCodeFix, I can debug into my analyzer and see exactly what it's doing, and it's reporting a Diagnostic error fine, as I want it to.
Now my code fix should be picking up that diagnostic error and then performing a fix. However my test fails fast after the Analyzer finishes, and none of my breakpoints in my code fix provider are hitting, apart from the FixableDiagnosticIds array property getter. And that is returning the DiagnosticId that my Analyzer is returning.
Other than that, it doesn't seem to fire. The RegisterCodeFixesAsync method never seems to hit.
Does anyone know why?
Thanks!
Analyzer contains this (which I can confirm is being hit through debugging:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MissingDependsOnAttributeAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
...
var properties = new Dictionary<string, string>
{
["FullName"] = namedTypeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Replace("global::", string.Empty)
}.ToImmutableDictionary();
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), properties, namedTypeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)));
And then my code fix:
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MissingDependsOnAttributeCodeFixProvider)), Shared]
public class MissingDependsOnAttributeCodeFixProvider : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(MissingDependsOnAttributeAnalyzer.DiagnosticId); }
}
public sealed override FixAllProvider GetFixAllProvider()
{
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
...
However nothing in the CodeFix ever hits apart from the FixableDiagnosticsId property getter.