Skip to content

Refresh NuGet static state in a dedicated task before restore tasks run and allow RestoreTask to be multithreadable - #7551

Merged
nkolev92 merged 3 commits into
NuGet:devfrom
JanProvaznik:dev-JanProvaznik-refreshStaticStateTask
Jul 15, 2026
Merged

nkolev92 merged 3 commits into
NuGet:devfrom
JanProvaznik:dev-JanProvaznik-refreshStaticStateTask

Conversation

@JanProvaznik

@JanProvaznik JanProvaznik commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Bug

item 4 NuGet/Home#14958

Description

Follow-up to #7507 (item 1 of NuGet/Home#14958), addressing the review discussion there about running the static-state reset before the other restore tasks.

#7507 drove the start-of-restore refresh and end-of-restore teardown of NuGet's process-global state from RestoreTask. As discussed on that PR, RestoreTask runs last in a restore — after the Get* collection tasks (e.g. GetRestoreSettingsTask, which reads NuGetEnvironment) have already read environment-derived state — and it does not run at all for dotnet package add, which stops at GenerateRestoreGraphFile / WriteRestoreGraphTask. So the start-of-restore refresh ran too late and missed entry points.

This PR:

  • Adds RefreshNuGetStaticStateTask, which raises StaticState.StartMSBuildRestoreTasks, wired as the first dependency of _GenerateRestoreGraph. Both in-proc restore entry targets (Restore and GenerateRestoreGraphFile) funnel through _GenerateRestoreGraph, which runs exactly once per restore on the entry node, so the refresh now runs once, ahead of every Get* task and RestoreTask / WriteRestoreGraphTask. The _RefreshNuGetStaticState target declares no Inputs/Outputs, so it is never skipped by MSBuild's up-to-date check.
  • Removes the now-earlier StartMSBuildRestoreTasks raise from RestoreTask.Execute; the end-of-restore teardown stays in its finally.
  • Marks RestoreTask [MSBuildMultiThreadableTask] so that under multithreaded MSBuild it runs in the same process as RefreshNuGetStaticStateTask and observes the refreshed state.
  • If a reset handler unexpectedly throws, RefreshNuGetStaticStateTask logs a localized message (not an error/warning) and continues, so an unexpected failure never fails the build.

Opening as draft for design discussion — in particular marking RestoreTask [MSBuildMultiThreadableTask], which reads environment / current-directory / file state and would ordinarily be migrated to TaskEnvironment; here it is marked so it shares the process with the refresh task. See the discussion on #7507.

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.

@JanProvaznik

JanProvaznik commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor Author

this works dotnet/dotnet#7625 as expected and the msbuild part is unblocked, ptal @nkolev92 @zivkan

@JanProvaznik
JanProvaznik marked this pull request as ready for review July 10, 2026 11:18
@JanProvaznik
JanProvaznik requested a review from a team as a code owner July 10, 2026 11:18
@JanProvaznik JanProvaznik changed the title Refresh NuGet static state in a dedicated task before restore tasks run Refresh NuGet static state in a dedicated task before restore tasks run and allow RestoreTask to be multithreadable Jul 10, 2026
@zivkan zivkan self-assigned this Jul 14, 2026
@JanProvaznik

JanProvaznik commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor Author

CI failure analysis — the two Functional Tests (net10.0) legs fail because of a floating test-SDK pin + a .NET 8 servicing publish-lag, not this PR

TL;DR: The only failing legs are Functional Tests on Linux (.NET 10.0) and Functional Tests on MacOS (.NET 10.0). They fail on the exact same 11 tests in Dotnet.Integration.Test (DotnetListPackageTests.*MultiTargetWithAliases* and DotnetPackageUpdateTests.*). This is not caused by this PR — it's an infra issue affecting all of dev, driven by the floating test-SDK pin.

Root cause

The Dotnet.Integration.Test functional tests install their test SDK from build/DotNetSdkTestVersions.txt into .test/dotnet, then run dotnet package update / dotnet list package with it. On dev (and therefore on this branch) that file uses a floating channel:

-Channel 10.0.4xx -Quality daily

So each run installs whatever the latest daily 10.0.4xx SDK is that day. As of 7/15 the daily SDK rolled to a band that resolves the net8.0 targeting packs at version 8.0.30, but those ref packs are not published to nuget.org yet (latest is 8.0.29), so restore fails:

error: NU1102: Unable to find package Microsoft.AspNetCore.App.Ref with version (= 8.0.30)
error: NU1102: Unable to find package Microsoft.NETCore.App.Ref  with version (= 8.0.30)

Confirmed against nuget.org: Microsoft.NETCore.App.Ref 8.0.30 does not exist (latest 8.0.29).

Why it's not this PR

  • The same 11 tests fail on the latest dev builds (def 289, builds 20260714.7–20260714.17) across Linux, macOS, and Windows net10 — without this change.
  • The last green dev build (20260713.21, commit 0e7ab7232) is this branch's exact merge base, and this branch does not contain any later dev commit. The same NuGet code that was green on 7/14 is red on 7/15 purely because the floating daily SDK advanced to the 8.0.30 band overnight.
  • This PR only reorders in-proc _GenerateRestoreGraph restore; it has nothing to do with targeting-pack resolution.
  • All other legs are green: Unit Tests (all OSes), Msbuild.Integration net472, Packaging, Source Build, static analysis.

