C
C#12mo ago
ewilliams

Source Generators In Nuget Packages

I’m working on a source generator and initially thought it wasn’t working. I was adding it to another project via a private nuget package. I ended up including a direct project reference and walla, the generator started kicking in and shows up in the analyzers. I cracked open the Nuget package and noticed there was no lib folder. So what’s the correct way to publish a nuget package for a source generator project? And what’s the correct way to include said nuget into another project.
4 Replies
ewilliams
ewilliamsOP12mo ago
Generator Project
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageOutputPath>..\..\release</PackageOutputPath>
<Version>1.1.1</Version>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageOutputPath>..\..\release</PackageOutputPath>
<Version>1.1.1</Version>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
</ItemGroup>

</Project>
Project Including Nuget
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="C3C.Application" Version="1.0.0" />
<PackageReference Include="C3C.Domain" Version="1.0.0" />
<PackageReference Include="C3C.Drivers" Version="1.0.0" />
<PackageReference Include="C3C.Drivers.Generator" Version="1.1.1" OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>/>

</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="C3C.Application" Version="1.0.0" />
<PackageReference Include="C3C.Domain" Version="1.0.0" />
<PackageReference Include="C3C.Drivers" Version="1.0.0" />
<PackageReference Include="C3C.Drivers.Generator" Version="1.1.1" OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>/>

</ItemGroup>
</Project>
333fred
333fred12mo ago
Also fyi that #roslyn exists and it's the better place to ask questions about source generators
ewilliams
ewilliamsOP12mo ago
Thanks. I did just get this working and will share my solution. Adding the Include DLL => /analyzers path did the trick. Also means nothing special has to happen when the package is pulled into another project.
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\..\docs\c3c_logo.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\..\docs\c3c_logo.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
Want results from more Discord servers?
Add your server