0

We are working on a project where we use the C# Analyzer project with the Roslyn API to show diagnostics in code through an extension.

We register a SymbolAction and in the method AnalyzeSymbol we then call upon a static class and method from a referenced project. However, when we try to debug the extension. It does not give a diagnostic warning and outright refuses to do anything.

Basically, we have a class library project named Recognize. The Analyzer, which is in a different project, references Recognize. We try to call a static method from Recognize to return a string in Analyzer. The example code will show this.

Both projects are using version netstandard2.0. Recognize also uses language version 12 as opposed to Analyzer which is left to the default.

We only changed the AnalyzeSymbol function which gets auto-generated when you make the "Code analyzer and codefix project". Please note the using Recognize; and Recognize.StaticTest.ReturnString() which instantly returns a string "Test" into a Diagnostic:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Recognize;
using System;
using System.Collections.Immutable;
using System.Diagnostics;

namespace Analyzer
{
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class Analyzer : DiagnosticAnalyzer
    {
  
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
        }

        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            context.ReportDiagnostic(Diagnostic.Create(SingletonDiagnosticDescriptor, context.Symbol.Locations[0], "Test"));
        
            context.ReportDiagnostic(Diagnostic.Create(SingletonDiagnosticDescriptor, context.Symbol.Locations[0], Recognize.StaticTest.ReturnString()));
        }
    }
}

It works fine without running methods from Recognize. That is the first ReportDiagnostic. Trying to do anything with Recognize does not give us any diagnostics.

For unknown reasons debugging refuses to work for any of us. We had to resort to commenting out and in lines of code and trying all kinds of things to get a referenced project to work.

For example, making an internal class in the Analyzer project and attempt to run the static method from Recognize there. This did actually allow the first ReportDiagnostic to appear.

We managed to get an error message by doing the above and encapsulating the entire block of code that calls the internal class in a Try Catch:

Could not load file or assembly 'FuckRoslyn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system could not find the file

Not sure how to we could resolve this though

Debugging opens a special development Visual Studio 2022 with the extension installed. None of our breakpoint hits, nor do we get a stacktrace or anything in Output in the original Visual Studio that relates to our extension. The extension solution has both the Analyzer and Recognize project.

It just seems to not want to allow anything from outside. We're honestly pretty lost on what to do next here as we assumed we could just send some symbols over to Recognize to attempt to run a few function on it and return a diagnostic.

2
  • 1
    Which project is referencing which other project? Can you provide more details? Do you get any error messages in the console? What exactly are you trying to achieve? Note that analyzers target .NET Standard 2.0, which means that only projects that have the same target can be referenced by your analyzer project. Commented Dec 6, 2023 at 18:44
  • @Julian Information added and clarified! Commented Dec 6, 2023 at 22:30

1 Answer 1

0

In order to fix the assembly load issue for use in the analyzer project you need to do the following:

  1. Go to your Extension project (VSIX)
  2. Open source.extension.vsixmanifest
  3. Go to assets
  4. Add a Microsoft.VisualStudio.Analyzer asset with the source being a project in the solution and then select the project you reference in the analyzer project
  5. Add a Microsoft.VisualStudio.MefComponent asset with the source being a project in the solution and then select the project you reference in the analyzer project

The analyzer project should now be able to find the assembly and use it.

Sign up to request clarification or add additional context in comments.

1 Comment

is there is a way to use the roslyn analyzer in a class lib instead if vsix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.