7

As I understand it there are two ways to distribute and consume roslyn analyzers:-

  1. As a Visual Studio plugin
  2. As a Nuget package

I frequently find myself wanting to enforce certain domain-specific restrictions, along with convenient code-fixes. (For example, "We need Entity Framework lazy-loading, and so every navigation property in the WidgetFrobber.EntityFrameworkEntities namespace should be virtual.")

It's trivial to write a tiny analyzer that fails the build if someone on my team accidentally writes public ICollection<Widget> instead of public virtual ICollection<Widget>, but since this hypothetical analyzer isn't intended to be shared beyond my team (or, in fact, beyond the .sln it's defined in) I'd rather do without distributing a plugin or updating a nuget package whenever I update the analyzer.

  • References -> Add Reference -> Project lets me reference the analyzer's types, but doesn't actually add it as an analyzer.

  • References -> Analyzers -> Add Analyzer -> Browse... expects a .dll rather than a project reference.

Is it possible to reference a Roslyn analyzer inside the .sln that defines it, in the same way that I can reference another project?

Iain Galloway
  • 18,669
  • 6
  • 52
  • 73

2 Answers2

3

You can click Add Analyzer, then add the DLL built by the project.

You'll probably want to add the Release build (except that then you must be Release before you can build Debug).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ah! Presumably if I set up the solution config so that the Debug config builds the Release config of the analyzer, and set the project dependencies so that the analyzer is build first, that should work out? – Iain Galloway Jul 27 '16 at 15:06
  • @IainGalloway: Yes. Alternatively, you could edit the `csproj` to use the current configuration in the path to the referenced DLL (although VS may overwrite that on save). – SLaks Jul 27 '16 at 15:09
  • 1
    I tried this approach in VS2017, but I always get the compiler warning CS8034: Unable to load Analyzer assembly xxxxxxx.dll : Could not load file or assembly 'file:///xxxxxxx.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format. The file is not missing and is in correct format (it can be normally opened with ILSpy). Anyone else had the same problem? – Zvonko Jun 11 '17 at 09:36
  • 1
    @Zvonko: Change the analyzer to Any CPU. – SLaks Jun 11 '17 at 13:48
  • @SLaks But how do I add the analyzer dll in solution level? If I have 100 projects in another solution, do I need to add to them one by one? – joe Sep 06 '19 at 06:47
1

This is how I ended up doing it. Add the following code to the .csproj you want the Analyzer to analyze. This seems to work with both the new SDK-style .csproj and the older version.

<ProjectReference Include="..\Path\To\Your\Analyzer\Analyzer.csproj"
    PrivateAssets="all"
    ReferenceOutputAssembly="false"
    OutputItemType="Analyzer"
/>

Credit to this Blog post: https://www.meziantou.net/referencing-an-analyzer-from-a-project.htm