I am modifying the default analyzer project that comes from the code analyzer template to try and get it to report at all of the declarations for a partial class.
I have modified the code to:
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
// Find just those named type symbols with names containing lowercase letters.
if (namedTypeSymbol.Name.ToCharArray().Any(char.IsLower))
{
foreach (var location in namedTypeSymbol.Locations)
{
// For all such symbols, produce a diagnostic.
var diagnostic = Diagnostic.Create(Rule, location, namedTypeSymbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
}
In two separate files, I have partial classes like this:
// File1.cs
partial class Foo
{
public string BarString;
}
// File2.cs
partial class Foo
{
public string FooBarString;
}
I put breakpoints on the ReportDiagnostic and am seeing it called for each location, but within Visual Studio it only reports diagnostics within a single file.
If I put multiple implementations of Foo
in a single file (and it happens to be reporting on that files declaration) then I will see both diagnostics reported.
Am I misunderstanding how diagnostics are supposed to be reported or is this a bug? If it is a bug, is it a Roslyn problem or is it a problem with Visual Studio's consumption of Roslyn?
This is a limitation of the V1 implementation of the Visual Studio diagnostic service.
There is an issue in the Roslyn repository to track this issue:
https://github.com/dotnet/roslyn/issues/3748#issuecomment-117231706
From the response in the Github issue: