C
C#2y ago
Poyo

✅ DiagnosticSuppressors and Visual Studio

In my .NET 7 project I have implemented a dependency injection framework that uses an attribute to load cached dependencies into a private method. However, code analysis does not know that the attribute indicates that the method will be implicitly used and will flag it as unused (IDE0051). I have written a DiagnosticSuppressor that in theory should suppress that warning if the method in question is tagged with my custom attribute, but it doesn't do anything just existing in my project. Is there something I need to do to hook it up to Visual Studio?
13 Replies
333fred
333fred2y ago
DiagnosticSuppressors are just a special form of analyzer They need to be in an analyzer project, referenced from your main project as analyzer, etc Frankly, that's much more work than a #pragma warning disable would be
Poyo
Poyo2y ago
well, that wouldn't be a bad idea considering i'll also need to have other analyzers. however, my understanding is that the only way to operate here is to build a vsix or nupkg of my CA project and add that to the main project. is that correct?
333fred
333fred2y ago
You can potentially do a project reference, but it's not the best experience
Poyo
Poyo2y ago
is there any documentation on how this works?
333fred
333fred2y ago
Not great documentation, unfortunately $ig covers the incremental generator model
333fred
333fred2y ago
The .NET Compiler Platform SDK (Roslyn APIs)
Learn to use the .NET Compiler Platform SDK (also called the Roslyn APIs) to understand .NET code, spot errors, and fix those errors.
333fred
333fred2y ago
But I wouldn't call our docs comprehensive Feel free to ask questions in #roslyn
Poyo
Poyo2y ago
alright, thank you!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Poyo
Poyo2y ago
okay, so i'm still having trouble here. i've got my analyzer reference:
<ProjectReference Include="..\Chungy.CodeAnalysis\Chungy.CodeAnalysis.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\Chungy.CodeAnalysis\Chungy.CodeAnalysis.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
but i'm still getting this warning:
An instance of analyzer Chungy.CodeAnalysis.DependencyLoaderDiagnosticSuppressor cannot be created from C:\Users\Poyo\Repositories\Chungy\Chungy.CodeAnalysis\bin\Release\netstandard2.0\Chungy.CodeAnalysis.dll: Could not load type 'Chungy.CodeAnalysis.DependencyLoaderDiagnosticSuppressor' from assembly 'Chungy.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'..
An instance of analyzer Chungy.CodeAnalysis.DependencyLoaderDiagnosticSuppressor cannot be created from C:\Users\Poyo\Repositories\Chungy\Chungy.CodeAnalysis\bin\Release\netstandard2.0\Chungy.CodeAnalysis.dll: Could not load type 'Chungy.CodeAnalysis.DependencyLoaderDiagnosticSuppressor' from assembly 'Chungy.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'..
Chungy.CodeAnalysis.DependencyLoaderDiagnosticSuppressor is a very simple class and Chungy.CodeAnalysis.csproj looks like this: https://gist.github.com/Poyo-SSB/95da26a7b60611399a83a0731cfa415a i've tried targeting net7.0 and netstandard2.0, not messing with nrt/language versions, etc. and i'm kind of at a loss what could be causing the error at this point oh, yeah, also worth mentioning: i'm on the latest vs (17.4.5), tried all the cleaning and rebuilding and deleting obj/bin/.vs and such. lots of reports of similar things happening as a result of visual studio bugs but i'm assuming this isn't one of those?
333fred
333fred2y ago
Without more details, it's rather hard to answer. What does your analyzer look like? You also might want to close this thread and ask in #roslyn, so there's more than just me paying attention to it
Poyo
Poyo2y ago
will do, thanks!