6

I've written an Roslyn Analyzer for a specific C# Solution and want to add the analyer to all projects in the solution via ProjectReference:

<Project>
    <PropertyGroup>
        <LangVersion>8.0</LangVersion>
    </PropertyGroup>
    <ItemGroup>
        <ProjectReference Include="(MSBuildProjectDirectory)\..\..\..\CodeAnalyzers\CodeAnalyzers\CodeAnalyzers.csproj"
         ReferenceOutputAssembly="false" 
         OutputItemType="Analyzer" />
    </ItemGroup>
</Project>

I placed this inside the Directory.Build.props file.

The Analyzer works, as long as I manually build the analyzer project before building the solution. If I don't do that I get the following warning for each project:

Warning CS8034  Unable to load Analyzer assembly C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll: Could not find file 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'.
System.IO.FileNotFoundException: Could not find file 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'.
File name: 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'

How do I tell Visual Studio to first build the analyzer project, before trying to load the dll? Building afterwards doesn't clear the issue, I suppose because of caching.

In the Project Build Order - dialog the analyzer project is at the top and in configuration manager the checkbox for build is set.

Eike S
  • 300
  • 1
  • 11
  • If you add the `ProjectReference` like this, won't the referenced project have a reference to itself? Do you see any other warnings during the build? – Costin Dec 17 '21 at 16:37

1 Answers1

0

It seems that you have analyzer project in the same solution as your specific C# Solution. More straightforward approach would be to create Nuget package from your separate analyzer solution (use template in VS "Analyzer with Code Fix (.NET Standard)") and then use PackageReference to include your analyzer in your solution. For instance, put in props file or in the csproj file something like:

<ItemGroup>
  <PackageReference Include="YourCustomAnalyzerId" Version="x.x.x" />
</ItemGroup>

and you will get analyzer in all you projects working just fine. Then your analyzer will be listed in Dependencies/Analyzers.

Spaso Lazarevic
  • 906
  • 8
  • 16