I'm experimenting with creating a NuGet package. I can create a package from a stand-alone class library project and it works fine. However, I'm seeing an error when I try to create a package from a class library project that references another class library project in the same solution.
I'm trying to create a NuGet package from a .NET Core 3.1 class library project, MyPackage
, which references another .NET Core 3.1 class library project, ReferencedClassLibrary
, in the same solution.
When I pack the MyPackage
project (via Visual Studio Solution Explorer > right click the project file > Pack) a *.nupkg file is created in the bin\debug folder. If I copy that *.nupkg file to the local NuGet package source I set up for testing, I can install the package into another solution.
However, during install of that NuGet package into another solution an error message is displayed:
NU1101: Unable to find package ReferencedClassLibrary. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, NuGet Personal Package Source, nuget.org
where "NuGet Personal Package Source" is the name of my local package source.
How do I include ReferencedClassLibrary
as part of NuGet package MyPackage
? Do I need to use NuGet.exe to pack the project with its dependencies or is there a way to do it via Visual Studio?
CodePudding user response:
If we assume that ReferencedClassLibrary
is <ProjectReference>
, then the expectation is that a ReferencedClassLibrary
will also be packaged as a nupkg, in a way that is available to the consumer - which will then resolve the transitive dependencies automatically. If you want to embed the additional dll in the same package, things get much more complicated, as that is not the "simple mode" way that is expected. At that point, you might be able to convince the .csproj to embed it, but you're more likely to end up having to write the nuspec by hand. Unless you welcome and embrace this, just ship ReferencedClassLibrary
as a nupkg.
CodePudding user response:
I've gone down a bit of rabbit hole reading about this and wanted to summarize what I found.
As Marc Gravell states in his answer, the expected mechanism for including a referenced project is to make it its own NuGet package. This is why dotnet pack
and packing via Visual Studio Solution Explorer don't give the option of bundling the referenced project into the package.
This is mentioned explicitly in the dotnet pack documentation:
NuGet dependencies of the packed project are added to the .nuspec file, so they're properly resolved when the package is installed. If the packed project has references to other projects, the other projects are not included in the package. Currently, you must have a package per project if you have project-to-project dependencies.
(emphasis mine)
There is a feature request in the NuGet project in GitHub for the option to bundle a referenced project into a package. See here: https://github.com/NuGet/Home/issues/3891. The feature request has languished for 6 years but surprisingly it hasn't been closed so there is vague hope for those who would like this feature. Presumably if this feature were implemented the new functionality would be added to dotnet pack
.
For those who would like to bundle a referenced project into a package without waiting for the feature request to be implemented, I've verified it's possible with
nuget pack <project file> -IncludeReferencedProjects
This works with .NET Core 3.1 projects as well as .NET Framework projects. Unfortunately nuget.exe
appears to ignore the package information embedded in a project file. If you don't supply a *.nuspec file nuget pack
will fall back on defaults, like using the project name for the package name.
If you would like to bundle a referenced project into a package now via dotnet pack
, rather than nuget pack
, this older Stackoverflow question gives some ideas, involving manually editing the project file: Build NuGet Package automatically including referenced dependencies I haven't tried any of these ideas to see if they work.
Here is another Stackoverflow answer with another edit of the project file to allow dotnet pack
to bundle referenced projects into the package: Packaging project reference in nuget : .net core Once again, I haven't tried this to see if it works.