Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet package shows no dependencies?

I try to make a NuGet package from a .NET 4.7.2 class library (VS2017), but the resulting NuGet package surprisingly shows no dependencies (which is an error).

My setup is like this:

  • My class library is .NET Framework 4.7.2
  • My class library uses another NuGet package (that has dependencies).
  • My class library uses packageReferences in .csproj
  • My class library contains a proper .nuspec file
  • I use nuget.exe pack for creating the package

The nuget.exe pack command should automatically fill in the needed dependencies - this also used to be the case earlier (in another project). However, at that time I used packages.config instead of packageReferences with my class library. Does that change anything?

What is going on?

How can I force the system to again include the needed dependencies in my package?

Notes:

  • The package is built by a MSBuild script on our TeamCity build server (without VS2017). It's the build script that calls both "nuget.exe restore" and later "nuget.exe pack" as part of its build logic.

  • MSBuild is version 15.7

  • nuget.exe is version 4.6.2

like image 914
Martin Christiansen Avatar asked Aug 13 '18 12:08

Martin Christiansen


People also ask

Do NuGet packages include dependencies?

Any time a package is installed or reinstalled, which includes being installed as part of a restore process, NuGet also installs any additional packages on which that first package depends. Those immediate dependencies might then also have dependencies on their own, which can continue to an arbitrary depth.

How do I resolve a NuGet package?

Navigate to Tools > Options > NuGet Package Manager > General, and then select the Allow NuGet to download missing packages check box under Package Restore. Enabling Restore NuGet Packages. In Solution Explorer, right-click the solution, and then select Restore NuGet Packages. Selecting Restore NuGet Packages.

Where can you find information about NuGet packages and their dependencies?

You can also find packages in the Dependencies > Packages node of your project in Solution Explorer. After you install a package, you can refer to it in the project with a using statement. (Optional) NuGet has two formats in which a project can use packages: PackageReference and packages. config.


1 Answers

How can I force the system to again include the needed dependencies in my package?

This is a known issue about nuget pack is ignoring dependencies when using PackageReference instead of packages.config.

To resolve this issue, you can use the following workaround, and NuGet team are still actively working on improving this scenario:

To package your C# Class Library which manages your dependencies via PackageReference in the csproj itself,

please add a reference to NuGet.Build.Tasks.Pack ( https://www.nuget.org/packages/NuGet.Build.Tasks.Pack/) and run msbuild /t:pack from the command line.

I have test this workaround, it works fine. To make sure this workaround works fine, we need to pay attention to the following points:

  • Need to add the nuget package NuGet.Build.Tasks.Pack to the project.
  • Need to add properties /p:PackageOutputPath="D:\TesterFolder" -p:Authors=tester
  • Use the command msbuild.exe /t:pack, like: msbuild.exe /t:pack "MyTestLibrary.csproj" /p:PackageOutputPath="D:\TestFolder" -p:Authors=tester

Besides, if you want to use .nuspec file to create the nuget package, you should use the following .nuspec file:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyTestLibrary</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <group targetFramework=".NETFramework4.7.2">
        <dependency id="Microsoft.Owin" version="4.0.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>

    <files>
        <file src="bin\Debug\MyTestLibrary.dll" target="lib\net472\MyTestLibrary.dll" />
    </files>
</package>

Then we could use nuget.exe pack to create the nuget package. But, using this method, we have to manually fill in the needed dependencies in the .nuspec file.

Hope this helps.

like image 81
Leo Liu-MSFT Avatar answered Oct 13 '22 00:10

Leo Liu-MSFT