@@ -20,7 +20,6 @@ internal sealed partial class NugetPackageRestorer : IDisposable
2020 {
2121 internal const string PublicNugetOrgFeed = "https://api.nuget.org/v3/index.json" ;
2222
23- private readonly bool checkNugetFeedResponsiveness = EnvironmentVariables . GetBooleanOptOut ( EnvironmentVariableNames . CheckNugetFeedResponsiveness ) ;
2423 private readonly FileProvider fileProvider ;
2524 private readonly FileContent fileContent ;
2625 private readonly IDotNet dotnet ;
@@ -30,6 +29,8 @@ internal sealed partial class NugetPackageRestorer : IDisposable
3029 private readonly DependencyDirectory missingPackageDirectory ;
3130 private readonly ILogger logger ;
3231 private readonly ICompilationInfoContainer compilationInfoContainer ;
32+ private readonly Lazy < bool > lazyCheckNugetFeedResponsiveness = new ( ( ) => EnvironmentVariables . GetBooleanOptOut ( EnvironmentVariableNames . CheckNugetFeedResponsiveness ) ) ;
33+ private bool CheckNugetFeedResponsiveness => lazyCheckNugetFeedResponsiveness . Value ;
3334 private HashSet < string > PrivateRegistryFeeds => dependabotProxy ? . RegistryURLs ?? [ ] ;
3435 private bool HasPrivateRegistryFeeds => PrivateRegistryFeeds . Any ( ) ;
3536
@@ -113,23 +114,22 @@ public DirectoryInfo[] GetOrderedPackageVersionSubDirectories(string packagePath
113114 public HashSet < AssemblyLookupLocation > Restore ( )
114115 {
115116 var assemblyLookupLocations = new HashSet < AssemblyLookupLocation > ( ) ;
116- logger . LogInfo ( $ "Checking NuGet feed responsiveness: { checkNugetFeedResponsiveness } ") ;
117- compilationInfoContainer . CompilationInfos . Add ( ( "NuGet feed responsiveness checked" , checkNugetFeedResponsiveness ? "1" : "0" ) ) ;
117+ logger . LogInfo ( $ "Checking NuGet feed responsiveness: { CheckNugetFeedResponsiveness } ") ;
118+ compilationInfoContainer . CompilationInfos . Add ( ( "NuGet feed responsiveness checked" , CheckNugetFeedResponsiveness ? "1" : "0" ) ) ;
118119
119120 HashSet < string > explicitFeeds = [ ] ;
120121 string ? explicitRestoreSources = null ;
121122
122123 try
123124 {
124- HashSet < string > reachableFeeds = [ ] ;
125125 HashSet < string > allFeeds = [ ] ;
126126
127127 // Find feeds that are configured in NuGet.config files and divide them into ones that
128128 // are explicitly configured for the project or by a private registry, and "all feeds"
129129 // (including inherited ones) from other locations on the host outside of the working directory.
130130 ( explicitFeeds , allFeeds ) = GetAllFeeds ( ) ;
131131
132- if ( checkNugetFeedResponsiveness )
132+ if ( CheckNugetFeedResponsiveness )
133133 {
134134 var inheritedFeeds = allFeeds . Except ( explicitFeeds ) . ToHashSet ( ) ;
135135
@@ -138,11 +138,7 @@ public HashSet<AssemblyLookupLocation> Restore()
138138 compilationInfoContainer . CompilationInfos . Add ( ( "Inherited NuGet feed count" , inheritedFeeds . Count . ToString ( ) ) ) ;
139139 }
140140
141- var explicitFeedsReachable = CheckSpecifiedFeeds ( explicitFeeds , out var reachableExplicitFeeds ) ;
142- reachableFeeds . UnionWith ( reachableExplicitFeeds ) ;
143- reachableFeeds . UnionWith ( GetReachableNuGetFeeds ( inheritedFeeds , isFallback : false ) ) ;
144-
145- if ( ! explicitFeedsReachable )
141+ if ( ! CheckSpecifiedFeeds ( explicitFeeds , out var reachableFeeds ) )
146142 {
147143 // todo: we could also check the reachability of the inherited nuget feeds, but to use those in the fallback we would need to handle authentication too.
148144 var unresponsiveMissingPackageLocation = DownloadMissingPackagesFromSpecificFeeds ( [ ] , explicitFeeds ) ;
@@ -151,6 +147,8 @@ public HashSet<AssemblyLookupLocation> Restore()
151147 : [ unresponsiveMissingPackageLocation ] ;
152148 }
153149
150+ reachableFeeds . UnionWith ( GetReachableNuGetFeeds ( inheritedFeeds , isFallback : false ) ) ;
151+
154152 // If feed responsiveness is being checked, we only want to use the feeds that are reachable (note this set includes private
155153 // registry feeds if they are reachable).
156154 explicitRestoreSources = MakeRestoreSourcesArgument ( reachableFeeds ) ;
@@ -216,7 +214,7 @@ public HashSet<AssemblyLookupLocation> Restore()
216214
217215 var usedPackageNames = GetAllUsedPackageDirNames ( dependencies ) ;
218216
219- var missingPackageLocation = checkNugetFeedResponsiveness
217+ var missingPackageLocation = CheckNugetFeedResponsiveness
220218 ? DownloadMissingPackagesFromSpecificFeeds ( usedPackageNames , explicitFeeds )
221219 : DownloadMissingPackages ( usedPackageNames ) ;
222220
@@ -233,15 +231,15 @@ public HashSet<AssemblyLookupLocation> Restore()
233231 /// <param name="feedsToCheck">The feeds to check.</param>
234232 /// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
235233 /// <returns>The list of feeds that could be reached.</returns>
236- private List < string > GetReachableNuGetFeeds ( HashSet < string > feedsToCheck , bool isFallback )
234+ private HashSet < string > GetReachableNuGetFeeds ( HashSet < string > feedsToCheck , bool isFallback )
237235 {
238236 var fallbackStr = isFallback ? "fallback " : "" ;
239237 logger . LogInfo ( $ "Checking { fallbackStr } NuGet feed reachability on feeds: { string . Join ( ", " , feedsToCheck . OrderBy ( f => f ) ) } ") ;
240238
241239 var ( initialTimeout , tryCount ) = GetFeedRequestSettings ( isFallback ) ;
242240 var reachableFeeds = feedsToCheck
243241 . Where ( feed => IsFeedReachable ( feed , initialTimeout , tryCount , allowExceptions : false ) )
244- . ToList ( ) ;
242+ . ToHashSet ( ) ;
245243
246244 if ( reachableFeeds . Count == 0 )
247245 {
@@ -255,7 +253,7 @@ private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool i
255253 return reachableFeeds ;
256254 }
257255
258- private List < string > GetReachableFallbackNugetFeeds ( HashSet < string > ? feedsFromNugetConfigs )
256+ private HashSet < string > GetReachableFallbackNugetFeeds ( HashSet < string > ? feedsFromNugetConfigs )
259257 {
260258 var fallbackFeeds = EnvironmentVariables . GetURLs ( EnvironmentVariableNames . FallbackNugetFeeds ) . ToHashSet ( ) ;
261259 if ( fallbackFeeds . Count == 0 )
@@ -778,19 +776,36 @@ private HashSet<string> GetExcludedFeeds()
778776 /// <param name="feeds">The set of package feeds to check.</param>
779777 /// <returns>
780778 /// True if all feeds are reachable or false otherwise.
781- /// Also returns the list of reachable feeds as an out parameter.
779+ /// Also returns the set of reachable feeds as an out parameter.
782780 /// </returns>
783- private bool CheckSpecifiedFeeds ( HashSet < string > feeds , out List < string > reachableFeeds )
781+ private bool CheckSpecifiedFeeds ( HashSet < string > feeds , out HashSet < string > reachableFeeds )
784782 {
785- // Exclude any feeds that are configured by the corresponding environment variable.
783+ // Exclude any feeds from the feed check that are configured by the corresponding environment variable.
784+ // These feeds are always assumed to be reachable.
786785 var excludedFeeds = GetExcludedFeeds ( ) ;
787786
788- var feedsToCheck = feeds . Where ( feed => ! excludedFeeds . Contains ( feed ) ) . ToHashSet ( ) ;
787+ HashSet < string > feedsToCheck = [ ] ;
788+ HashSet < string > feedsNotToCheck = [ ] ;
789+ foreach ( var feed in feeds )
790+ {
791+ if ( excludedFeeds . Contains ( feed ) )
792+ {
793+ logger . LogInfo ( $ "Not checking reachability of NuGet feed '{ feed } ' as it is in the list of excluded feeds.") ;
794+ feedsNotToCheck . Add ( feed ) ;
795+ }
796+ else
797+ {
798+ feedsToCheck . Add ( feed ) ;
799+ }
800+ }
801+
789802 reachableFeeds = GetReachableNuGetFeeds ( feedsToCheck , isFallback : false ) ;
790803 var allFeedsReachable = reachableFeeds . Count == feedsToCheck . Count ;
791804
792805 EmitUnreachableFeedsDiagnostics ( allFeedsReachable ) ;
793806
807+ reachableFeeds . UnionWith ( feedsNotToCheck ) ;
808+
794809 return allFeedsReachable ;
795810 }
796811
0 commit comments