简体   繁体   中英

How to autoload Roslyn Analyzer (without codefix)

TL;DR

  • Created VSIX Package
  • Added Analyzer project item
  • Pressed F5.
  • The experimental instance starts but the analyzer is not loaded and can't be debugged.
  • Manually adding the analyzer works.
  • Question: how to autoload the analyzer?

Problem description:

There are lots of tutorials (even official docs) that start with a project template: Analyzer with Code Fix (NuGet + VSIX) . However, in the latest version of .NET Compiler Platform, I don't have such project template.

So, I've created a VSIX project . Then, I have created a Analyzer project item. Note that I don't have a CodeFix item as I don't need to fix code, only show some warnings.

This is what I've got (I've made a few changes):

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MyAnalyzer : DiagnosticAnalyzer
{
    public const string DiagnosticId = "MyAnalyzer";
    internal static readonly LocalizableString Title = "MyAnalyzer Title";
    internal static readonly LocalizableString MessageFormat = "MyAnalyzer";
    internal const string Category = "MyAnalyzer Category";

    internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, true);

    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics 
        => ImmutableArray.Create(Rule);

    public override void Initialize(AnalysisContext context)
    {
        context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.IfStatement);
    }           

    private void Analyze(SyntaxNodeAnalysisContext context)
    {
        IfStatementSyntax ifStatement = context.Node as IfStatementSyntax;

        if(ifStatement == null)
            return;

        context.ReportDiagnostic(
            Diagnostic.Create(
                Rule, 
                context.Node.GetLocation(), 
                "Hey, this is an IF statement."));
        }

    }
}

That's it. The project contains only this file, a .vsixmanifest and a packages.config .

When I run the project in debug mode (with F5), the experimental instance is loaded and I can see the package in Extensions and Updates . However, no breakpoints are being hit ( no symbols loaded ) and the analyzer is not visible in the list of analyzers (in solution explorer).

Then I do the following:

  • Rename .vsix file in output folder to .vsix.zip and open it
  • Extract the assembly DLL from the zip
  • Right-click analyzers in solution explorer
  • Manually browse/add the analyzer's assembly DLL

Then all of a sudden, symbols are loaded and the debugger stops on breakpoints.

Is there any way to load analzyer automatically in the experimental instance? Am I missing some configuration?

The VSIX template is available from the Visual Studio gallery .

I suspect that what's missing from your vsix project is the correct list of components in the VSIX package manifest. You need to define both a MefComponent and an Analyzer in your list of assets:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM