Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Visual Studio

Visual Studio: Fix NuGet Restoring a Newer Package Version Than packages.lock.json Specifies Because Locked Mode Was Never Enabled

- 31.10.26 - ErcanOPAK

🛠️ Fix NuGet Restoring a Newer Package Version Than packages.lock.json Specifies Because Locked Mode Was Never Enabled

packages.lock.json exists so a restore resolves to exactly the same package versions on every machine, recording the precise version NuGet picked the first time instead of just the floating range in the csproj. NuGet only treats that file as authoritative, though, when you explicitly ask it to: a plain restore with no extra flag is still free to re-resolve a floating version range to whatever is newest right now, and will quietly rewrite the lock file to match instead of refusing. One developer who restored last week and a CI pipeline that restores today can end up building against two different patch versions of the same dependency, with an identical lock file history on both and nothing in the output calling out that anything changed.

🔎 The Problem

packages.lock.json (committed to source control):
  "Newtonsoft.Json": {
    "type": "Direct",
    "requested": "[13.0.3, )",
    "resolved": "13.0.3"
  }

csproj:
  <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  (but floated elsewhere via Directory.Packages.props as "13.*")

Plain restore (no --locked-mode):
  dotnet restore
  -> resolves 13.0.4 (newer patch released since the lock was committed)
  -> lock file is silently REWRITTEN to match, no warning printed

Teammate's machine, restored last week:
  -> still has 13.0.3 cached, never re-resolved
  -> two machines, two different resolved versions, same lock file history

✅ Fix: Make Restore Actually Enforce the Lock File

  • Pass --locked-mode on dotnet restore (or set <RestoreLockedMode>true</RestoreLockedMode> in the project or a shared Directory.Build.props) so restore fails loudly instead of silently rewriting the lock file when a resolved version would change.
  • In CI, always combine --locked-mode with the restore step that runs before build, so a drifted lock file breaks the pipeline immediately instead of quietly shipping whatever NuGet happened to resolve that day.
  • If you actually want to pick up a newer version on purpose, run a plain dotnet restore (without locked mode) once to update the lock file, review the diff, and commit the updated packages.lock.json alongside the change, instead of deleting the file to make a locked-mode failure go away.

🚨 Why This Is Dangerous

  • A lock file nobody enforces gives a false sense of reproducibility: it looks like a safety net in source control while every actual restore quietly ignores it, so two machines can end up running different package versions with no error, no warning, and no diff showing why behavior differs.

A lock file that nothing enforces is just a snapshot of what restore happened to pick last time, not something that’s actually locking anything until you tell NuGet to fail when reality drifts from it.

— Developer Tip

Related posts:

Visual Studio: Fix a Code Analyzer That Stops Flagging Issues After a NuGet Update

Visual Studio: Fix a Code Analyzer That Stops Flagging Issues After a NuGet Update

Visual Studio Debugger Skips Lines — Optimized Build Trap

Visual Studio Debugger Skips Lines — Optimized Build Trap

Visual Studio: Test Impact Analysis for Smarter Testing

Visual Studio: Test Impact Analysis for Smarter Testing

Views: 10

Post navigation

HTML5: Fix a template Element’s Markup Never Picking Up Page Styles Because Its Content Lives Outside the Document Tree Until Cloned
WordPress: Fix Site Health Reporting the REST API as Broken Because a Security Plugin Blocks the Loopback Request, Not Real Visitor Traffic

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com