Skip to content

Commit 0ab113b

Browse files
committed
C#: Re-factor GetAllFeeds into properties in the Feed Manager and encapsulate as immutable hash sets.
1 parent cb288b6 commit 0ab113b

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ internal sealed partial class FeedManager : IDisposable
2929
public bool HasPrivateRegistryFeeds { get; }
3030
public bool CheckNugetFeedResponsiveness { get; } = EnvironmentVariables.GetBooleanOptOut(EnvironmentVariableNames.CheckNugetFeedResponsiveness);
3131

32+
private readonly Lazy<ImmutableHashSet<string>> lazyExplicitFeeds;
33+
public ImmutableHashSet<string> ExplicitFeeds => lazyExplicitFeeds.Value;
34+
35+
private readonly Lazy<ImmutableHashSet<string>> lazyAllFeeds;
36+
public ImmutableHashSet<string> AllFeeds => lazyAllFeeds.Value;
37+
3238
public FeedManager(ILogger logger, IDotNet dotnet, DependabotProxy? dependabotProxy, FileProvider fileProvider)
3339
{
3440
this.logger = logger;
@@ -38,6 +44,9 @@ public FeedManager(ILogger logger, IDotNet dotnet, DependabotProxy? dependabotPr
3844
PrivateRegistryFeeds = dependabotProxy?.RegistryURLs.ToImmutableHashSet() ?? [];
3945
HasPrivateRegistryFeeds = PrivateRegistryFeeds.Count > 0;
4046
emptyPackageDirectory = new DependencyDirectory("empty", "empty package", logger);
47+
48+
lazyExplicitFeeds = new Lazy<ImmutableHashSet<string>>(GetExplicitFeeds);
49+
lazyAllFeeds = new Lazy<ImmutableHashSet<string>>(GetAllFeeds);
4150
}
4251

4352
private string? GetDirectoryName(string path)
@@ -271,7 +280,7 @@ private HashSet<string> GetExcludedFeeds()
271280
/// True if there is a timeout when trying to reach the feeds (excluding any feeds that are configured
272281
/// to be excluded from the check) or false otherwise.
273282
/// </returns>
274-
public bool CheckSpecifiedFeeds(HashSet<string> feeds, out HashSet<string> reachableFeeds)
283+
public bool CheckSpecifiedFeeds(ImmutableHashSet<string> feeds, out HashSet<string> reachableFeeds)
275284
{
276285
// Exclude any feeds from the feed check that are configured by the corresponding environment variable.
277286
// These feeds are always assumed to be reachable.
@@ -342,7 +351,7 @@ private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool i
342351
return reachableFeeds;
343352
}
344353

345-
public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNugetConfigs)
354+
public List<string> GetReachableFallbackNugetFeeds(ImmutableHashSet<string>? feedsFromNugetConfigs)
346355
{
347356
var fallbackFeeds = EnvironmentVariables.GetURLs(EnvironmentVariableNames.FallbackNugetFeeds).ToHashSet();
348357
if (fallbackFeeds.Count == 0)
@@ -365,7 +374,7 @@ public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNug
365374
return GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, out var _);
366375
}
367376

368-
public (HashSet<string> explicitFeeds, HashSet<string> allFeeds) GetAllFeeds()
377+
private ImmutableHashSet<string> GetExplicitFeeds()
369378
{
370379
var nugetConfigs = fileProvider.NugetConfigs;
371380

@@ -391,10 +400,17 @@ public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNug
391400
explicitFeeds.UnionWith(PrivateRegistryFeeds);
392401
}
393402

403+
return explicitFeeds.ToImmutableHashSet();
404+
}
405+
406+
private ImmutableHashSet<string> GetAllFeeds()
407+
{
408+
var nugetConfigs = fileProvider.NugetConfigs;
409+
394410
HashSet<string> allFeeds = [];
395411

396412
// Add all explicitFeeds to the set of all feeds.
397-
allFeeds.UnionWith(explicitFeeds);
413+
allFeeds.UnionWith(ExplicitFeeds);
398414

399415
// Obtain the list of feeds from the root source directory.
400416
// If a NuGet file is present it will be respected, otherwise we will just get the machine/environment specific feeds.
@@ -414,7 +430,7 @@ public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNug
414430

415431
logger.LogInfo($"Found {allFeeds.Count} NuGet feeds (with inherited ones) in nuget.config files: {string.Join(", ", allFeeds.OrderBy(f => f))}");
416432

417-
return (explicitFeeds, allFeeds);
433+
return allFeeds.ToImmutableHashSet();
418434
}
419435

420436
[GeneratedRegex(@"^E\s(.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ public HashSet<AssemblyLookupLocation> Restore()
117117
// Find feeds that are configured in NuGet.config files and divide them into ones that
118118
// are explicitly configured for the project or by a private registry, and "all feeds"
119119
// (including inherited ones) from other locations on the host outside of the working directory.
120-
(var explicitFeeds, var allFeeds) = feedManager.GetAllFeeds();
120+
var explicitFeeds = feedManager.ExplicitFeeds;
121+
var allFeeds = feedManager.AllFeeds;
121122

122123
if (feedManager.CheckNugetFeedResponsiveness)
123124
{
124-
var inheritedFeeds = allFeeds.Except(explicitFeeds).ToHashSet();
125+
var inheritedFeeds = allFeeds.Except(explicitFeeds).ToImmutableHashSet();
125126

126127
if (inheritedFeeds.Count > 0)
127128
{
@@ -312,7 +313,7 @@ private void RestoreProjects(IEnumerable<string> projects, HashSet<string> reach
312313
compilationInfoContainer.CompilationInfos.Add(("Failed project restore with missing package error", nugetMissingPackageFailures.ToString()));
313314
}
314315

315-
private AssemblyLookupLocation? DownloadMissingPackagesFromSpecificFeeds(IEnumerable<string> usedPackageNames, HashSet<string>? feedsFromNugetConfigs)
316+
private AssemblyLookupLocation? DownloadMissingPackagesFromSpecificFeeds(IEnumerable<string> usedPackageNames, ImmutableHashSet<string>? feedsFromNugetConfigs)
316317
{
317318
var reachableFallbackFeeds = feedManager.GetReachableFallbackNugetFeeds(feedsFromNugetConfigs);
318319
compilationInfoContainer.CompilationInfos.Add(("Reachable fallback NuGet feed count", reachableFallbackFeeds.Count.ToString()));

0 commit comments

Comments
 (0)