Skip to content

NU1512 error when RestoreLockedMode=true and RestoreForceEvaluate=true - #7451

Merged
martinrrm merged 2 commits into
devfrom
dev-mruizmares-fix8222LockedModeForceEvaluate
Jun 9, 2026
Merged

martinrrm merged 2 commits into
devfrom
dev-mruizmares-fix8222LockedModeForceEvaluate

Conversation

@martinrrm

@martinrrm martinrrm commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Bug

Fixes: NuGet/Home#8222

Description

When restore runs with both RestoreLockedMode=true and RestoreForceEvaluate=true, the two contradictory properties were silently reconciled in favor of RestoreForceEvaluate: the lock file was re-evaluated and regenerated, locked-mode enforcement never ran, and nothing told the user their locked-mode guarantee was being ignored.

Root cause is in EvaluatePackagesLockFileAsync (RestoreCommand.cs): the lock-file read and locked-mode enforcement are both nested behind if (!_request.RestoreForceEvaluate ...), so force-evaluate short-circuits locked mode entirely.

This was previously investigated in #4434 (never merged). That PR proposed a hard error, but the recorded PM/team decision (Apr 2022) was the opposite: RestoreForceEvaluate should continue to override RestoreLockedMode, and a warning should inform the user that the override is happening — failing the build would block the legitimate "edit a csproj, then force-evaluate to refresh the lock file" workflow, especially when RestoreLockedMode is set in a Directory.Build.props.

This PR implements that decision: restore still succeeds and force-evaluate still wins, but it now emits a warning (reusing NU1005) so the ignored locked mode is no longer silent:

RestoreForceEvaluate overrides RestoreLockedMode; the packages lock file will be re-evaluated and regenerated, and locked mode is ignored.

The warning is gated by SdkAnalysisLevel (minimum 11.0.100), per the repo's feature guide which lists "new restore warning" as an in-scope change that must be gated to avoid breaking -warnaserror builds on older SDKs. It is additionally guarded to fire only when a lock file is in play (_isLockFileEnabled) and on the original restore action (IsRestoreOriginalAction), matching the surrounding locked-mode code paths.

The message is intentionally a single concise sentence, consistent with NuGet's existing warning/error style; the detailed explanation and remediation steps belong on the NU1005 documentation page.

Changes

  • Added the gated warning in EvaluatePackagesLockFileAsync, reusing NuGetLogCode.NU1005; restore behavior is otherwise unchanged (force-evaluate wins).
  • Added the localized Warning_RestoreLockedModeAndForceEvaluate string (resx + Designer + regenerated xlf). RestoreLockedMode/RestoreForceEvaluate are marked do-not-localize.
  • Added two functional tests: one asserting the warning is raised and restore succeeds, and one asserting no warning when SdkAnalysisLevel is below the minimum.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@martinrrm
martinrrm requested a review from a team as a code owner June 3, 2026 21:56
@martinrrm
martinrrm requested review from jeffkl and kartheekp-ms June 3, 2026 21:56
@martinrrm
martinrrm requested a review from nkolev92 June 3, 2026 21:57
@martinrrm martinrrm changed the title new NU1020 error new NU1020 error when RestoreLockedMode=true and RestoreForceEvaluate=true Jun 3, 2026
zivkan
zivkan previously approved these changes Jun 4, 2026
kartheekp-ms
kartheekp-ms previously approved these changes Jun 4, 2026
@martinrrm
martinrrm dismissed stale reviews from kartheekp-ms and zivkan via 057ef51 June 4, 2026 22:41
@martinrrm
martinrrm force-pushed the dev-mruizmares-fix8222LockedModeForceEvaluate branch from 9701332 to 057ef51 Compare June 4, 2026 22:41
When restore ran with both RestoreLockedMode=true and RestoreForceEvaluate=true,
the contradictory inputs were silently reconciled in favor of force-evaluate: the
lock file was re-evaluated and regenerated, locked-mode enforcement never ran, and
nothing told the user their locked-mode guarantee was being ignored.

Per the PM/team decision recorded on the earlier attempt (#4434), force-evaluate
should continue to take precedence so that the legitimate "edit a csproj, then
force-evaluate to refresh the lock file" workflow keeps working (locked mode is
often set in a Directory.Build.props). Instead of failing the restore, emit a
warning so the override is no longer silent.

The warning reuses NU1005 and is gated behind SdkAnalysisLevel >= 11.0.100, since
the feature guide treats a new restore warning as an in-scope change that must be
gated to avoid breaking -warnaserror builds on older SDKs. It is additionally
guarded to fire only when a lock file is in play and on the original restore
action, matching the surrounding locked-mode code paths.

Fixes: NuGet/Home#8222

Co-authored-by: Copilot <[email protected]>
@martinrrm
martinrrm force-pushed the dev-mruizmares-fix8222LockedModeForceEvaluate branch from 057ef51 to ca71b8e Compare June 4, 2026 22:59
@martinrrm
martinrrm requested review from kartheekp-ms and zivkan June 4, 2026 23:02
@martinrrm martinrrm changed the title new NU1020 error when RestoreLockedMode=true and RestoreForceEvaluate=true NU1005 error when RestoreLockedMode=true and RestoreForceEvaluate=true Jun 4, 2026

@nkolev92 nkolev92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fine. We need a different code.

@martinrrm
martinrrm force-pushed the dev-mruizmares-fix8222LockedModeForceEvaluate branch from ca71b8e to 466d6d4 Compare June 5, 2026 19:38
Per PR review feedback, give the "RestoreForceEvaluate overrides
RestoreLockedMode" warning its own code rather than overloading the existing
NU1005 error (which has the distinct meaning "Invalid combination of
RestorePackagesWithLockFile and packages.lock.json file"). NU1512 sits in the
restore input-warning range (1500-1599) and lets this warning carry its own
documentation page.

Co-authored-by: Copilot <[email protected]>
@martinrrm
martinrrm force-pushed the dev-mruizmares-fix8222LockedModeForceEvaluate branch from 466d6d4 to 60f1d30 Compare June 5, 2026 19:41
@martinrrm martinrrm changed the title NU1005 error when RestoreLockedMode=true and RestoreForceEvaluate=true NU1512 error when RestoreLockedMode=true and RestoreForceEvaluate=true Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RestoreLockedMode=true & RestoreForceEvaluate=true should error

4 participants