Skip to content

Commit 9ea2def

Browse files
authored
Pass through the cache context to the vulnerability resource (#5361)
1 parent e873b49 commit 9ea2def

4 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommandProviders.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ public RestoreCommandProviders(
3939
private static IReadOnlyList<IVulnerabilityInformationProvider> CreateVulnerabilityInfoProviders(IReadOnlyList<IRemoteDependencyProvider> remoteProviders)
4040
{
4141
List<IVulnerabilityInformationProvider> providers = new(remoteProviders.Count);
42+
using var sourceCacheContext = new SourceCacheContext();
4243
for (int i = 0; i < remoteProviders.Count; i++)
4344
{
4445
if (remoteProviders[i].SourceRepository != null)
4546
{
46-
providers.Add(new VulnerabilityInformationProvider(remoteProviders[i].SourceRepository, NullLogger.Instance));
47+
providers.Add(new VulnerabilityInformationProvider(remoteProviders[i].SourceRepository, sourceCacheContext, NullLogger.Instance));
4748
}
4849
}
4950
return providers;
@@ -150,7 +151,7 @@ public static RestoreCommandProviders Create(
150151
var vulnerabilityInfoProviders = new List<IVulnerabilityInformationProvider>(remoteProviders.Count);
151152
foreach (SourceRepository source in sources)
152153
{
153-
var provider = new VulnerabilityInformationProvider(source, log);
154+
var provider = new VulnerabilityInformationProvider(source, cacheContext, log);
154155
vulnerabilityInfoProviders.Add(provider);
155156
}
156157

src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommandProvidersCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public RestoreCommandProviders GetOrCreate(
120120
var vulnerabilityInformationProviders = new List<IVulnerabilityInformationProvider>(sources.Count);
121121
foreach (SourceRepository source in sources)
122122
{
123-
IVulnerabilityInformationProvider provider = _vulnerabilityInformationProviders.GetOrAdd(source, s => new VulnerabilityInformationProvider(s, log));
123+
IVulnerabilityInformationProvider provider = _vulnerabilityInformationProviders.GetOrAdd(source, s => new VulnerabilityInformationProvider(s, cacheContext, log));
124124
vulnerabilityInformationProviders.Add(provider);
125125
}
126126

src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/AuditUtility.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ private PackageVulnerabilitySeverity ParseAuditLevel()
399399

400400
internal enum NuGetAuditMode { Unknown, Direct, All }
401401

402+
// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
402403
private NuGetAuditMode ParseAuditMode()
403404
{
404405
string? auditMode = _restoreAuditProperties?.AuditMode?.Trim();
@@ -431,6 +432,7 @@ internal enum EnabledValue
431432
ExplicitOptOut
432433
}
433434

435+
// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
434436
public static EnabledValue ParseEnableValue(string? value, string projectFullPath, ILogger logger)
435437
{
436438
if (string.IsNullOrEmpty(value) || string.Equals(value, "default", StringComparison.OrdinalIgnoreCase))
@@ -455,6 +457,7 @@ public static EnabledValue ParseEnableValue(string? value, string projectFullPat
455457
return EnabledValue.Invalid;
456458
}
457459

460+
// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
458461
internal static string GetString(EnabledValue enableAudit)
459462
{
460463
return enableAudit switch
@@ -467,6 +470,7 @@ internal static string GetString(EnabledValue enableAudit)
467470
};
468471
}
469472

473+
// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
470474
internal static string GetString(NuGetAuditMode auditMode)
471475
{
472476
return auditMode switch

src/NuGet.Core/NuGet.Commands/RestoreCommand/VulnerabilityInformationProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ namespace NuGet.Commands
1515
internal sealed class VulnerabilityInformationProvider : IVulnerabilityInformationProvider
1616
{
1717
private readonly SourceRepository _source;
18+
private readonly SourceCacheContext _sourceCacheContext;
1819
private readonly ILogger _logger;
1920

2021
private readonly AsyncLazy<GetVulnerabilityInfoResult?> _vulnerabilityInfo;
2122

22-
public VulnerabilityInformationProvider(SourceRepository source, ILogger logger)
23+
public VulnerabilityInformationProvider(SourceRepository source, SourceCacheContext cacheContext, ILogger logger)
2324
{
2425
_source = source;
26+
_sourceCacheContext = cacheContext;
2527
_logger = logger;
2628

2729
_vulnerabilityInfo = new AsyncLazy<GetVulnerabilityInfoResult?>(GetVulnerabilityInfoAsync);
@@ -42,8 +44,7 @@ public VulnerabilityInformationProvider(SourceRepository source, ILogger logger)
4244
return null;
4345
}
4446

45-
using SourceCacheContext cacheContext = new();
46-
GetVulnerabilityInfoResult result = await vulnerabilityInfoResource.GetVulnerabilityInfoAsync(cacheContext, _logger, CancellationToken.None);
47+
GetVulnerabilityInfoResult result = await vulnerabilityInfoResource.GetVulnerabilityInfoAsync(_sourceCacheContext, _logger, CancellationToken.None);
4748
return result;
4849
}
4950
}

0 commit comments

Comments
 (0)