Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Roslyn code analyzer in same solution

I have a solution comprised of several .NET Core projects. I have a few code analysis tasks I'd like to perform that are only applicable to this solution, so it doesn't make sense to put them in a separate repo/solution. Using the appropriate template, I've created three projects for the analyzers:

  • Example.Analyzer
  • Example.Analyzer.Test
  • Example.Analyzer.Vsix

Example.Analyzer.Vsix doesn't compile because Visual Studio requires .NET Framework, but I'm targeting .NET Core for cross-platform use. For now, I'm ignoring that project, but I intend to delete it. Example.Analyzer and Example.Analyzer.Test both target netcoreapp3.0, along with all the other projects in the solution.

I've written my analyzers and the tests pass. However, I'm not sure how to actually use these analyzers from the other projects. I've tried adding Example.Analyzer as a dependency via a ProjectReference, but that doesn't seem to enable the analyzers.

like image 368
Zenexer Avatar asked Mar 19 '19 15:03

Zenexer


People also ask

What is roslyn code analyzer?

. NET Compiler Platform (Roslyn) Analyzers inspect your C# or Visual Basic code for style, quality, maintainability, design, and other issues. This inspection or analysis happens during design time in all open files. Analyzers are divided into the following groups: Code style analyzers are built into Visual Studio.

What are analyzers in Visual Studio?

Live Code Analyzers are used to add custom error messages and warnings that appear live as you're typing, along with automatic code fixes to help you clean them up. They are available as NuGet packages that you add to your projects in Visual Studio 2015.

How do I create a code analyzer?

Create the solutionIn Visual Studio, choose File > New > Project... to display the New Project dialog. Under Visual C# > Extensibility, choose Analyzer with code fix (. NET Standard). Name your project "MakeConst" and click OK.


2 Answers

Literally yesterday I wanted to create a set of Analyzers for a product I am working on and it didn't made sense to do it in a different solution and put on a private Nuget just so I could use them. I was able to find a solution that works perfectly:

  1. In the project that will be analyzed, add a reference to the project that contains the analyzers.
  2. Edit the project file, find the ProjectReference tag you just created and add the properties ReferenceOutputAssembly=false, OutputItemType=Analyzer. It should look similar to this:
<ProjectReference Include="..\..\analyzers\AnalyzersProject\AnalyzersProject.csproj">
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  <OutputItemType>Analyzer</OutputItemType>
</ProjectReference>

This is all that is needed. Now, as soon as your solution is built, your new Analyzers should work. The only caveat is that Visual Studio seems to cache the analyzers, so if you change it, you might need to close and reopen VS for the new/altered analyzers to work.

As much as I would like to, I can't take full credit for this. I did get very close to this solution by myself yesterday, but this morning I found a blog post that simplified things a little (that is is where I took the final solution above from, mine had a few more "unnecessary" lines)

One thing I did in my case was take advantage of a "Directory.Build.props" file in my projects folder to add this project reference. This way, all projects created inside that folder are automagically using the Analyzers I created.

like image 72
Kelps Sousa Alux Avatar answered Oct 03 '22 18:10

Kelps Sousa Alux


From what I have found about it, there are two ways one can add analyzers support to a project: via vsix or nuget package (as in an example here).

The installation of this package as nuget dependency shows that there is specific property to identify the type of a dependency content:

<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

As you can see, one of the asset types is analyzers. Unfortunately, it seems to be unsupported to set "IncludeAssets" property for project dependency even tho it's visible in properties pane.

I would suggest you to try nuget reference instead of project reference.

To get a nuget package out of your project just right click it and select publish. Also, local nuget repository source will be required to put your new nuget there.

like image 37
fenriv Avatar answered Oct 03 '22 18:10

fenriv