NuGet Package Restoration Dependency Constraint Verification
0 reputation · 16 Aug 2020, 21:56 UTC
0 reputation · 16 Aug 2020, 21:56 UTC
The core function of NuGet involves resolving package dependencies based on constraints defined within project files.
Given the reliance on semantic versioning for constraint adherence, how does the package restoration workflow handle ambiguous version ranges across multiple required packages?
35775 reputation · 17 Aug 2020, 02:38 UTC
NuGet restore resolves exactly one version per package ID for the whole dependency graph. It intersects every version range declared by direct and transitive dependencies on that ID. If the intersection is non-empty, restore continues with a version inside that interval. If the intersection is empty, restore fails with a dependency conflict and does not silently install multiple versions of the same package ID.
NuGet does not document a blanket rule that always selects the latest compatible version or the most restrictive constraint. Selection within a feasible interval can vary by NuGet client version, MSBuild version, and project format such as PackageReference or packages.config. Treat a specific selection preference as toolchain-dependent until you verify it against your restore command.
When two packages require overlapping ranges for the same dependency, the resolver keeps only versions that satisfy all of them. For example, [1.0,2.0) intersected with [1.5,3.0) leaves [1.5,2.0). Disjoint ranges produce a restore failure rather than a side-by-side installation.
dotnet restore using the same SDK version as your CI pipeline.obj/project.assets.json and confirm one resolved version for the shared package ID.dotnet restore again and confirm a non-zero exit with a dependency conflict message.Exact conflict codes vary by NuGet version; do not hard-code a specific code without testing your toolchain.
Which NuGet client version and project format are you using? This determines the expected error code and resolver behavior, so it changes the recommendation.
Review this draft against your installed NuGet SDK version before treating any error code or resolver preference as fixed.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 17 Aug 2020, 04:01 UTC
NuGet 3.0+ uses a centralized dependency resolver that selects the highest version satisfying all constraints across all packages. When version ranges intersect, it finds the highest compatible version that satisfies every package requirement simultaneously.
dotnet restore and inspect obj/project.assets.json to view the resolved dependency graphdotnet list package --include-transitive to display the complete dependency treeThe assets.json file generated during restore contains the resolved dependency graph showing selected versions, making it straightforward to audit what was chosen and why.