Skip to content

Commit 650e382

Browse files
committed
C#: For specific listed nuget feeds in a project, still allow their use unless there is a timeout when trying to reach them.
1 parent 2a70a9c commit 650e382

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public HashSet<AssemblyLookupLocation> Restore()
148148
// All explicit feeds can be considered reachable
149149
HashSet<string> reachableFeeds = [];
150150
reachableFeeds.UnionWith(explicitFeeds);
151-
reachableFeeds.UnionWith(GetReachableNuGetFeeds(inheritedFeeds, isFallback: false));
151+
// Inherited feeds should only be used, if they are indeed reachable (as they may be environment specific).
152+
reachableFeeds.UnionWith(GetReachableNuGetFeeds(inheritedFeeds, isFallback: false, allowNonTimeoutExceptions: false));
152153

153154
// If feed responsiveness is being checked, we only want to use the feeds that are reachable (note this set includes private
154155
// registry feeds if they are reachable).
@@ -231,15 +232,16 @@ public HashSet<AssemblyLookupLocation> Restore()
231232
/// </summary>
232233
/// <param name="feedsToCheck">The feeds to check.</param>
233234
/// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
235+
/// <param name="allowNonTimeoutExceptions">Whether to allow non-timeout exceptions.</param>
234236
/// <returns>The list of feeds that could be reached.</returns>
235-
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback)
237+
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback, bool allowNonTimeoutExceptions)
236238
{
237239
var fallbackStr = isFallback ? "fallback " : "";
238240
logger.LogInfo($"Checking {fallbackStr}NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}");
239241

240242
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback);
241243
var reachableFeeds = feedsToCheck
242-
.Where(feed => IsFeedReachable(feed, initialTimeout, tryCount, allowExceptions: false))
244+
.Where(feed => IsFeedReachable(feed, initialTimeout, tryCount, allowNonTimeoutExceptions))
243245
.ToList();
244246

245247
if (reachableFeeds.Count == 0)
@@ -274,7 +276,7 @@ private List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNu
274276
}
275277
}
276278

277-
var reachableFallbackFeeds = GetReachableNuGetFeeds(fallbackFeeds, isFallback: true);
279+
var reachableFallbackFeeds = GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, allowNonTimeoutExceptions: false);
278280

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

@@ -675,7 +677,7 @@ private static async Task ExecuteGetRequest(string address, HttpClient httpClien
675677
}
676678
}
677679

678-
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowExceptions = true)
680+
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowNonTimeoutExceptions)
679681
{
680682
logger.LogInfo($"Checking if NuGet feed '{feed}' is reachable...");
681683

@@ -730,9 +732,9 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
730732
}
731733

732734
// We're only interested in timeouts.
733-
var start = allowExceptions ? "Considering" : "Not considering";
735+
var start = allowNonTimeoutExceptions ? "Considering" : "Not considering";
734736
logger.LogInfo($"Querying NuGet feed '{feed}' failed in a timely manner. {start} the feed for use. The reason for the failure: {exc.Message}");
735-
return allowExceptions;
737+
return allowNonTimeoutExceptions;
736738
}
737739
}
738740

@@ -798,7 +800,7 @@ private bool CheckSpecifiedFeeds(HashSet<string> feeds)
798800
return true;
799801
}).ToHashSet();
800802

801-
var reachableFeeds = GetReachableNuGetFeeds(feedsToCheck, isFallback: false);
803+
var reachableFeeds = GetReachableNuGetFeeds(feedsToCheck, isFallback: false, allowNonTimeoutExceptions: true);
802804
var allFeedsReachable = reachableFeeds.Count == feedsToCheck.Count;
803805

804806
EmitUnreachableFeedsDiagnostics(allFeedsReachable);

0 commit comments

Comments
 (0)