ערוך

שתף באמצעות


NuGet Warning NU5128

Scenario 1

Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:

  • Add a dependency group for .NETStandard2.0 to the nuspec

Issue

The lib/<tfm>/ or ref/<tfm>/ directory in the package contains at least one file for the Target Framework Moniker (TFM) specified in the warning message. However, no dependency group exists for this TFM in the nuspec file. This may cause package consumers to believe the package is not compatible with the TFM, even if the package does not have dependencies. If the package has undeclared dependencies, the project using the package will experience runtime errors.

Solution

  • Run NuGet's pack target on the project

If possible, use NuGet's MSBuild pack target, as it automatically matches assembly TFMs with dependency groups from the project's target frameworks. Note that your project must use PackageReference for its own NuGet dependencies. If your project uses packages.config, you need to use nuget.exe pack and a nuspec file.

  • Manually edited nuspec file

If you are using a custom nuspec file, we recommend each TFM for which lib/ or ref/ assemblies exist should have a matching dependency group, even if the dependencies are the same as the next compatible TFM. For example, if a package contains netstandard1.0 and netstandard2.0 assemblies, and the dependencies are the same for both, we recommend both TFMs be listed as dependency groups with duplicate dependency items.

Note that the TFM identifier used in the assembly paths use a different format to the TFM identifier used in dependency groups. The warning message specifies the correct name to use in the dependency group. If your package does not have any dependencies for that target framework, use an empty group. For example:

<package>
  <metadata>
    ...
    <dependencies>
      <group targetFramework=".NETFramework4.7.2" />
    </dependencies>
  </metadata>
  ...
</package>
  • Remove the lib/ or ref/ files

If you do not wish your package to be compatible with the reported TFM, modify your project such that no lib/<tfm>/ or ref/<tfm>/ files are in the package for that TFM. For example, if the warning says to add a dependency group for .NETFramework4.7.2 to the nuspec, then remove any lib/net472/* and ref/net472/* files from your package.

Scenario 2

Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:

  • Add lib or ref assemblies for the netstandard2.0 target framework

Issue

The nuspec file has a dependency group for the reported Target Framework Moniker (TFM), but no assemblies exist for this TFM in either lib/ or ref/. If there are assemblies for a compatible TFM, the package will still install, but the dependencies might be incorrect for a assemblies used at compile time and could cause the project to fail at runtime.

Solution

  • Run NuGet's pack target on the project

If possible, use NuGet's MSBuild pack target, as it automatically matches assembly TFMs with dependency groups from the project's target frameworks. Note that your project must use PackageReference for its own NuGet dependencies. If your project uses packages.config, you need to use nuget.exe pack and a nuspec file.

  • Manually edit the nuspec file

Add the reported TFM as an additional Target Framework for which your project compiles for, and add the assemblies to the package. If you are using an SDK style project to multi-target multiple TFMs, NuGet's MSBuild pack targets can automatically add assemblies in the correct lib/<tfm>/ folder and create dependency groups with the correct TFMs and dependencies. If you are using a non-SDK style project, you will likely need to create an additional project file for the additional TFM, and modify your nuspec file to copy the output assemblies in the correct location in the package.

  • Add an empty _._ file

If your package does not contain any assemblies, such as a meta-package, consider adding an empty _._ file to the lib/<tfm>/ directories for the TFMs listed in the warning message. For example, if the warning says to add assemblies for the netstandard2.0 target framework, create an empty lib/netstandard2.0/_._ file in your package.

  • Remove the dependency group

If you use a custom nuspec file, remove the dependency group for the reported TFM, leaving only dependency groups for TFMs for which lib/<tfm>/ or ref/<tfm>/ files exist.

  • Remove all dependencies for packages that are not related to assemblies

If your package does not contain any lib/ or ref/ files and is not a meta-package, it likely does not have any dependencies that the package consumer needs. If you are packing with NuGet's MSBuild Pack target, you can set <SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking> in any PropertyGroup in your project file. If you are using a custom nuspec file, remove the <dependencies> element.

  • Other scenarios

This warning was added during NuGet 5.3's development, and first was available in .NET Core SDK 3.0 Preview 9. NuGet/Home#8583 tracks an issue where the warning was being raised in too many scenarios. You can use the NoWarn MSBuild property (add <NoWarn>$(NoWarn);NU5128</NoWarn> to any PropertyGroup in your project file). If you have multiple projects affected, you can use Directory.Build.targets to automatically add NoWarn to all projects.