Why PR #7564's build (1509646) is green

PR #7564 ("[dev] Source code updates from dotnet/dotnet", the darc source-flow) pins the test SDK to a fixed version instead of the floating daily channel:

- -Channel 10.0.4xx -Quality daily
+ -Version 10.0.400-preview.0.26360.120

That pinned SDK predates the 8.0.30 requirement, so its net8.0 targeting packs resolve and the same 11 tests pass. It's the SDK pin, not any product change.

Resolution

Any one of these makes the two legs green — no product-code change is needed:

  1. [dev] Source code updates from dotnet/dotnet #7564 merges into dev (pins the test SDK), then dev is merged into this branch; or
  2. the 8.0.30 ref/targeting packs are published to nuget.org, after which even the floating daily channel resolves.

A requeue before either of those re-installs the same daily SDK and hits the same missing 8.0.30 packs, so it fails identically.

@zivkan
zivkan enabled auto-merge (squash) July 15, 2026 10:32
@JanProvaznik

Copy link
Copy Markdown
Contributor Author

rebasing on current dev

JanProvaznik and others added 3 commits July 15, 2026 13:36
Follow-up to NuGet#7507 (NuGet/Home#14958 item 1). That PR drove the start- and
end-of-restore reset of NuGet's process-global state from RestoreTask, but as
discussed there RestoreTask runs last in a restore - after the Get* collection
tasks (e.g. GetRestoreSettingsTask, which reads NuGetEnvironment) have already
read environment-derived state - and it never runs at all for
dotnet package add, which stops at GenerateRestoreGraphFile /
WriteRestoreGraphTask. So the start-of-restore refresh ran too late and missed
entry points.

Add RefreshNuGetStaticStateTask, which raises StaticState.StartMSBuildRestoreTasks,
wired as the first dependency of _GenerateRestoreGraph. Both in-proc restore
entry targets (Restore and GenerateRestoreGraphFile) funnel through
_GenerateRestoreGraph, which runs exactly once per restore on the entry node, so
the refresh now runs once, ahead of every Get* task and
RestoreTask / WriteRestoreGraphTask. Remove the now-earlier raise from
RestoreTask.Execute; the end-of-restore teardown stays in its finally.

Mark RestoreTask [MSBuildMultiThreadableTask] so that under multithreaded MSBuild
it runs in the same process as RefreshNuGetStaticStateTask and observes the
refreshed state. If a reset handler unexpectedly throws, the task logs a
localized message and continues rather than failing the build.

Co-authored-by: Copilot <[email protected]>
Address PR feedback: instead of swallowing an unexpected exception from the
static-state refresh and logging a localized informational message, log the
exception (with stack trace) via Log.LogErrorFromException and return
!Log.HasLoggedErrors. This matches the error-handling convention used by
StaticGraphRestoreTaskBase and makes an unexpected failure diagnosable and
build-breaking rather than silent.

Remove the now-unused RefreshNuGetStaticState_UnexpectedError resource string
(resx, Designer, xlf) and update the CA1031 suppression justification.

Co-authored-by: Copilot <[email protected]>
auto-merge was automatically disabled July 15, 2026 11:37

Head branch was pushed to by a user without write access

@JanProvaznik
JanProvaznik force-pushed the dev-JanProvaznik-refreshStaticStateTask branch from 9dfd6e9 to 43fd9db Compare July 15, 2026 11:37
@JanProvaznik
JanProvaznik requested a review from nkolev92 July 15, 2026 16:00
@nkolev92
nkolev92 enabled auto-merge (squash) July 15, 2026 17:39
@nkolev92
nkolev92 merged commit 10426ea into NuGet:dev Jul 15, 2026
20 checks passed
JanProvaznik added a commit to JanProvaznik/NuGet.Client that referenced this pull request Aug 14, 2026
The resets added in NuGet#7507 and rewired in NuGet#7551 were scoped to a restore. Every
requirement they serve is scoped to a build:

- Environment variables do not change during a build. MSBuild Server applies the
  client's environment snapshot at the start of each build, so per-restore firing
  refreshes nothing that per-build firing would not.
- The stale-credential repro that motivated this work is two separate MSBuild
  invocations against one server process, which needs the plugin discarded once
  per build.

Per-restore scope bought nothing and cost NuGet/Home#15044, #15045 and #15046.

Collapse StartMSBuildRestoreTasks and EndMSBuildRestoreTasks into a single
BuildEnded event, raised once per build per node from RestoreTask's registered
build-lifetime object. Delete RefreshNuGetStaticStateTask, the
_RefreshNuGetStaticState target and its UsingTask, which existed only to raise
the start event.

Two rules now bind every handler, both documented on the event:

- Invalidate; do not recompute. At the end of build N the process still holds
  build N's environment, so a handler that reads it here caches the value that is
  on its way out. NuGetTestMode, PreviewFeatureSettings and ExceptionLogger
  recomputed eagerly and are converted to compute on first use; the other
  subscribers already invalidated lazily.
- Do not swap a resource that has work in flight. SourceRepositoryDependencyProvider's
  throttle was removed from the scheme in the previous commit for this reason;
  PluginLogger moved to this event because its lifetime is the plugins' lifetime.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 197f186a-e367-42cc-9d89-7c301bfa0843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants