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.
In order to fix the assembly load issue for use in the analyzer project you need to do the following:
source.extension.vsixmanifest
Microsoft.VisualStudio.Analyzer
asset with the source being a project in the solution and then select the project you reference in the analyzer projectMicrosoft.VisualStudio.MefComponent
asset with the source being a project in the solution and then select the project you reference in the analyzer projectThe analyzer project should now be able to find the assembly and use it.