Skip to content

Commit c4771f3

Browse files
committed
C#: Only include feeds that we can connect to.
1 parent 9359731 commit c4771f3

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,18 @@ public HashSet<AssemblyLookupLocation> Restore()
138138
compilationInfoContainer.CompilationInfos.Add(("Inherited NuGet feed count", inheritedFeeds.Count.ToString()));
139139
}
140140

141-
if (!CheckSpecifiedFeeds(explicitFeeds))
141+
if (!CheckSpecifiedFeeds(explicitFeeds, out var reachableFeeds))
142142
{
143+
// If we experience a timeout, we use this fallback.
143144
// 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.
144145
var unresponsiveMissingPackageLocation = DownloadMissingPackagesFromSpecificFeeds([], explicitFeeds);
145146
return unresponsiveMissingPackageLocation is null
146147
? []
147148
: [unresponsiveMissingPackageLocation];
148149
}
149150

150-
// All explicit feeds can be considered reachable
151-
HashSet<string> reachableFeeds = [];
152-
reachableFeeds.UnionWith(explicitFeeds);
153151
// Inherited feeds should only be used, if they are indeed reachable (as they may be environment specific).
154-
reachableFeeds.UnionWith(GetReachableNuGetFeeds(inheritedFeeds, isFallback: false, allowNonTimeoutExceptions: false));
152+
reachableFeeds.UnionWith(GetReachableNuGetFeeds(inheritedFeeds, isFallback: false, out var _));
155153

156154
// If feed responsiveness is being checked, we only want to use the feeds that are reachable (note this set includes private
157155
// registry feeds if they are reachable).
@@ -234,16 +232,22 @@ public HashSet<AssemblyLookupLocation> Restore()
234232
/// </summary>
235233
/// <param name="feedsToCheck">The feeds to check.</param>
236234
/// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
237-
/// <param name="allowNonTimeoutExceptions">Whether to allow non-timeout exceptions.</param>
235+
/// <param name="isTimeout">Whether a timeout occurred while checking the feeds.</param>
238236
/// <returns>The list of feeds that could be reached.</returns>
239-
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback, bool allowNonTimeoutExceptions)
237+
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback, out bool isTimeout)
240238
{
241239
var fallbackStr = isFallback ? "fallback " : "";
242240
logger.LogInfo($"Checking {fallbackStr}NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}");
243241

244242
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback);
243+
var timeout = false;
245244
var reachableFeeds = feedsToCheck
246-
.Where(feed => IsFeedReachable(feed, initialTimeout, tryCount, allowNonTimeoutExceptions))
245+
.Where(feed =>
246+
{
247+
var reachable = IsFeedReachable(feed, initialTimeout, tryCount, out var feedTimeout);
248+
timeout |= feedTimeout;
249+
return reachable;
250+
})
247251
.ToList();
248252

249253
if (reachableFeeds.Count == 0)
@@ -255,6 +259,7 @@ private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool i
255259
logger.LogInfo($"Reachable {fallbackStr}NuGet feeds: {string.Join(", ", reachableFeeds.OrderBy(f => f))}");
256260
}
257261

262+
isTimeout = timeout;
258263
return reachableFeeds;
259264
}
260265

@@ -263,7 +268,7 @@ private bool IsDefaultFeedReachable()
263268
if (CheckNugetFeedResponsiveness)
264269
{
265270
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback: false);
266-
return IsFeedReachable(PublicNugetOrgFeed, initialTimeout, tryCount, allowNonTimeoutExceptions: false);
271+
return IsFeedReachable(PublicNugetOrgFeed, initialTimeout, tryCount, out var _);
267272
}
268273

269274
return true;
@@ -289,7 +294,7 @@ private List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNu
289294
}
290295
}
291296

292-
var reachableFallbackFeeds = GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, allowNonTimeoutExceptions: false);
297+
var reachableFallbackFeeds = GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, out var _);
293298

294299
compilationInfoContainer.CompilationInfos.Add(("Reachable fallback NuGet feed count", reachableFallbackFeeds.Count.ToString()));
295300

@@ -690,7 +695,7 @@ private static async Task<HttpResponseMessage> ExecuteGetRequest(string address,
690695
return await httpClient.GetAsync(address, cancellationToken);
691696
}
692697

693-
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowNonTimeoutExceptions)
698+
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, out bool isTimeout)
694699
{
695700
logger.LogInfo($"Checking if NuGet feed '{feed}' is reachable...");
696701

@@ -723,6 +728,8 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
723728

724729
using HttpClient client = new(httpClientHandler);
725730

731+
isTimeout = false;
732+
726733
for (var i = 0; i < tryCount; i++)
727734
{
728735
using var cts = new CancellationTokenSource();
@@ -746,16 +753,13 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
746753
continue;
747754
}
748755

749-
// Adjust the message based on whether non-timeout exceptions are allowed.
750-
var useMessage = allowNonTimeoutExceptions
751-
? "Considering the feed for use despite of the failure as it wasn't a timeout."
752-
: "Not considering the feed for use.";
753-
logger.LogInfo($"Querying NuGet feed '{feed}' failed. {useMessage} The reason for the failure: {exc.Message}");
754-
return allowNonTimeoutExceptions;
756+
logger.LogInfo($"Querying NuGet feed '{feed}' failed. The reason for the failure: {exc.Message}");
757+
return false;
755758
}
756759
}
757760

758761
logger.LogWarning($"Didn't receive answer from NuGet feed '{feed}'. Tried it {tryCount} times.");
762+
isTimeout = true;
759763
return false;
760764
}
761765

@@ -798,10 +802,12 @@ private HashSet<string> GetExcludedFeeds()
798802
/// Checks that we can connect to the specified NuGet feeds.
799803
/// </summary>
800804
/// <param name="feeds">The set of package feeds to check.</param>
805+
/// <param name="reachableFeeds">The list of feeds that were reachable.</param>
801806
/// <returns>
802-
/// True if all feeds are reachable (excluding any feeds that are configured to be excluded from the check) or false otherwise.
807+
/// True if there is no timeout when trying to reach the feeds (excluding any feeds that are configured
808+
/// to be excluded from the check) or false otherwise.
803809
/// </returns>
804-
private bool CheckSpecifiedFeeds(HashSet<string> feeds)
810+
private bool CheckSpecifiedFeeds(HashSet<string> feeds, out HashSet<string> reachableFeeds)
805811
{
806812
// Exclude any feeds from the feed check that are configured by the corresponding environment variable.
807813
// These feeds are always assumed to be reachable.
@@ -817,12 +823,11 @@ private bool CheckSpecifiedFeeds(HashSet<string> feeds)
817823
return true;
818824
}).ToHashSet();
819825

820-
var reachableFeeds = GetReachableNuGetFeeds(feedsToCheck, isFallback: false, allowNonTimeoutExceptions: true);
821-
var allFeedsReachable = reachableFeeds.Count == feedsToCheck.Count;
822-
823-
EmitUnreachableFeedsDiagnostics(allFeedsReachable);
826+
reachableFeeds = GetReachableNuGetFeeds(feedsToCheck, isFallback: false, out var isTimeout).ToHashSet();
824827

825-
return allFeedsReachable;
828+
var noTimeout = !isTimeout;
829+
EmitUnreachableFeedsDiagnostics(noTimeout);
830+
return noTimeout;
826831
}
827832

828833
/// <summary>

0 commit comments

Comments
 (0)