Home: Dependencies are installed for netstandard2.0 even though targetframework is netstandard1.6

Created on 5 Feb 2018  路  7Comments  路  Source: NuGet/Home

Details about Problem

Goal:
Ceate a multi-targeted NuGet package, to target netstandard1.6 and netstandard2.0.
Note: I chose this very small project: https://github.com/rhyous/BusinessRules

Problem:
Dependencies are installed for netstandard2.0 even though the dependencies are specifically targeted in the nuspec for netstandard1.6 and the project it was installed on chose to use the netstandard2.0 version. It was a .Net 4.6.1 project. So I tried a netstandard2.0 project. It had the same issue.

Expectation:
It seems that those dependencies would only install if the project targets the framework specified in the targetFramework attribute of the dependencies group element.

This seems especially important as NetStandard2.0 needs far less dependencies than NetStandard1.0

NuGet product used (NuGet.exe | VS UI | Package Manager Console | dotnet.exe):

NuGet version (x.x.x.xxx):
4.5.1.4879

dotnet.exe --version (if appropriate):

VS version (if appropriate):

2017, exact version is 15.5.6

OS version (i.e. win10 v1607 (14393.321)):

Windows 8.1

Worked before? If so, with which NuGet version:

Never tested before.

Detailed repro steps so we can see the same problem

  1. Created the following .nuspec
<?xml version="1.0"?>
<package >
  <metadata>
    <id>Rhyous.BusinessRules</id>
    <version>1.0.0</version>
    <title>Rhyous.BusinessRules</title>
    <authors>Jared.Barneck</authors>
    <owners></owners>
    <description>A simple library to make Business Rules obvious as opposed to hidden in obscure lines of code.</description>
    <releaseNotes>
    </releaseNotes>
    <summary>A simple library to make Business Rules obvious as opposed to hidden in obscure lines of code.</summary>
    <language>en-US</language>
    <projectUrl>https://nuget.org/packages/Rhyous.BusinessRules.NuGet</projectUrl>
    <iconUrl>https://nuget.org/Content/Images/packageDefaultIcon-50x50.png</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <licenseUrl>http://opensource.org/licenses/Apache-2.0</licenseUrl>
    <copyright>Copyright Rhyous Publising LLC 2018</copyright>
    <dependencies>
        <group targetFramework="netstandard1.6">
          <dependency id="System.Runtime.Serialization.Formatters" version="4.3.0" />
          <dependency id="System.Threading.Tasks.Parallel" version="4.3.0" />
        </group>
    </dependencies>
    <tags>BusinessRules</tags>
  </metadata>
  <files>
    <file src="lib\" target="lib" />
    <file src="tools\" target="tools" />
  </files>
</package>

Note: The section that I thought would work is the dependencies section.

    <dependencies>
        <group targetFramework="netstandard1.6">
          <dependency id="System.Runtime.Serialization.Formatters" version="4.3.0" />
          <dependency id="System.Threading.Tasks.Parallel" version="4.3.0" />
        </group>
    </dependencies>
  1. Build the NuGet package.

  2. Install the NuGet package on a project that is targetting .net461.

...

Other suggested things

Verbose Logs

Please include verbose logs (NuGet.exe -verbosity detailed | dotnet.exe --verbosity diag | etc...)

