Skip to content

Commit ad0d1cb

Browse files
committed
C#: Address review comments.
1 parent 8952df2 commit ad0d1cb

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Collections.Immutable;
45
using System.IO;
56
using System.Linq;
67
using System.Net;
@@ -30,9 +31,9 @@ internal sealed partial class NugetPackageRestorer : IDisposable
3031
private readonly DependencyDirectory emptyPackageDirectory;
3132
private readonly ILogger logger;
3233
private readonly ICompilationInfoContainer compilationInfoContainer;
33-
private bool CheckNugetFeedResponsiveness { get; } = EnvironmentVariables.GetBooleanOptOut(EnvironmentVariableNames.CheckNugetFeedResponsiveness);
34-
private HashSet<string> PrivateRegistryFeeds => dependabotProxy?.RegistryURLs ?? [];
35-
private bool HasPrivateRegistryFeeds => PrivateRegistryFeeds.Any();
34+
private readonly bool checkNugetFeedResponsiveness = EnvironmentVariables.GetBooleanOptOut(EnvironmentVariableNames.CheckNugetFeedResponsiveness);
35+
private readonly ImmutableHashSet<string> privateRegistryFeeds;
36+
private readonly bool hasPrivateRegistryFeeds;
3637

3738
public DependencyDirectory PackageDirectory { get; }
3839

@@ -49,6 +50,8 @@ public NugetPackageRestorer(
4950
this.fileContent = fileContent;
5051
this.dotnet = dotnet;
5152
this.dependabotProxy = dependabotProxy;
53+
this.privateRegistryFeeds = dependabotProxy?.RegistryURLs.ToImmutableHashSet() ?? [];
54+
this.hasPrivateRegistryFeeds = privateRegistryFeeds.Count > 0;
5255
this.diagnosticsWriter = diagnosticsWriter;
5356
this.logger = logger;
5457
this.compilationInfoContainer = compilationInfoContainer;
@@ -115,8 +118,8 @@ public DirectoryInfo[] GetOrderedPackageVersionSubDirectories(string packagePath
115118
public HashSet<AssemblyLookupLocation> Restore()
116119
{
117120
var assemblyLookupLocations = new HashSet<AssemblyLookupLocation>();
118-
logger.LogInfo($"Checking NuGet feed responsiveness: {CheckNugetFeedResponsiveness}");
119-
compilationInfoContainer.CompilationInfos.Add(("NuGet feed responsiveness checked", CheckNugetFeedResponsiveness ? "1" : "0"));
121+
logger.LogInfo($"Checking NuGet feed responsiveness: {checkNugetFeedResponsiveness}");
122+
compilationInfoContainer.CompilationInfos.Add(("NuGet feed responsiveness checked", checkNugetFeedResponsiveness ? "1" : "0"));
120123

121124
HashSet<string> explicitFeeds = [];
122125
HashSet<string> reachableFeeds = [];
@@ -128,7 +131,7 @@ public HashSet<AssemblyLookupLocation> Restore()
128131
// (including inherited ones) from other locations on the host outside of the working directory.
129132
(explicitFeeds, var allFeeds) = GetAllFeeds();
130133

131-
if (CheckNugetFeedResponsiveness)
134+
if (checkNugetFeedResponsiveness)
132135
{
133136
var inheritedFeeds = allFeeds.Except(explicitFeeds).ToHashSet();
134137

@@ -212,7 +215,7 @@ public HashSet<AssemblyLookupLocation> Restore()
212215

213216
var usedPackageNames = GetAllUsedPackageDirNames(dependencies);
214217

215-
var missingPackageLocation = CheckNugetFeedResponsiveness
218+
var missingPackageLocation = checkNugetFeedResponsiveness
216219
? DownloadMissingPackagesFromSpecificFeeds(usedPackageNames, explicitFeeds)
217220
: DownloadMissingPackages(usedPackageNames);
218221

@@ -261,7 +264,7 @@ private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool i
261264

262265
private bool IsDefaultFeedReachable()
263266
{
264-
if (CheckNugetFeedResponsiveness)
267+
if (checkNugetFeedResponsiveness)
265268
{
266269
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback: false);
267270
return IsFeedReachable(PublicNugetOrgFeed, initialTimeout, tryCount, out var _);
@@ -373,7 +376,7 @@ private string FeedsToRestoreArgument(IEnumerable<string> feeds)
373376
private string? MakeRestoreSourcesArgument(string path, HashSet<string> reachableFeeds)
374377
{
375378
// Do not construct an set of explicit NuGet sources to use for restore.
376-
if (!CheckNugetFeedResponsiveness && !HasPrivateRegistryFeeds)
379+
if (!checkNugetFeedResponsiveness && !hasPrivateRegistryFeeds)
377380
{
378381
return null;
379382
}
@@ -382,12 +385,12 @@ private string FeedsToRestoreArgument(IEnumerable<string> feeds)
382385
var folder = GetDirectoryName(path);
383386
var feedsToConsider = folder is not null ? GetFeeds(() => dotnet.GetNugetFeedsFromFolder(folder)).ToHashSet() : [];
384387

385-
if (HasPrivateRegistryFeeds)
388+
if (hasPrivateRegistryFeeds)
386389
{
387-
feedsToConsider.UnionWith(PrivateRegistryFeeds);
390+
feedsToConsider.UnionWith(privateRegistryFeeds);
388391
}
389392

390-
var feedsToUse = CheckNugetFeedResponsiveness
393+
var feedsToUse = checkNugetFeedResponsiveness
391394
? feedsToConsider.Where(reachableFeeds.Contains)
392395
: feedsToConsider;
393396

@@ -992,10 +995,10 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
992995

993996
// If private package registries are configured for C#, then consider those
994997
// in addition to the ones that are configured in `nuget.config` files.
995-
if (HasPrivateRegistryFeeds)
998+
if (hasPrivateRegistryFeeds)
996999
{
997-
logger.LogInfo($"Found {PrivateRegistryFeeds.Count} private registry feeds configured for C#: {string.Join(", ", PrivateRegistryFeeds.OrderBy(f => f))}");
998-
explicitFeeds.UnionWith(PrivateRegistryFeeds);
1000+
logger.LogInfo($"Found {privateRegistryFeeds.Count} private registry feeds configured for C#: {string.Join(", ", privateRegistryFeeds.OrderBy(f => f))}");
1001+
explicitFeeds.UnionWith(privateRegistryFeeds);
9991002
}
10001003

10011004
HashSet<string> allFeeds = [];

0 commit comments

Comments
 (0)