Skip to content

Commit 43aaec6

Browse files
committed
C#: Push the feed fetching logic into DownloadPackages.
1 parent 6ee50ce commit 43aaec6

1 file changed

Lines changed: 22 additions & 30 deletions

File tree

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public HashSet<AssemblyLookupLocation> Restore()
133133
{
134134
// If we experience a timeout, we use this fallback.
135135
// 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.
136-
var unresponsiveMissingPackageLocation = DownloadMissingPackagesAndUseFallback([]);
136+
var unresponsiveMissingPackageLocation = DownloadMissingPackages([]);
137137
return unresponsiveMissingPackageLocation is null
138138
? []
139139
: [unresponsiveMissingPackageLocation];
@@ -195,9 +195,7 @@ public HashSet<AssemblyLookupLocation> Restore()
195195

196196
var usedPackageNames = GetAllUsedPackageDirNames(dependencies);
197197

198-
var missingPackageLocation = feedManager.CheckNugetFeedResponsiveness
199-
? DownloadMissingPackagesAndUseFallback(usedPackageNames)
200-
: DownloadMissingPackages(usedPackageNames);
198+
var missingPackageLocation = DownloadMissingPackages(usedPackageNames);
201199

202200
if (missingPackageLocation is not null)
203201
{
@@ -303,22 +301,29 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
303301
compilationInfoContainer.CompilationInfos.Add(("Failed project restore with missing package error", nugetMissingPackageFailures.ToString()));
304302
}
305303

306-
private AssemblyLookupLocation? DownloadMissingPackagesAndUseFallback(IEnumerable<string> usedPackageNames)
304+
private AssemblyLookupLocation? DownloadMissingPackages(IEnumerable<string> usedPackageNames)
307305
{
308-
var reachableFallbackFeeds = feedManager.GetReachableFallbackNugetFeeds();
309-
compilationInfoContainer.CompilationInfos.Add(("Reachable fallback NuGet feed count", reachableFallbackFeeds.Count.ToString()));
306+
IEnumerable<string> feeds = [];
307+
if (feedManager.CheckNugetFeedResponsiveness || feedManager.HasPrivateRegistryFeeds)
308+
{
309+
// Attempt to get the fallback configuration.
310+
var reachableFallbackFeeds = feedManager.GetReachableFallbackNugetFeeds();
311+
compilationInfoContainer.CompilationInfos.Add(("Reachable fallback NuGet feed count", reachableFallbackFeeds.Count.ToString()));
310312

311-
if (reachableFallbackFeeds.Count > 0)
313+
if (reachableFallbackFeeds.Count == 0)
314+
{
315+
logger.LogWarning("Skipping download of missing packages from specific feeds as no fallback NuGet feeds are reachable.");
316+
return null;
317+
}
318+
feeds = reachableFallbackFeeds;
319+
}
320+
else if (GetNugetConfig() is string config)
312321
{
313-
return DownloadMissingPackages(usedPackageNames, fallbackNugetFeeds: reachableFallbackFeeds);
322+
feeds = feedManager.FeedsToUseFromConfig(config);
314323
}
315324

316-
logger.LogWarning("Skipping download of missing packages from specific feeds as no fallback NuGet feeds are reachable.");
317-
return null;
318-
}
325+
var nugetSources = feedManager.FeedsToDotnetRestoreArgument(feeds);
319326

320-
private AssemblyLookupLocation? DownloadMissingPackages(IEnumerable<string> usedPackageNames, IEnumerable<string>? fallbackNugetFeeds = null)
321-
{
322327
var alreadyDownloadedPackages = usedPackageNames.Select(p => p.ToLowerInvariant());
323328
var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames();
324329

@@ -351,26 +356,14 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
351356

352357
logger.LogInfo($"Found {notYetDownloadedPackages.Count} packages that are not yet restored");
353358

354-
IEnumerable<string> feeds = [];
355-
if (fallbackNugetFeeds is not null)
356-
{
357-
feeds = fallbackNugetFeeds;
358-
}
359-
else if (GetNugetConfig() is string config)
360-
{
361-
feeds = feedManager.FeedsToUseFromConfig(config);
362-
}
363-
364-
var nugetSources = feedManager.FeedsToDotnetRestoreArgument(feeds);
365-
366359
compilationInfoContainer.CompilationInfos.Add(("Fallback nuget restore", notYetDownloadedPackages.Count.ToString()));
367360

368361
var successCount = 0;
369362
var sync = new Lock();
370363

371364
Parallel.ForEach(notYetDownloadedPackages, new ParallelOptions { MaxDegreeOfParallelism = DependencyManager.Threads }, package =>
372365
{
373-
var success = TryRestorePackageManually(package.Name, nugetSources, package.PackageReferenceSource, tryWithoutNugetConfig: fallbackNugetFeeds is null);
366+
var success = TryRestorePackageManually(package.Name, nugetSources, package.PackageReferenceSource);
374367
if (!success)
375368
{
376369
return;
@@ -476,8 +469,7 @@ private static IEnumerable<string> GetRestoredPackageDirectoryNames(DirectoryInf
476469
.Select(d => Path.GetFileName(d).ToLowerInvariant());
477470
}
478471

479-
private bool TryRestorePackageManually(string package, string? nugetSources = null, PackageReferenceSource packageReferenceSource = PackageReferenceSource.SdkCsProj,
480-
bool tryWithoutNugetConfig = true, bool tryPrereleaseVersion = true)
472+
private bool TryRestorePackageManually(string package, string? nugetSources = null, PackageReferenceSource packageReferenceSource = PackageReferenceSource.SdkCsProj, bool tryPrereleaseVersion = true)
481473
{
482474
logger.LogInfo($"Restoring package {package}...");
483475
using var tempDir = new TemporaryDirectory(
@@ -505,7 +497,7 @@ private bool TryRestorePackageManually(string package, string? nugetSources = nu
505497
return true;
506498
}
507499

508-
if (tryWithoutNugetConfig && res.HasNugetPackageSourceError && nugetSources is not null && !feedManager.CheckNugetFeedResponsiveness)
500+
if (!feedManager.CheckNugetFeedResponsiveness && res.HasNugetPackageSourceError && nugetSources is not null)
509501
{
510502
logger.LogDebug($"Trying to restore '{package}' without nuget.config.");
511503
// Restore could not be completed because the listed source is unavailable. Try without the nuget.config:

0 commit comments

Comments
 (0)