Skip to content

Commit 1339778

Browse files
authored
Merge pull request #22774 from michaelnebel/csharp/improvefeedreachabilitylogging
C#: Improve feed reachability logging.
2 parents 2222138 + f726a89 commit 1339778

2 files changed

Lines changed: 23 additions & 27 deletions

File tree

‎csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs‎

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,15 @@ public FeedManager(ILogger logger, IDotNet dotnet, IRegistryProxy? registryProxy
107107

108108
lazyExplicitFeeds = new Lazy<ImmutableHashSet<string>>(GetExplicitFeeds);
109109
lazyAllFeeds = new Lazy<ImmutableHashSet<string>>(GetAllFeeds);
110-
lazyReachableExplicitFeeds = new Lazy<ImmutableHashSet<string>>(() =>
111-
{
112-
logger.LogInfo("Discovering reachable explicit NuGet feeds.");
113-
return CheckSpecifiedFeeds(ExplicitFeeds);
114-
});
110+
lazyReachableExplicitFeeds = new Lazy<ImmutableHashSet<string>>(() => CheckSpecifiedFeeds(ExplicitFeeds, "explicit"));
115111
lazyReachableFeeds = new Lazy<ImmutableHashSet<string>>(() =>
116112
{
117-
logger.LogInfo("Discovering reachable inherited NuGet feeds.");
118113
// Inherited feeds should only be used, if they are indeed reachable (as they may be environment specific).
119-
var reachableInheritedFeeds = CheckSpecifiedFeeds(InheritedFeeds);
114+
var reachableInheritedFeeds = CheckSpecifiedFeeds(InheritedFeeds, "inherited");
120115
return ReachableExplicitFeeds.Union(reachableInheritedFeeds).ToImmutableHashSet();
121116
});
122-
lazyReachableFallbackFeeds = new Lazy<ImmutableHashSet<string>>(() =>
123-
{
124-
logger.LogInfo("Discovering reachable fallback NuGet feeds.");
125-
var reachableFallbackFeeds = GetReachableFallbackNugetFeeds();
126-
return reachableFallbackFeeds.ToImmutableHashSet();
127-
});
128-
lazyReachableDefaultFeeds = new Lazy<ImmutableHashSet<string>>(() =>
129-
{
130-
logger.LogInfo("Discovering reachable default NuGet feeds.");
131-
return CheckSpecifiedFeeds(DefaultFeeds);
132-
});
117+
lazyReachableFallbackFeeds = new Lazy<ImmutableHashSet<string>>(GetReachableFallbackNugetFeeds);
118+
lazyReachableDefaultFeeds = new Lazy<ImmutableHashSet<string>>(() => CheckSpecifiedFeeds(DefaultFeeds, "default"));
133119
}
134120

135121
public FeedManager(ILogger logger, IDotNet dotnet, IRegistryProxy? registryProxy, IFileProvider fileProvider)
@@ -300,8 +286,15 @@ private HashSet<string> GetExcludedFeeds()
300286
/// </summary>
301287
/// <param name="feeds">The set of package feeds to check.</param>
302288
/// <returns>The list of feeds that were reachable.</returns>
303-
private ImmutableHashSet<string> CheckSpecifiedFeeds(ImmutableHashSet<string> feeds)
289+
private ImmutableHashSet<string> CheckSpecifiedFeeds(ImmutableHashSet<string> feeds, string kind)
304290
{
291+
if (feeds.Count == 0)
292+
{
293+
return [];
294+
}
295+
296+
logger.LogInfo($"Discovering reachable {kind} NuGet feeds.");
297+
305298
// Exclude any feeds from the feed check that are configured by the corresponding environment variable.
306299
// These feeds are always assumed to be reachable.
307300
var excludedFeeds = GetExcludedFeeds();
@@ -316,7 +309,7 @@ private ImmutableHashSet<string> CheckSpecifiedFeeds(ImmutableHashSet<string> fe
316309
return true;
317310
}).ToHashSet();
318311

319-
var reachable = GetReachableNuGetFeeds(feedsToCheck, isFallback: false);
312+
var reachable = feedsToCheck.Count > 0 ? GetReachableNuGetFeeds(feedsToCheck, kind, isFallback: false) : [];
320313

321314
// Always consider feeds excluded for the reachability check as reachable.
322315
return reachable.Union(feeds.Where(feed => excludedFeeds.Contains(feed))).ToImmutableHashSet();
@@ -328,10 +321,9 @@ private ImmutableHashSet<string> CheckSpecifiedFeeds(ImmutableHashSet<string> fe
328321
/// <param name="feedsToCheck">The feeds to check.</param>
329322
/// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
330323
/// <returns>The list of feeds that could be reached.</returns>
331-
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback)
324+
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, string kind, bool isFallback)
332325
{
333-
var fallbackStr = isFallback ? "fallback " : "";
334-
logger.LogInfo($"Checking {fallbackStr}NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}");
326+
logger.LogInfo($"Checking {kind} NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}");
335327

336328
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback);
337329
var reachableFeeds = feedsToCheck
@@ -340,17 +332,17 @@ private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool i
340332

341333
if (reachableFeeds.Count == 0)
342334
{
343-
logger.LogWarning($"No {fallbackStr}NuGet feeds are reachable.");
335+
logger.LogWarning($"No {kind} NuGet feeds are reachable.");
344336
}
345337
else
346338
{
347-
logger.LogInfo($"Reachable {fallbackStr}NuGet feeds: {string.Join(", ", reachableFeeds.OrderBy(f => f))}");
339+
logger.LogInfo($"Reachable {kind} NuGet feeds: {string.Join(", ", reachableFeeds.OrderBy(f => f))}");
348340
}
349341

350342
return reachableFeeds;
351343
}
352344

353-
private List<string> GetReachableFallbackNugetFeeds()
345+
private ImmutableHashSet<string> GetReachableFallbackNugetFeeds()
354346
{
355347
var fallbackFeeds = EnvironmentVariables.GetURLs(EnvironmentVariableNames.FallbackNugetFeeds).ToHashSet();
356348
if (fallbackFeeds.Count == 0)
@@ -374,7 +366,7 @@ private List<string> GetReachableFallbackNugetFeeds()
374366
logger.LogInfo($"Using fallback NuGet feeds from environment variable '{EnvironmentVariableNames.FallbackNugetFeeds}'.");
375367
}
376368

377-
return GetReachableNuGetFeeds(fallbackFeeds, isFallback: true);
369+
return GetReachableNuGetFeeds(fallbackFeeds, "fallback", isFallback: true).ToImmutableHashSet();
378370
}
379371

380372
private ImmutableHashSet<string> GetExplicitFeeds()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Avoid logging warnings when there are no explicit or inherited feeds to check for reachability.

0 commit comments

Comments
 (0)