Roslyn: Roslyn Code Fix Provider not Working

Created on 11 Aug 2017  Â·  7Comments  Â·  Source: dotnet/roslyn

Version Used:
vs 2015
Steps to Reproduce:

In the Roslyn Code Fix Provider not Working and unable to debug

//////////////////////////////////////Analyzer class starts
namespace SampleCodeAnalyzer
{
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class SampleCodeAnalyzer : DiagnosticAnalyzer
    {

....constants defined for analyzer..
 private static DiagnosticDescriptor Rule1 = new DiagnosticDescriptor(DiagnosticId1, Title1, MessageFormat1, Category1, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description1);

........other code for analysis


}

//////////////////////////////////////code Provider
namespace SampleCodeAnalyzer
{
    [ExportCodeFixProvider(LanguageNames.CSharp, Name = "SampleCodeAnalyzerCodeFixProvider"), Shared]
    public class SampleCodeAnalyzerCodeFixProvider : CodeFixProvider
    {
        private const string title = "Fix Expressions";

        public sealed override ImmutableArray<string> FixableDiagnosticIds
        {
            get { return ImmutableArray.Create(SampleCodeAnalyzer.DiagnosticId1); }
        }

        public sealed override FixAllProvider GetFixAllProvider()
        {
            return WellKnownFixAllProviders.BatchFixer;
        }

        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root =
              await context.Document.GetSyntaxRootAsync(context.CancellationToken)
              .ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the invocation expression identified by the diagnostic.
            var invocationExpr =
              root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf()
              .OfType<InvocationExpressionSyntax>().First();

            // Register a code action that will invoke the fix.
            context.RegisterCodeFix(
              CodeAction.Create(title, c =>
              FixRegexAsync(context.Document, invocationExpr, c), equivalenceKey: title), diagnostic);
        }

        private async Task<Document> FixRegexAsync(Document document,
          InvocationExpressionSyntax invocationExpr,
          CancellationToken cancellationToken)
        {
            var semanticModel =
              await document.GetSemanticModelAsync(cancellationToken);

            var memberAccessExpr =
              invocationExpr.Expression as MemberAccessExpressionSyntax;
          .....
        }
    }
}

Expected Behavior:

on right click the fix provider bulb should appear, which happens but on click of the bulb it should hit the debugger, which doesn't happen. Am i doing anything wrong?

Area-IDE Bug

All 7 comments

I have the same problem.

  • Visual Studio 15.4.3
  • Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.1

I have the same problem as well. The template project in Visual Studio calls the example code fix provider...but any custom providers I write are never instantiated.

  • Visual Studio 15.5.7
  • Microsoft.CodeAnalysis.CSharp.Workspaces 2.4.0

I have the same issue ! The analyzer is working perfectly but code fixes are not "recognized by VS".

Microsoft.CodeAnalysis.CSharp.Workspaces:1.0.0
Visual Studio 15.7.6

it works in run time.. not in debug mode.Thats how i got it right.

Get Outlook for Androidhttps://aka.ms/ghei36


From: jonathand1 notifications@github.com
Sent: Wednesday, August 8, 2018 6:27:49 PM
To: dotnet/roslyn
Cc: Sumangalaknair; Author
Subject: Re: [dotnet/roslyn] Roslyn Code Fix Provider not Working (#21444)

I have the same issue ! The analyzer is working perfectly but code fixes are not "recognized by VS".

Microsoft.CodeAnalysis.CSharp.Workspaces:1.0.0
Visual Studio 15.7.6

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/dotnet/roslyn/issues/21444#issuecomment-411395636, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ATrdpDy7u9p2qg9Ki2d0YmstrrAfMluMks5uOuBNgaJpZM4O0Umx.

I too am finding this issue. I have a code analyzer which works as expected, and a corresponding code fix provider which passes all unit tests

When debugging the analyzer correctly identifies the issue in the code, but code fix doesn't appear when clicking on the light bulb. However, if I install my library into Visual Studio manually (either as an extension or a NuGet package) then it works as expected.

I'm happy to make my full solution available if someone can investigate this in more depth to find what is causing the issue in debugging.

Visual Studio 2017 15.8.6

@DMEvans unfortunately the issue here is typically that the VS hive you are using is in a bad state. See the remarks here for how to reset the experimental hive.

The answer to this question on stack overflow helped me.

What it suggests it that you must specify your DLL as an MEFComponent in the assets section of your vsixmanifest.

This is unlikely to be the problem, if you created your code fix based on the "Analyzer with code fix" template. If you are adding an an analyzer and a code fix to a VSIX project, it might be the error.

Was this page helpful?
0 / 5 - 0 ratings