Version Ranges & Resolution
Version="13.0.3" looks like "exactly 13.0.3". In NuGet it isn't: it means 13.0.3 or higher. And when several versions qualify, NuGet doesn't pick the newest, as npm or Cargo would; it picks the lowest one that qualifies. Those two rules, plus "the nearest declaration wins", explain almost every version NuGet ever chose for you.
Range notation
| Written | Meaning |
|---|---|
1.0 |
>= 1.0 (a minimum, inclusive) |
[1.0] |
exactly 1.0 |
[1.0,2.0) |
>= 1.0 and < 2.0 |
(1.0,) |
> 1.0 |
(,1.0] |
<= 1.0 |
1.*, 1.0.* |
floating: the highest matching version |
*-* |
the highest version including prereleases |
Lowest applicable version
mkdir app && cd app
try() {
cat > app.csproj <<XML
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="$1" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q > restore.log 2>&1
resolved=$(grep -o '"Newtonsoft.Json/[^"]*"' obj/project.assets.json | head -1)
printf '%-18s -> %s %s\n' "$1" "$resolved" "$(grep -o 'warning NU1603' restore.log | head -1)"
}
echo "available: $(ls /opt/nuget-feed | grep '^Newtonsoft' | tr '\n' ' ')"
try 13.0.3
try 12.0.1
try '[13.0.3,14.0)'
try '(13.0.3,)'
try '(,13.0.4]'
try '13.*'
try '13.0.*'available: Newtonsoft.Json.13.0.3.nupkg Newtonsoft.Json.13.0.4.nupkg
13.0.3 -> "Newtonsoft.Json/13.0.3"
12.0.1 -> "Newtonsoft.Json/13.0.3" warning NU1603
[13.0.3,14.0) -> "Newtonsoft.Json/13.0.3"
(13.0.3,) -> "Newtonsoft.Json/13.0.4"
(,13.0.4] -> "Newtonsoft.Json/13.0.3"
13.* -> "Newtonsoft.Json/13.0.4"
13.0.* -> "Newtonsoft.Json/13.0.4"
13.0.3resolved to 13.0.3 although 13.0.4 is available: lowest applicable.12.0.1isn't on the feed. NuGet took the lowest version above it, 13.0.3, and raised NU1603 ("depends on Newtonsoft.Json (>= 12.0.1) but … 12.0.1 was not found"). Treat that warning seriously: you're getting a different major version than you asked for.- Ranges without a floating part also resolve to their lowest member. Only floating versions (
13.*) take the highest.
Why lowest? Repeatability: a new release on the feed doesn't change what an existing project restores. The minimum you wrote is what you get, until you edit it. Floating versions give that up for convenience, which is why lock files and Central Package Management restrict them.
Nearest wins, and downgrades
The second rule applies to the graph. If the project references a package directly, that declaration wins over anything requested further down the graph, even if it is lower. NuGet reports that as a downgrade, NU1605, which the SDK treats as an error:
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.14.1" />
</ItemGroup>
</Project>
XML
dotnet restore 2>&1 | grep -E 'NU1605' | awk '!seen[$0]++' | sed 's|/home/learner/app/||'app.csproj : error NU1605: Warning As Error: Detected package downgrade: Microsoft.TestPlatform.ObjectModel from 18.0.1 to 17.14.1. Reference the package directly from the project to select a different version.
app.csproj : error NU1605: app -> Microsoft.NET.Test.Sdk 18.0.1 -> Microsoft.TestPlatform.TestHost 18.0.1 -> Microsoft.TestPlatform.ObjectModel (>= 18.0.1)
app.csproj : error NU1605: app -> Microsoft.TestPlatform.ObjectModel (>= 17.14.1)
Microsoft.NET.Test.Sdk 18.0.1 needs Microsoft.TestPlatform.TestHost 18.0.1, which needs ObjectModel >= 18.0.1. The project asks for 17.14.1 directly. Nearest wins would pick 17.14.1 and break TestHost's minimum, so NuGet prints both paths. Warning As Error: shows the mechanism: NU1605 is formally a warning, and the SDK adds it to WarningsAsErrors for every project. Fix it by raising the direct reference (or removing it, letting the graph decide).
When two transitive paths ask for different minimums of the same package (cousins), NuGet picks the lowest version that satisfies all of them, which is the highest of the minimums.
A resolver in miniature
This model implements the three rules on a small graph: direct references win, cousins take the highest minimum, and each range resolves to its lowest available member.
AVAILABLE = {"Json": ["12.0.3", "13.0.3", "13.0.4"], "Log": ["1.0.0", "1.2.0", "2.0.0"],
"Http": ["3.1.0"], "Cache": ["5.0.0"]}
DEPS = {("Http", "3.1.0"): {"Json": "13.0.3", "Log": "1.0.0"},
("Cache", "5.0.0"): {"Log": "1.2.0"}}
def key(v):
return tuple(int(x) for x in v.split("."))
def lowest_at_least(pkg, minimum):
ok = [v for v in AVAILABLE[pkg] if key(v) >= key(minimum)]
return min(ok, key=key) if ok else None
def resolve(direct):
chosen = dict(direct) # nearest wins: direct pins are final
wants = {} # transitive minimums per package
frontier = [(p, lowest_at_least(p, v)) for p, v in direct.items()]
while frontier:
pkg, ver = frontier.pop()
chosen[pkg] = ver
for dep, minimum in DEPS.get((pkg, ver), {}).items():
wants.setdefault(dep, []).append((minimum, pkg))
if dep not in direct:
best = max((m for m, _ in wants[dep]), key=key)
frontier.append((dep, lowest_at_least(dep, best)))
for dep, reqs in wants.items():
if dep in direct:
for minimum, by in reqs:
if key(direct[dep]) < key(minimum):
print(f" NU1605 downgrade: {dep} {direct[dep]} < {minimum} wanted by {by}")
return chosen
print("A:", resolve({"Http": "3.1.0", "Cache": "5.0.0"}))
print("B:", resolve({"Http": "3.1.0", "Log": "2.0.0"}))
print("C:", resolve({"Http": "3.1.0", "Json": "12.0.3"}))A: {'Http': '3.1.0', 'Cache': '5.0.0', 'Log': '1.2.0', 'Json': '13.0.3'}
B: {'Http': '3.1.0', 'Log': '2.0.0', 'Json': '13.0.3'}
NU1605 downgrade: Json 12.0.3 < 13.0.3 wanted by Http
C: {'Http': '3.1.0', 'Json': '12.0.3', 'Log': '1.0.0'}
- A:
Logis wanted at>= 1.0.0by Http and>= 1.2.0by Cache; the cousins agree on 1.2.0, the lowest version satisfying both. - B: the direct
Log 2.0.0wins over Http's lower minimum. That's an upgrade, so no warning. - C: the direct
Json 12.0.3is below what Http needs: a downgrade, reported like NU1605.
Test yourself
A project has <PackageReference Include="X" Version="2.1.0" /> and the feed holds 2.1.0, 2.2.0 and 3.0.0. Which version is restored?
- 3.0.0
- 2.2.0
- 2.1.0
- Restore fails: no exact match rule
Which notation pins exactly version 2.1.0?
- 2.1.0
- [2.1.0]
- 2.1.*
- (2.1.0)
Your project references Log 1.0 directly; a dependency needs Log >= 1.5. What happens?
- NuGet silently picks 1.5
- Nearest wins picks 1.0, and NuGet reports a downgrade (NU1605, an error by default)
- Both versions are loaded
- The dependency is skipped