BizzyBob
BizzyBob

Reputation: 14740

How to correctly specify version range for dependency..?

I'm creating an SDK and utilize Newtonsoft.Json. I want to allow any version of 11 and any version of 12, so I set my package ref like this:

<PackageReference Include="Newtonsoft.Json" Version="[11,13)" />

However, when I attempt to install my SDK into another existing solution, installation fails with the following error:

NU1603: MySampleSdk 0.0.1 depends on Newtonsoft.Json (>= 11.0.0 && < 13.0.0) but Newtonsoft.Json 11.0.0 was not found. An approximate best match of Newtonsoft.Json 11.0.1 was resolved.

Isn't 11.0.1 within >= 11.0.0 && < 13.0.0?

What am I missing here?

Upvotes: 2

Views: 853

Answers (2)

UяošKoт
UяošKoт

Reputation: 464

While that is not your problem and the solution to your problem indeed is within another answer, it still is the question asked, so...

When referring to package dependencies, NuGet supports using interval notation for specifying version ranges, summarized as follows:

Notation    Applied rule    Description
1.0 x       ≥ 1.0           Minimum version, inclusive
[1.0,)      x ≥ 1.0         Minimum version, inclusive
(1.0,)      x > 1.0         Minimum version, exclusive
[1.0]       x == 1.0        Exact version match
(,1.0]      x ≤ 1.0         Maximum version, inclusive
(,1.0)      x < 1.0         Maximum version, exclusive
[1.0,2.0]   1.0 ≤ x ≤ 2.0   Exact range, inclusive
(1.0,2.0)   1.0 < x < 2.0   Exact range, exclusive
[1.0,2.0)   1.0 ≤ x < 2.0   Mixed inclusive minimum and exclusive maximum version
(1.0)       invalid         invalid

A few examples:

<!-- Accepts 1.3.2 up to 1.4.x, but not 1.5 and higher.
     Will resolve to the smallest acceptable stable version. -->
<PackageReference Include="ExamplePackage" Version="[1.3.2,1.5)" />

<!-- Accepts any version above, but not including 4.1.3. Could be
     used to guarantee a dependency with a specific bug fix. 
     Will resolve to the smallest acceptable stable version.-->
<PackageReference Include="ExamplePackage" Version="(4.1.3,)" /

More at Microsoft.com: Package versioning

Upvotes: 1

zivkan
zivkan

Reputation: 14961

If you read the NuGet message carefully, it's just telling you "hey, just FYI, version X was requested, but it doesn't exist, so I used version Y instead".

If you look at the docs for NU1603, you'll notice this is a warning, not an error. If your build/restore is failing, it means that your project has opted in for treating warnings as errors.

So, from the point of view of the project that's consuming the project, it can be solved by not asking NuGet error on warnings. Alternatively you could use <NoWarn>$(NoWarn);NU1603</NoWarn> to ignore all instances of this warning.

From a package author point of view, you'd be kinder to your users if you make sure the minimum version is always a version that exists. Newtonsoft.Json 11.0.0 doesn't exist, so make the minimum version 11.0.1 instead.

Upvotes: 3

Related Questions