The situation is as follows: we have a Visual Studio solution with multiple projects (all are SDK-style, all produce class libraries), some of the projects are common libraries referenced by two other projects. Those two projects should be packed into NuGet packages which need to also include the DLLs produced by the referenced projects (that’s not a problem), however what I’d like is for the those packes to have in their dependencies not only their immediately referenced packages, but also the dependencies of the referenced projects.
To provide a simplified example:
- ProjectA, references Autofac and Newtonsoft.Json package
- ProjectB, references ProjectA, references Serilog package
ProjectB creates NuGet package.
The desired behavior is for ProjectB.nuget to include ProjectA.dll and ProjectB.all, and to list Autofac, Newtonsoft.Json and Serilog as dependencies.
The current setup is similar to the following:
ProjectB has
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..ProjectAProjectA.csproj" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetDependenciesDlls</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="GetDependenciesDlls" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" />
</ItemGroup>
</Target>
That gets one half of the desired result, namely, inclusion of ProjectA.dll in the generated package (it ends up in the lib folder alongside ProjectB.dll. However, the referenced packages of project A are not listed in the dependencies (only the direct references of ProjectB are).
The nuspec file inside the package has
<dependencies>
<group targetFramework="net6">
<dependency id="Serilog" version="3.1.1" exclude="Build,Analyzers" />
</group>
</dependencies>
and I’d like it to be
<dependencies>
<group targetFramework="net6">
<dependency id="Autofac" version="8.0.0" exclude="Build,Analyzers" />
<dependency id="Newtonsoft.Json" version="13.0.3" exclude="Build,Analyzers" />
<dependency id="Serilog" version="3.1.1" exclude="Build,Analyzers" />
</group>
</dependencies>
Of course, it can be achieved by simply making ProjectB reference th same packages as project A, but I’d really prefer for an automatic solution.
So, the question is, can it be done?