Packaging transitive analyzers with NuGet
Consider the scenario of ThisAssembly and its referenced packages: the main package is essentially a meta-package so that anyone wanting to leverage all the codegen in all the ThisAssembly.* packages can reference a single one. By default, NuGet pack will create a package that declares the project reference dependencies like so:
1
<dependencies>
2
<group targetFramework=".NETStandard2.0">
3
<dependency id="ThisAssembly.AssemblyInfo" version="42.42.42" exclude="Build,Analyzers" />
4
<dependency id="ThisAssembly.Metadata" version="42.42.42" exclude="Build,Analyzers" />
5
<dependency id="ThisAssembly.Project" version="42.42.42" exclude="Build,Analyzers" />
6
<dependency id="ThisAssembly.Strings" version="42.42.42" exclude="Build,Analyzers" />
7
</group>
8
</dependencies>
Copied!
Note all those exclude. Not good since now it means projects referencing this package will not get the transitive analyzers, which are precisely the point of this meta-package.
The fix is highly non-obvious: you must explicitly state that none of the referenced project assets are to be flagged as private:
1
<ItemGroup>
2
<ProjectReference Include="../ThisAssembly.AssemblyInfo/ThisAssembly.AssemblyInfo.csproj" PrivateAssets="none" />
3
<ProjectReference Include="../ThisAssembly.Metadata/ThisAssembly.Metadata.csproj" PrivateAssets="none" />
4
<ProjectReference Include="../ThisAssembly.Project/ThisAssembly.Project.csproj" PrivateAssets="none" />
5
<ProjectReference Include="../ThisAssembly.Strings/ThisAssembly.Strings.csproj" PrivateAssets="none" />
6
</ItemGroup>
Copied!
This properly allows the transitive build and analyzer assets to be properly installed on the referencing project.
Last modified 1mo ago
Copy link