UndeadEmo
UndeadEmo

Reputation: 523

Nuget package creation not including referenced dlls

I'm using visual studio 2019 community version and the package manager console. I am using .net core 2.2 and Package References is enabled. I want to be able to create one single nuget pack coreproj1 which will also include the dlls for Coreproj2 and coreProj3, without having to pack CoreProj2 and CoreProj3 as there are many projects and packing them all is not feasible. I've looked online and cannot find a clear solution as not many of them are using package reference

  CoreProj1 references CoreProj2 and CoreProj3 
  CoreProj2 and CoreProj3 do not reference anything

I'm currently using

   dotnet pack "full project path" --output "outputPath" 

When i try to install the package i get the following error (i get the same error for CoreProj3)

  Error NU1101  Unable to find package CoreProj2. No packages exist with this id in source(s): LocalPackages, Microsoft Visual Studio Offline Packages, nuget.org,  TestNuget   source\repos\TestNuget\TestNuget\TestNuget.csproj   1

Upvotes: 2

Views: 4998

Answers (2)

kzu
kzu

Reputation: 1871

Here's the repro:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <PackageId>CoreProj1</PackageId>
    <Description>CoreProj1</Description>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\CoreProj2\CoreProj2.csproj" />
    <ProjectReference Include="..\CoreProj3\CoreProj3.csproj" />
  </ItemGroup>
</Project>

Then both Proj2 and 3 have the same content:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
</Project>

Running dotnet pack on the first results in a package that doesn't have the assemblies for either referenced project.

The fix is to install NuGetizer (disclaimer: this was a project originally authored by Xamarin/Microsoft by myself and then sort of abandoned until I forked it and took ownership):

<ItemGroup>
  <PackageReference Include="NuGetizer" Version="0.5.0" />
</ItemGroup>

After installing it and running dotnet pack again, you get all three projects properly packaged. Moreover, you can also install the dotnet global tool nugetize so you can very quickly inspect what would be packaged, without incurring in build times:

dotnet tool install -g dotnet-nugetize

Then just run nugetize from the project folder:

❯ nugetize
Package: CoreProj1.1.0.0.nupkg
        C:\Temp\corepack\CoreProj1\bin\CoreProj1.nuspec
    Authors    : CoreProj1
    Description: CoreProj1
    Version    : 1.0.0
Contents:
    /lib/
    net5.0/
        CoreProj1.dll
        CoreProj1.pdb
        CoreProj2.dll
        CoreProj2.pdb
        CoreProj3.dll
        CoreProj3.pdb

Which uses subtle coloring in the console to make it nicer:

enter image description here

Upvotes: 4

zivkan
zivkan

Reputation: 15081

Including referenced projects assemblies using the MSBuild pack targets (dotnet pack is effectively just an alias for dotnet msbuld -t:pack) is not supported. nuget.exe pack has a IncludeReferencedProjects argument that does what you wish, but does not work correctly for projects using PackageReference or SDK-style projects (which are always PackageReference).

One possible workaround is to have a build script that first build all your projects, then copies all your assemblies into a nuget package layout, and use nuget.exe pack to pack that directory layout.

Another workaround is to have a nuspec with a bunch of <file src="..." target="lib\<tfm>\"> elements that tells NuGet to pack all the assemblies from their compiled locations.

Another workaround is to use MSBuild extensibility to tell NuGet's MSBuils targets about the assemblies you want packed after build, but before pack. There are some comments in the thread linked above giving examples how it might be done.

Upvotes: 4

Related Questions