93

I'm creating my first nuget package. I added a dependency with a version that is not the latest version. However, I don't want to update to the latest version of this dependency. Is it possible to instruct it to use the specific version?

<dependencies>
  <dependency id="NHibernate" version="3.2.0.3001" />
</dependencies>

When I install the package I see this:

Attempting to resolve dependency 'NHibernate (≥ 3.2.0.3001)'.

This creates the following when I install the package.

<packages>
  <package id="Iesi.Collections" version="3.2.0.4000" />
  <package id="NHibernate" version="3.2.0.4000" />
</packages>

I'd really like to see something like this: Attempting to resolve dependency 'NHibernate (3.2.0.3001)'.

Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
devlife
  • 15,275
  • 27
  • 77
  • 131
  • 7
    You can do this as answered below. But bear in mind that anyone who uses your package and also uses NHibernate, (or any other dependency where you have specified exact package version) then cannot update this for any reason unless you let them. This is a bad state of affairs. A "locked version" bit us recently: In our case the version-locked package was not NHib but a unrelated package. Locking version may be necessary if there are genuine breaking changes in an upstream library, but in our case that we had there were not; just overuse of this syntax. So if you use it, use it with caution! – Anthony Nov 29 '15 at 15:29

4 Answers4

138

You should be able to force an exact version with brackets:

<dependency id="NHibernate" version="[3.2.0.3001]" />

Full info on the formats you can use are on the NuGet site, here:

http://docs.nuget.org/docs/reference/version-range-specification

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • 1
    You can do this. But bear in mind that anyone who uses your package and also uses NHibernate, then cannot update NHibernate for any reason unless you let them. This is not a great state of affairs. – Anthony Nov 20 '15 at 13:26
  • 3
    @Anthony I think this comment is better on the question that my answer; I was just showing how to do it, not advocating it. The problem you describe isn't easily solved though; if you have two lots of code that just don't work the same version of NH (because of bugs, API differences, whatever), then you're already screwed. The real fix is private dependencies (like Node has), but I can't see .NET ever getting "proper" support for that :( – Danny Tuppeny Nov 20 '15 at 16:57
  • 1
    Yes, I agree and will re-attach the comment. To be clear, a "locked version" bit us recently so it's fresh in my mind. In our case the version-locked package was not NHib but a completely unrelated package. Locking version may be necessary if there are genuine breaking changes in a library, but in our case that we had there were not; just overuse of this syntax. So use it with caution! – Anthony Nov 29 '15 at 15:23
36

From the NuGet docs site, the complete notations:

enter image description here

Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
3

According to http://nuget.codeplex.com/wikipage?title=Dependency%20Resolution and other sources, simply specifying the lower bound as

<dependencies>
  <dependency id="NHibernate" version="3.2.0.3001" />
</dependencies>

will result in the highest revision/patch level of the lowest major/minor version matching that version.

Unless I completely misunderstand the documentation, this would match the highest 3.2.* version but not 3.3.* or greater versions unless no 3.2.* version could be found.

If there is some reason why 3.2.0.3001 is the only version against which you wish to depend, you may find that your package is not compatible with other packages which also depend on NHibernate for example because the other package depends on NHibernate [3.2.0.3002,3.3) which means at least 3.2.0.3002 but lower than 3.3.

Chris Lee
  • 51
  • 1
  • 5
2

From the user side, you can also constrain the upgrade by specifying allowedVersions in the packages.config. http://docs.nuget.org/docs/reference/versioning#Constraining_Upgrades_To_Allowed_Versions

ferventcoder
  • 11,952
  • 3
  • 57
  • 90
  • 1
    As of today, the reference as changed to https://learn.microsoft.com/en-us/nuget/create-packages/dependency-versions#version-ranges – superjos Mar 05 '17 at 14:52