@@ -276,6 +276,7 @@ private void Reset(bool isDisposing = false)
276276 if ( ! isDisposing )
277277 {
278278 _solutionLoadedEvent . Reset ( ) ;
279+ _isFirstRestore = true ;
279280
280281 _workerCts = new CancellationTokenSource ( ) ;
281282
@@ -423,13 +424,24 @@ public async Task<bool> RestoreAsync(SolutionRestoreRequest request, Cancellatio
423424 using ( var restoreOperation = new BackgroundRestoreOperation ( ) )
424425 {
425426 await PromoteTaskToActiveAsync ( restoreOperation , token ) ;
427+ RestoreReadinessResult restoreReadiness = new ( ) ;
428+ // Only do a wait for readiness if this is an on-build restore and it is the first restore. Otherwise, we will just do a restore immediately.
429+ if ( request . RestoreSource == RestoreOperationSource . OnBuild && _isFirstRestore )
430+ {
431+ restoreReadiness = await WaitForOnBuildRestoreReadinessAsync (
432+ waitForSolutionLoadedAsync : WaitForSolutionLoadedAsync ,
433+ isAllProjectsNominatedAsync : ( ) => _solutionManager . Value . IsAllProjectsNominatedAsync ( ) ,
434+ getUtcNow : ( ) => DateTimeOffset . UtcNow ,
435+ token : token ) ;
436+ }
437+
426438 var restoreTrackingData = GetRestoreTrackingData (
427- restoreReason : ImplicitRestoreReason . None ,
439+ restoreReason : restoreReadiness . RestoreReason ,
428440 requestCount : 1 ,
429- projectRestoreInfoSourcesCount : - 1 ,
430- bulkRestoreCoordinationCheckStartTime : default ,
431- projectsReadyCheckCount : 0 ,
432- projectReadyTimings : new List < TimeSpan > ( ) ,
441+ projectRestoreInfoSourcesCount : restoreReadiness . ProjectRestoreInfoSourcesCount ,
442+ bulkRestoreCoordinationCheckStartTime : restoreReadiness . BulkRestoreCoordinationCheckStartTime ,
443+ projectsReadyCheckCount : restoreReadiness . ProjectsReadyCheckCount ,
444+ projectReadyTimings : restoreReadiness . ProjectReadyTimings ,
433445 request . ExplicitRestoreReason ) ;
434446 var result = await ProcessRestoreRequestAsync ( restoreOperation , request , restoreTrackingData , token ) ;
435447
@@ -454,33 +466,16 @@ await Task.WhenAll(
454466 Interlocked . Exchange ( ref _restoreJobContext , new SolutionRestoreJobContext ( ) ) ;
455467 }
456468
469+
457470 private async Task < bool > StartBackgroundJobRunnerAsync ( CancellationToken token )
458471 {
459- // Hops onto a background pool thread
460472 await TaskScheduler . Default ;
461473
462474 var status = false ;
463- // Check if the solution is fully loaded
464- while ( ! _solutionLoadedEvent . IsSet )
465- {
466- // Needed when OnAfterBackgroundSolutionLoadComplete fires before
467- // Advise has been called.
468- if ( await IsSolutionFullyLoadedAsync ( ) )
469- {
470- _solutionLoadedEvent . Set ( ) ;
471- break ;
472- }
473- else
474- {
475- // Waits for 100ms to let solution fully load or canceled
476- await _solutionLoadedEvent . WaitAsync ( )
477- . WithTimeout ( TimeSpan . FromMilliseconds ( DelaySolutionLoadRetry ) )
478- . WithCancellation ( token ) ;
479- }
480- }
475+ await WaitForSolutionLoadedAsync ( token ) ;
481476
482477 ImplicitRestoreReason restoreReason = ImplicitRestoreReason . None ;
483- DateTime ? bulkRestoreCoordinationCheckStartTime = default ;
478+ DateTimeOffset ? bulkRestoreCoordinationCheckStartTime = default ;
484479 // Loops until there are pending restore requests or it's get cancelled
485480 while ( ! token . IsCancellationRequested )
486481 {
@@ -509,7 +504,7 @@ await _solutionLoadedEvent.WaitAsync()
509504 await PromoteTaskToActiveAsync ( restoreOperation , token ) ;
510505
511506 token . ThrowIfCancellationRequested ( ) ;
512- DateTime lastNominationReceived = DateTime . UtcNow ;
507+
513508 int requestCount = 1 ;
514509 int projectsReadyCheckCount = 0 ;
515510 int projectRestoreInfoSourcesCount = - 1 ;
@@ -532,7 +527,7 @@ await _solutionLoadedEvent.WaitAsync()
532527 var projectReadyCheckMeasurement = Stopwatch . StartNew ( ) ;
533528 if ( bulkRestoreCoordinationCheckStartTime == default )
534529 {
535- bulkRestoreCoordinationCheckStartTime = DateTime . UtcNow ;
530+ bulkRestoreCoordinationCheckStartTime = DateTimeOffset . UtcNow ;
536531 }
537532 projectsReadyCheckCount ++ ;
538533 // If we are about to start restore, we should run through all the projects to ensure there isn't a pending nomination.
@@ -546,7 +541,7 @@ await _solutionLoadedEvent.WaitAsync()
546541 if ( restoreInfoSource . HasPendingNomination )
547542 {
548543 allProjectsReady = false ;
549- TimeSpan timeoutTime = CalculateTimeoutTime ( bulkRestoreCoordinationCheckStartTime . Value , DateTime . UtcNow , BulkRestoreCoordinationTimeout ) ;
544+ TimeSpan timeoutTime = CalculateTimeoutTime ( bulkRestoreCoordinationCheckStartTime . Value , DateTimeOffset . UtcNow , BulkRestoreCoordinationTimeout ) ;
550545 var timeoutTask = Task . Delay ( timeoutTime , token ) ;
551546 var whenNominatedTask = restoreInfoSource . WhenNominated ( token ) ;
552547
@@ -555,6 +550,10 @@ await _solutionLoadedEvent.WaitAsync()
555550 {
556551 bulkCheckTimeout = true ;
557552 }
553+ else
554+ {
555+ await whenNominatedTask ;
556+ }
558557 }
559558 }
560559
@@ -584,7 +583,6 @@ await _solutionLoadedEvent.WaitAsync()
584583 else
585584 {
586585 requestCount ++ ;
587- lastNominationReceived = DateTime . UtcNow ;
588586 // Upgrade request if necessary
589587 if ( next != null && next . RestoreSource != request . RestoreSource )
590588 {
@@ -641,14 +639,71 @@ await _solutionLoadedEvent.WaitAsync()
641639 return status ;
642640 }
643641
642+ /// <summary>
643+ /// Waits for the solution to be loaded and all projects to be nominated.
644+ /// The current implementation assumes this is *only* called when the solution is first loaded and the first restore is being requested.
645+ /// This is because we only want to wait for all projects to be nominated on the first restore, otherwise we will just do a restore immediately, honoring the user action.
646+ /// </summary>
647+ internal static async Task < RestoreReadinessResult > WaitForOnBuildRestoreReadinessAsync (
648+ Func < CancellationToken , Task > waitForSolutionLoadedAsync ,
649+ Func < Task < bool > > isAllProjectsNominatedAsync ,
650+ Func < DateTimeOffset > getUtcNow ,
651+ CancellationToken token )
652+ {
653+ RestoreReadinessResult restoreReadiness = new ( ) ;
654+
655+ await waitForSolutionLoadedAsync ( token ) ;
656+
657+ restoreReadiness . BulkRestoreCoordinationCheckStartTime = getUtcNow ( ) ;
658+ while ( ! token . IsCancellationRequested )
659+ {
660+ if ( await isAllProjectsNominatedAsync ( ) )
661+ {
662+ restoreReadiness . RestoreReason = ImplicitRestoreReason . AllProjectsNominated ;
663+ break ;
664+ }
665+
666+ TimeSpan timeoutTime = CalculateTimeoutTime ( restoreReadiness . BulkRestoreCoordinationCheckStartTime . Value , getUtcNow ( ) , BulkRestoreCoordinationTimeout ) ;
667+ if ( timeoutTime == TimeSpan . Zero )
668+ {
669+ restoreReadiness . RestoreReason = ImplicitRestoreReason . NominationsIdleTimeout ;
670+ break ;
671+ }
672+
673+ await Task . Delay ( IdleTimeoutMs , token ) ;
674+ }
675+
676+ token . ThrowIfCancellationRequested ( ) ;
677+
678+ return restoreReadiness ;
679+ }
680+
681+ private async Task WaitForSolutionLoadedAsync ( CancellationToken token )
682+ {
683+ while ( ! _solutionLoadedEvent . IsSet )
684+ {
685+ // Needed when OnAfterBackgroundSolutionLoadComplete fires before
686+ // Advise has been called.
687+ if ( await IsSolutionFullyLoadedAsync ( ) )
688+ {
689+ _solutionLoadedEvent . Set ( ) ;
690+ break ;
691+ }
692+
693+ // Waits for the solution to fully load or canceled
694+ await _solutionLoadedEvent . WaitAsync ( token )
695+ . WithTimeout ( TimeSpan . FromMilliseconds ( DelaySolutionLoadRetry ) ) ;
696+ }
697+ }
698+
644699 /// <summary>
645700 /// Calculates the timeout time.
646701 /// </summary>
647702 /// <param name="startTime">The start time from which to calculate</param>
648703 /// <param name="currentTime">The current time</param>
649704 /// <param name="timeoutTime">The timeout time</param>
650705 /// <returns>The leftover timeout time, or 0.</returns>
651- internal static TimeSpan CalculateTimeoutTime ( DateTime startTime , DateTime currentTime , TimeSpan timeoutTime )
706+ internal static TimeSpan CalculateTimeoutTime ( DateTimeOffset startTime , DateTimeOffset currentTime , TimeSpan timeoutTime )
652707 {
653708 TimeSpan leftoverTime = ( startTime - currentTime ) + timeoutTime ;
654709 if ( leftoverTime . Ticks > 0 )
@@ -658,11 +713,11 @@ internal static TimeSpan CalculateTimeoutTime(DateTime startTime, DateTime curre
658713 return new TimeSpan ( ticks : 0 ) ;
659714 }
660715
661- private static Dictionary < string , object > GetRestoreTrackingData ( ImplicitRestoreReason restoreReason , int requestCount , int projectRestoreInfoSourcesCount , DateTime ? bulkRestoreCoordinationCheckStartTime , int projectsReadyCheckCount , List < TimeSpan > projectReadyTimings , ExplicitRestoreReason explicitRestoreReason )
716+ private static Dictionary < string , object > GetRestoreTrackingData ( ImplicitRestoreReason restoreReason , int requestCount , int projectRestoreInfoSourcesCount , DateTimeOffset ? bulkRestoreCoordinationCheckStartTime , int projectsReadyCheckCount , List < TimeSpan > projectReadyTimings , ExplicitRestoreReason explicitRestoreReason )
662717 {
663718 double bulkRestoreCoordinationTotalTime = bulkRestoreCoordinationCheckStartTime == default ?
664719 0.0 :
665- ( DateTime . UtcNow - bulkRestoreCoordinationCheckStartTime . Value ) . TotalSeconds ;
720+ ( DateTimeOffset . UtcNow - bulkRestoreCoordinationCheckStartTime . Value ) . TotalSeconds ;
666721
667722 return new ( )
668723 {
@@ -787,6 +842,22 @@ public override int OnAfterBackgroundSolutionLoadComplete()
787842 return VSConstants . S_OK ;
788843 }
789844
845+ internal sealed class RestoreReadinessResult
846+ {
847+ public RestoreReadinessResult ( )
848+ {
849+ RestoreReason = ImplicitRestoreReason . None ;
850+ ProjectRestoreInfoSourcesCount = - 1 ;
851+ ProjectReadyTimings = [ ] ;
852+ }
853+
854+ public ImplicitRestoreReason RestoreReason { get ; set ; }
855+ public DateTimeOffset ? BulkRestoreCoordinationCheckStartTime { get ; set ; }
856+ public int ProjectsReadyCheckCount { get ; set ; }
857+ public int ProjectRestoreInfoSourcesCount { get ; set ; }
858+ public List < TimeSpan > ProjectReadyTimings { get ; }
859+ }
860+
790861 private class BackgroundRestoreOperation
791862 : IEquatable < BackgroundRestoreOperation > , IDisposable
792863 {
0 commit comments