The NuSpec-generated NuGet file resolves the wrong version of the dependency - how can I get it to do otherwise?
I created a NuSpec file for my .NET project like this:
<?xml version="1.0"?>
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>Author</authors>
<description>My Project</description>
<owners>Me</owners>
<dependencies>
</dependencies>
</metadata>
</package>
My project also has two NuGet dependent dependencies:
<package id="Autofac" version="3.5.2" targetFramework="net451" />
<package id="Autofac.Extras.NLog" version="1.2.3" targetFramework="net451" />
When I create a NuGet package for my project using this NuSpec, NuGet is smart enough to take advantage of these additional dependencies. When I install my NuGet package to a new project, I also get dependencies Autofac
and is Autofac.Extras.NLog
also referenced and automatically inserted into the packages.config file for my new project.
However ... the version of Autofac I am getting is wrong. Instead of version, 3.5.2
I get version 2.6.1.841
:
<package id="Autofac" version="2.6.1.841" targetFramework="net451" />
<package id="Autofac.Extras.NLog" version="1.2.3" targetFramework="net451" />
Now Autofac.Extras.NLog
has a dependency on β₯ 2.2.4.900
(at the time of writing). I have two questions:
- It looks like NuGet runs the
Autofac.Extras.NLog
Autofac dependency first by installingAutofac 2.6.1.841
. When he comes to the execution of my Autofac depdency project, he sees that Autofac is already installed and therefore does nothing. How can I get NuGet to resolve Autofac's version dependency3.5.2
? - Even though NuGet does resolve the "wrong" NuGet dependency (at least for my purposes), why does it resolve
2.6.1.841
and not2.2.4.900
which is the minimum version specified in the dependencyAutofac.Extras.NLog
?
source to share
The solution is to add these dependencies to the NuSpec file:
<?xml version="1.0"?>
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>Author</authors>
<description>My Project</description>
<owners>Me</owners>
<dependencies>
<group targetFramework="net451">
<dependency id="Autofac" version="3.5.2"/> <!-- EXTRA DEPENDENCY -->
</group>
</dependencies>
</metadata>
</package>
It would be good to know if there is a solution out there where you don't need to maintain the NuSpec file every time there are dependency version changes in the project you are packaging.
source to share
you can limit the version of the artifact by reference by specifying the version number in square brackets. please find a sample below
<package id="Autofac" version="[3.5.2]" targetFramework="lib/net45" />
edit your packages.config file with the above line and see if that works. !!
source to share
It looks like Nuget 3.5 (now in Beta) has finally solved this problem. I tested my project and the dependencies were calculated correctly (Nuget 3.4 did not calculate them correctly).
This is a pull request: https://github.com/NuGet/NuGet.Client/pull/632/files
source to share