Search code examples
c#dependenciesnugetnuget-package

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?


Solution

  • 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.