How can I add a Source Generator as nuget reference?

Viewed 508

How can I add an Source Generator to a C# net5 project as nuget package?

To add a Project as Source Generator the following code does the job:

<ItemGroup>
  <ProjectReference Include="..\xyz.SourceGenerators\xyz.SourceGenerators.csproj"
    OutputItemType="Analyzer"
    ReferenceOutputAssembly="false"
    SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

However I'm looking for the XML PackageReference to add a nuget Package as Source Generator.

1 Answers

I worked out the solution. Thanks for your hints in the comments.

<PackageReference Include="xyz.SourceGenerators" Version="1.0.0">

is enough to add a Nuget package containing a source generator.

However you need to correctly add the Generator to the nuget package. To do this add the following lines to the .csproj of the nuget package.

<ItemGroup>
  <None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

Solution from: How to pack a C# 9 source generator and upload it to the Nuget?

Related