I have A library (Let call it project A
) and a Source Generator(B
) and they do not reference each other. A developer will reference both A
and B
in their project(C
). B
looks for specific attributes in C
and generate a code that calls A
. This works when I manually reference both projects in C
but now I want to create a NuGet package containing both A and B so that when you install it you will have both referenced. Below is the csproj for both projects.
Project A
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net6.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace><!-- Add the following properties to generate a package on build and include the source generator as a dependency --><GeneratePackageOnBuild>true</GeneratePackageOnBuild><IncludeBuildOutput>true</IncludeBuildOutput><IncludeSymbols>true</IncludeSymbols><SymbolPackageFormat>snupkg</SymbolPackageFormat><PackageId>$(MSBuildProjectName.Replace(" ", "_"))</PackageId><PackageVersion>1.0.0</PackageVersion><Authors>Nana Akwasi Aforo</Authors><Description> This package provides a .NET library that simplifies the use of dependency injection and factory pattern in your code. It offers helpers that allow you to register and create services with different implementations and scopes easily.</Description><PackageLicenseExpression>MIT</PackageLicenseExpression><RepositoryUrl>https://github.com</RepositoryUrl><RepositoryType>git</RepositoryType></PropertyGroup><ItemGroup><!-- Keep the following item as it is needed for a library project --><PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /></ItemGroup><!-- Add the following item group to include the source generator as a dependency --><ItemGroup><None Include="..\B\bin\Release\netstandard2.0\B.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /></ItemGroup></Project>
Project B
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>netstandard2.0</TargetFramework><EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules><RootNamespace>DependencyInjectionToolkit.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace><GeneratePathProperty>true</GeneratePathProperty><IsRoslynComponent>true</IsRoslynComponent><LangVersion>10</LangVersion></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" PrivateAssets="all" /><PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" /></ItemGroup></Project>
If you extract the NuGet file A is in Lib\net6.0
and B is in analyzers\dotnet\cs
but when you install the package the source generator does not work.As a matter of fact, it does not show up under the Analyzers for the dependency of C
I tried creating my on manifest but that too did not work.
<?xml version="1.0" encoding="utf-8"?><package ><metadata><id>testpackage</id><version>1.0.0</version><title>testpackage</title><authors>author</authors><requireLicenseAcceptance>false</requireLicenseAcceptance><license type="expression">MIT</license><licenseUrl>https://licenses.nuget.org/MIT</licenseUrl><icon>_b81bf80c-8132-4a64-81c5-82cf3cbe999e.png</icon><projectUrl>https://github.com</projectUrl><description>short description</description><tags>tags</tags><repository type="git" url="https://github.com"/><dependencies><group targetFramework="net6.0"><dependency id="Microsoft.Extensions.DependencyInjection.Abstractions" version="7.0.0" exclude="Build,Analyzers" /></group><group targetFramework=".NETStandard2.0" /></dependencies></metadata><files><!-- Include the source generator assembly --><file src="B\bin\Release\netstandard2.0\DependencyInjection.B.dll" target="analyzers\dotnet\cs"/><!-- Include the dependency injection library assembly --><file src="A\bin\Release\net6.0\A.dll" target="lib\net6.0"/><!-- Include any other files you want to include in your package --><file src="README.md" target="contentFiles\any\any\README.md"/><file src="_b81bf80c-8132-4a64-81c5-82cf3cbe999e.png" target="_b81bf80c-8132-4a64-81c5-82cf3cbe999e.png"/></files></package>