1>------ Build started: Project: BusinessRules.NuGet, Configuration: Release Any CPU ------
1> 1 file(s) copied.
1> 1 file(s) copied.
1> BusinessRules.NuGet -> C:\Users\jbarneck\Documents\Projects\Rhyous\Rhyous.BusinessRules\src\BusinessRules.NuGet\bin\Release\Rhyous.BusinessRules.NuGet.dll
1>
1> NuGet Packager 2.0.3
1>
1> Creating backup...
1> Renamed Rhyous.BusinessRules.1.0.0.nupkg to Rhyous.BusinessRules.1.0.0.nupkg.bak
1>
1> Updating NuGet...
1> Checking for updates from https://www.nuget.org/api/v2/.
1> Currently running NuGet.exe 4.5.1.
1> NuGet.exe is up to date.
1>
1> Creating package...
1> NuGet Version: 4.5.1.4879
1> Attempting to build package from 'Package.nuspec'.
1> Id: Rhyous.BusinessRules
1> Version: 1.0.0
1> Authors: Jared.Barneck
1> Description: A simple library to make Business Rules obvious as opposed to hidden in obscure lines of code.
1> License Url: http://opensource.org/licenses/Apache-2.0
1> Project Url: https://nuget.org/packages/Rhyous.BusinessRules.NuGet
1> Tags: BusinessRules
1> Dependencies: System.Runtime.Serialization.Formatters [4.3.0, ), System.Threading.Tasks.Parallel [4.3.0, )
1> Added file '[Content_Types].xml'.
1> Added file '_rels/.rels'.
1> Added file 'lib/netstandard1.6/Rhyous.BusinessRules.dll'.
1> Added file 'lib/netstandard2.0/Rhyous.BusinessRules.dll'.
1> Added file 'package/services/metadata/core-properties/286ee24ba3e1473ba64c4e476ac6f0fa.psmdcp'.
1> Added file 'Rhyous.BusinessRules.nuspec'.
1> Added file 'tools/init.ps1'.
1> Added file 'tools/install.ps1'.
1> Added file 'tools/uninstall.ps1'.
1> Successfully created package 'C:\Users\jbarneck\Documents\Projects\Rhyous\Rhyous.BusinessRules\src\BusinessRules.NuGet\Rhyous.BusinessRules.1.0.0.nupkg'.
1>
1> Publishing package...
1> Repository Url: https://www.nuget.org
1>
1> Pushing Rhyous.BusinessRules.1.0.0.nupkg to the NuGet gallery (https://www.nuget.org)...
1> PUT https://www.nuget.org/api/v2/package/
1> Conflict https://www.nuget.org/api/v2/package/ 946ms
1> Response status code does not indicate success: 409 (A package with ID 'Rhyous.BusinessRules' and version '1.0.0' already exists and cannot be modified.).
1>
1> Exit Code: 0
1>
1> Build Exit Code: 0
1> SUCCESS: Published package.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Sample Project

Very helpful if you can zip a project and paste into this issue!
I chose this very small project: https://github.com/rhyous/BusinessRules

Question

All 7 comments

Dependencies are selected based on the project's target framework.

You can find additional help on consuming packages here: https://docs.microsoft.com/nuget

Dependencies are selected based on the project's target framework.

You can find additional help on consuming packages here: https://docs.microsoft.com/nuget

You realize that in any technical forum or bug repo, posting the top level documentation is an insult, right? Is it common practice for you to insult your users?

I didn't come here to be insulted. I came here for help. I expect a higher level of etiquette and courtesy.

Now, back to my issue . . .

I've been living with a weird issue for a while that I'd like to eliminate.

Here is a more directly relevant part of the documentation. Specifically the Dependency Group section of this link: https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies

Here is what I have:

NuGet Package: Rhyous.BusinessRules
Lib folder

  • netstandard1.6

    • deps



      • System.Runtime.Serialization.Formatters


      • System.Threading.Tasks.Parallel



  • netstandard2.0

    • deps



      • none specified



You can see that only netstandard1.6 has any dependencies in my configuration file that I posted.

    <dependencies>
        <group targetFramework="netstandard1.6">
          <dependency id="System.Runtime.Serialization.Formatters" version="4.3.0" />
          <dependency id="System.Threading.Tasks.Parallel" version="4.3.0" />
        </group>
    </dependencies>

What I understand, according to the documentation, is that the following would be the results of installing the above package to different projects running the following target frameworks:

EXPECTED

  • NetStandard1.6

    • Packages

    • Rhyous.BusinessRules <-- Dependencies are installed

    • System.Runtime.Serialization.Formatters

    • System.Threading.Tasks.Parallel

  • Netstandard2.0

    • Packages

    • Rhyous.BusinessRules <-- No dependencies

  • NetFramwork 4.6.1

    • Packages

    • Rhyous.BusinessRules <-- No dependencies

However, the results were that all packages received the dependencies that were only included in the dependency group for netstandard1.6.

ACTUAL

  • NetStandard1.6

    • Packages

    • Rhyous.BusinessRules <-- Dependencies are installed

    • System.Runtime.Serialization.Formatters

    • System.Threading.Tasks.Parallel

  • Netstandard2.0

    • Packages

    • Rhyous.BusinessRules <-- Dependencies are installed

    • System.Runtime.Serialization.Formatters

    • System.Threading.Tasks.Parallel

  • NetFramwork 4.6.1

    • Packages

    • Rhyous.BusinessRules <-- Dependencies are installed

    • System.Runtime.Serialization.Formatters

    • System.Threading.Tasks.Parallel

I tried adding a fallback group, as specified by the documentation, but there was no change.

    <dependencies>
        <group />
        <group targetFramework="netstandard1.6">
          <dependency id="System.Runtime.Serialization.Formatters" version="4.3.0" />
          <dependency id="System.Threading.Tasks.Parallel" version="4.3.0" />
        </group>
    </dependencies>

If there is an easy solution and this is not a bug, I am more than happy to learn what the solution is.
Is there an undocumented property I am missing?

<dependencies>
  <group targetFramework="netstandard1.6">
    <dependency id="System.Runtime.Serialization.Formatters" version="4.3.0" />
    <dependency id="System.Threading.Tasks.Parallel" version="4.3.0" />
  </group>
  <group targetFramework="netstandard2.0" />
</dependencies>

Ok so the dependencies push to later version. So when netstandard2.1 releases, I wouldn't need another group for it, because it would fallback to the 2.0 group, right?

I have this target frameworks document:
https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies
https://docs.microsoft.com/en-us/nuget/reference/target-frameworks

How would someone know that net461 falls back to use the netstandard1.6 dependencies? Seems like there is a fallback Matrix that might only be documented in code here? Thoughts?
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Frameworks/DefaultFrameworkMappings.cs

So when netstandard2.1 releases, I wouldn't need another group for it, because it would fallback to the 2.0 group, right?

That's correct. For dependencies, assets, and all things in packages NuGet takes the nearest compatible framework found. You can also think of it as the nearest without going over, so if your project is netstandard2.0 then netstandard1.6 is the nearest framework. When you add in an empty group with netstandard2.0 that becomes the nearest. An incompatible framework such as netstandard2.1 would be ignored. The fallback group is only used if no framework groups match at all.

You can find more on framework compatibility here: https://docs.microsoft.com/en-us/dotnet/standard/net-standard

The rules coded in NuGet that you referenced are built on .NET Framework's compatibility rules, and these unfortunately it can be painful to figure these out. Using the chart as a reference is the best way to deal with this as a package author.

Thank you. That link was very useful.

And your solution worked.

OK, you have convinced me that this isn't a bug and does not need a code change. It is just as you said:

painful to figure these out.

Glad it worked for you! I'll close this out, if you hit any other issues let me know.

Was this page helpful?
0 / 5 - 0 ratings