Repository navigation
Refresh NuGet static state in a dedicated task before restore tasks run and allow RestoreTask to be multithreadable - #7551
Conversation
|
this works dotnet/dotnet#7625 as expected and the msbuild part is unblocked, ptal @nkolev92 @zivkan |
CI failure analysis — the two
|
|
rebasing on current |
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]>
Head branch was pushed to by a user without write access
9dfd6e9 to
43fd9db
Compare
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
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,RestoreTaskruns last in a restore — after theGet*collection tasks (e.g.GetRestoreSettingsTask, which readsNuGetEnvironment) have already read environment-derived state — and it does not run at all fordotnet package add, which stops atGenerateRestoreGraphFile/WriteRestoreGraphTask. So the start-of-restore refresh ran too late and missed entry points.This PR:
RefreshNuGetStaticStateTask, which raisesStaticState.StartMSBuildRestoreTasks, wired as the first dependency of_GenerateRestoreGraph. Both in-proc restore entry targets (RestoreandGenerateRestoreGraphFile) funnel through_GenerateRestoreGraph, which runs exactly once per restore on the entry node, so the refresh now runs once, ahead of everyGet*task andRestoreTask/WriteRestoreGraphTask. The_RefreshNuGetStaticStatetarget declares noInputs/Outputs, so it is never skipped by MSBuild's up-to-date check.StartMSBuildRestoreTasksraise fromRestoreTask.Execute; the end-of-restore teardown stays in itsfinally.RestoreTask[MSBuildMultiThreadableTask]so that under multithreaded MSBuild it runs in the same process asRefreshNuGetStaticStateTaskand observes the refreshed state.RefreshNuGetStaticStateTasklogs 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 toTaskEnvironment; here it is marked so it shares the process with the refresh task. See the discussion on #7507.PR Checklist