Reputation: 12295
I'm trying to add a Roslyn Analyzer and Code Fix Provider to an existing project, ApiEndpoints.csproj
that is distributed as a nuget package.
When consuming the nuget package in a new Visual Studio instance, the Analyzer is clearly working, but the corresponding Code Fix Provider does not appear int the list of Code Fixes. How can I troubleshoot and get my Code Fix Provider to correctly register with Visual Studio?
I originally built the Analyzer & Code Fix Provider in a new project via the Analyzer with Code Fix (.NET Standard) Project Template. When I bundle that into a nuget package, deploy locally, and consume it from a new Studio instance, the Analyzer works and the Code Fix Provider recommends a solution:
However, when I move the Analyzer and Code Fix Provider class to the main ApiEndpoints.csproj
that is bundled with other classes, only the Analyzer seems to register, there's only the option to Suppress, my Code Fix Provider isn't in the list:
ApiEndpoints.csproj
:
I can attach a Visual Studio debugger hosting the Analyzer/Fix Provider to a different instance of Studio that's consuming it. Break points in the Analzyer are getting hit, but nothing in the Code Fix Provider. So I don't think an exception in my Code Fix Provider is preventing registration.
I've gone line-by-line in the ApiEndpints.csproj
vs ApiEndpoints.Analyzers.csproj
and aligned SuppressDependenciesWhenPacking
(true), GeneratePackageOnBuild
(true), IncludeBuildOutput
(true). These changes didn't help.
Restarted both Visual Studio instances numerous times. When initially consuming a nuget package with an Analyzer I've seen the Analyzer not show up until Visual Studio is restarted. But multiple restarts didn't fix the Code Fix Provider
Changed the Analyzer's DiagnosticId
a few times and ensured the Code Fix Provider was referencing it.
Deployed the same Analyzer + CodeFixProvider from different projects (Analyzer with Code Fix (.NET Standard) vs Class Library). Works in one but not the other, which makes me think it's not a problem with the Code Fix Provider, but some kind of registration/plumbing code/config.
How can I troubleshoot and get my Code Fix Provider to correctly register with Visual Studio?
Upvotes: 1
Views: 374
Reputation: 14846
You shouldn't package libraries and analyzers/fixers in the same assembly or even the same package.
Besides forcing your users to take a run-time dependency on the analyzers/fixers and their dependencies, the tooling does not support that use case.
You should package them in their own packages and have the library package take a dependency on the analyzers/fixers package.
Upvotes: 0