In a .NET project restored by NuGet, why does a transitive package stay on an older patch, and when does a direct PackageReference cause NU1605?
answer
- the floor wins, not the newest
- direct dependency wins inside its subgraph
- cousins: lowest version satisfying all
- NU1605 is in WarningsAsErrors for C#
- dotnet nuget why prints the paths
basics
~20 sRestore gives each transitive package the lowest version that satisfies every range, so it stays on the floor a dependency declared. A direct PackageReference overrides deeper requests, and if it is lower NuGet reports NU1605, which C# SDK projects treat as an error.
solid answer
~40 sNuGet resolves the graph with **lowest applicable version**: a transitive package lands on the smallest version that satisfies every range asking for it, so newer patches on the feed are ignored. Projects in the same solution can therefore resolve the same package differently, which is the usual 'version drift'. **Direct dependency wins** lets a project's own `PackageReference` override anything deeper in its subgraph — including downward. When the direct version is below what a dependency asked for, restore reports **NU1605 Detected package downgrade**; the C# SDK adds NU1605 to `WarningsAsErrors`, so the build fails. Two packages demanding incompatible exact versions with no direct reference gives **NU1107**; a direct version above a dependency's upper bound gives **NU1608**. `dotnet package list --include-transitive` shows what resolved, and `dotnet nuget why` shows which path pulled it in.
code
bash · 2 linesdotnet package list --project Ledger.Api.csproj --include-transitive
dotnet nuget why Ledger.Api.csproj Microsoft.EntityFrameworkCorego deeper
Remember that restore picks the lowest version that satisfies the ranges, and that adding your own PackageReference overrides what a dependency asked for.
Walk through lowest applicable, direct-dependency-wins and cousin resolution on a concrete graph, and say what NU1605, NU1608 and NU1107 each mean.
Diagnose drift with dotnet package list --include-transitive and dotnet nuget why, and fix it with deliberate direct references or central versions rather than cache clearing.
Decide how a repository keeps versions aligned over time: central versions, pinning policy and who owns upgrades, weighed against the churn of chasing every patch.
## The drift people actually see Take an accounting back office where `Ledger.Api` references `Npgsql.EntityFrameworkCore.PostgreSQL` 10.0.3. At its v10.0.3 tag, that provider's project declares its EF Core dependency as `[10.0.4,11.0.0)`. Meanwhile `Ledger.Reports` references `Microsoft.EntityFrameworkCore` 10.0.12 directly. After restore, `Ledger.Api` runs on EF Core **10.0.4** and `Ledger.Reports` on **10.0.12**, although both live in one solution and nuget.org has had 10.0.12 for months. Nobody downgraded anything; the rules simply produce different answers for different graphs. ## The rules that produce it NuGet's PackageReference restore applies four rules: 1. **Lowest applicable version** — every range resolves to its smallest available member. `[10.0.4,11.0.0)` yields 10.0.4, not the newest 10.0.x. 2. **Floating versions** — only a `*` version looks for the highest match. 3. **Direct dependency wins** — when a package appears at several depths of one subgraph, the version declared nearest the project wins, and the deeper requests in that subgraph are ignored. 4. **Cousin dependencies** — when different subgraphs ask for different versions and no direct reference settles it, NuGet takes the lowest version satisfying all of them. If `Ledger.Api` also referenced a `Ledger.Domain` project that asks for EF Core 10.0.12, the cousin rule would lift `Ledger.Api` to 10.0.12. The resolved graph is written to `obj/project.assets.json`, which the build reads; it is not source-controlled. ## When the direct reference goes down Direct-dependency-wins also works downward. Suppose someone adds this to `Ledger.Api` to "line up" versions: ```xml <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" /> ``` The direct 10.0.1 beats the provider's `>= 10.0.4`, and restore reports **NU1605 Detected package downgrade**, printing both paths. NU1605 is a warning code, but `Microsoft.NET.Sdk.CSharp.props` appends it to `WarningsAsErrors`, so a C# project fails to build. The fix is to raise the direct reference to at least the dependency's floor, or remove it. | Code | What happened | Usual fix | |---|---|---| | NU1605 | a direct (or nearer) version is lower than a deeper package asked for | raise the direct reference | | NU1608 | the resolved version is above a dependency's upper bound or exact pin | widen or suppress deliberately | | NU1107 | two packages demand incompatible versions and nothing direct decides | add a direct reference with the chosen version | | NU1601 | a project's floor was not on the feeds, a higher version was used | publish or correct the floor | ## Diagnosing a drift report - `dotnet package list --include-transitive` lists top-level packages with **Requested** and **Resolved** columns, then every transitive package with the version restore chose. - `dotnet nuget why Ledger.Api.csproj Microsoft.EntityFrameworkCore` prints the dependency paths that bring the package in, per target framework, which tells you whose floor you are sitting on. - Comparing those outputs across projects shows where versions diverge; the cure is a deliberate direct reference, or Central Package Management, rather than deleting `obj` and hoping. Re-running restore never "catches up" on its own: the rules are deterministic, so the same graph gives the same versions until a range in it changes. ## A drift investigation, step by step A payroll bug appears only in `Ledger.Api`, and someone suspects EF Core. A disciplined walk-through looks like this: 1. Run `dotnet package list --include-transitive` in both `Ledger.Api` and `Ledger.Reports` and compare the **Resolved** EF Core versions: 10.0.4 against 10.0.12. 2. Run `dotnet nuget why` on `Ledger.Api` to see that EF Core arrives only through the PostgreSQL provider, whose floor is 10.0.4. 3. Decide which version the suite should run on, and make that a direct, reviewed reference (or a central version) instead of relying on whatever floor a dependency happens to declare. 4. Restore, confirm the new resolved version, and run the tests that failed. The common wrong turns are clearing the NuGet caches, deleting `obj`, or adding a floating `10.0.*` to "get the latest" — the first two change nothing, the third trades drift for non-repeatable builds. ## What to tell an interviewer - Drift is the lowest-applicable rule working as designed, not a cache problem. - A direct reference is the override lever, and it can lower versions as well as raise them. - NU1605 breaking a C# build is intentional: a downgrade below a dependency's floor can mean missing methods at run time.
- Why does C# treat NU1605 as an error when NuGet documents it as a warning?The C# SDK props add NU1605 to `WarningsAsErrors`, because a version below what a dependency was compiled against can surface later as a missing method or type at run time. Failing the build makes the downgrade visible; you can remove it from `WarningsAsErrors` but you are then accepting that risk.
- How would you stop 25 projects drifting apart in the first place?Declare each package's version once with Central Package Management (`Directory.Packages.props`), so every direct reference agrees, and opt into transitive pinning when a transitive package must also be raised. Then upgrades are single-line changes reviewed in one place.
- Does a ProjectReference count as a direct dependency for this rule?No — the packages a referenced project brings are transitive for the consuming project. Their ranges join the graph as another subgraph, so the cousin rule picks the lowest version satisfying them all, unless the consuming project references the package directly.
- NuGet restores the newest version that a transitive dependency's range allows.
- A direct PackageReference can only raise a version, never lower one.
- NU1605 is just a warning, so a downgrade never fails a C# build.
- Every project in one solution resolves the same version of a shared package.
- Deleting obj and restoring again pulls the newest patch of transitive packages.