-
Notifications
You must be signed in to change notification settings - Fork 756
Expand file tree
/
Copy pathPackageUpdateIO.cs
More file actions
505 lines (439 loc) · 20.2 KB
/
Copy pathPackageUpdateIO.cs
File metadata and controls
505 lines (439 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.CommandLine.XPlat.Utility;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
using NuGet.Shared;
using NuGet.Versioning;
using static NuGet.CommandLine.XPlat.Commands.Package.Update.PackageUpdateCommandRunner;
namespace NuGet.CommandLine.XPlat.Commands.Package.Update;
/// <summary>
/// Implementation of IPackageUpdateIO that handles package updates by performing restore operations.
/// </summary>
internal class PackageUpdateIO : IPackageUpdateIO, IDisposable
{
private readonly MSBuildAPIUtility _msbuildUtility;
private readonly IEnvironmentVariableReader _environmentVariableReader;
private readonly ISettings _settings;
private readonly IPackageSourceProvider _sourceProvider;
private readonly CachingSourceProvider _cachingSourceProvider;
private readonly IReadOnlyList<PackageSource> _enabledSources;
private readonly SourceCacheContext _sourceCacheContext;
public PackageUpdateIO(string solutionDirectory, MSBuildAPIUtility msbuildUtility, IEnvironmentVariableReader environmentVariableReader)
{
_msbuildUtility = msbuildUtility;
_environmentVariableReader = environmentVariableReader;
// the CommandLine option validates that an existing filesystem object is provided, so we can be confident that
// we either have a directory or a file here.
string settingsRoot = Directory.Exists(solutionDirectory) ? solutionDirectory : Path.GetDirectoryName(solutionDirectory)!;
_settings = Settings.LoadDefaultSettings(solutionDirectory);
_sourceProvider = new PackageSourceProvider(_settings);
_cachingSourceProvider = new CachingSourceProvider(_sourceProvider);
_enabledSources = SettingsUtility.GetEnabledSources(_settings).AsList();
_sourceCacheContext = new SourceCacheContext();
}
public void Dispose()
{
_sourceCacheContext.Dispose();
GC.SuppressFinalize(this);
}
/// <inheritdoc cref="IPackageUpdateIO.GetDependencyGraphSpec(string)"/>
public DependencyGraphSpec? GetDependencyGraphSpec(string project)
{
string tempFile = Path.GetTempFileName();
try
{
if (!RunMsbuildTarget(project, tempFile))
{
return null;
}
DependencyGraphSpec result = DependencyGraphSpec.Load(tempFile);
// Fixup virtual project paths.
if (_msbuildUtility.VirtualProjectBuilder?.GetVirtualProjectPath(project) is { } virtualProjectPath)
{
foreach (var packageSpec in result.Projects)
{
if (packageSpec.FilePath == virtualProjectPath)
{
packageSpec.FilePath = project;
}
}
}
return result;
}
finally
{
File.Delete(tempFile);
}
bool RunMsbuildTarget(string project, string tempFile)
{
// When being run from the dotnet CLI, use the same dotnet executable, just in case the dotnet on the PATH is different
// But when NuGet.CommandLine.XPlat is being called directly, call dotnet on the path, so this code is debuggable.
string dotnetPath = _environmentVariableReader.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet";
bool isFileBasedApp = _msbuildUtility.VirtualProjectBuilder?.IsValidEntryPointPath(project) == true;
// don't redirect stdout or stderr, so errors are output. But use quiet verbosity, so that success has no output.
ProcessStartInfo processStartInfo = new ProcessStartInfo(dotnetPath)
{
Arguments = (isFileBasedApp ? "build " : "msbuild ") +
$"\"{project}\" " +
(isFileBasedApp ? "--no-restore " : "-restore:false ") +
"-target:GenerateRestoreGraphFile " +
$"-property:RestoreGraphOutputPath=\"{tempFile}\" " +
"-property:RestoreRecursive=false " +
"-nologo " +
"-verbosity:quiet " +
(!isFileBasedApp ? $"-noautoresponse" : null), // currently not supported for file-based apps
UseShellExecute = false,
Environment =
{
{ "MSBUILDTERMINALLOGGER", "off" },
},
};
using var process = Process.Start(processStartInfo);
if (process is null) throw new System.Exception("Unexpected error starting child process. Process.Start returned null.");
process.WaitForExit();
return process.ExitCode == 0;
}
}
/// <inheritdoc cref="IPackageUpdateIO.PreviewUpdatePackageReferenceAsync(DependencyGraphSpec, ILogger, CancellationToken)"/>
public async Task<IPackageUpdateIO.RestoreResult> PreviewUpdatePackageReferenceAsync(
DependencyGraphSpec dgSpec,
ILogger logger,
CancellationToken cancellationToken)
{
var providerCache = new RestoreCommandProvidersCache();
// Restore outputs a lot of messages at normal verbosity, which update doesn't want.
var restoreLogger = new RemappedLevelLogger(
logger,
new RemappedLevelLogger.Mapping
{
Information = LogLevel.Verbose,
Minimal = LogLevel.Verbose,
});
// Pre-loaded request provider containing the graph file
var providers = new List<IPreLoadedRestoreRequestProvider>
{
new DependencyGraphSpecRequestProvider(providerCache, dgSpec)
};
var restoreContext = new RestoreArgs()
{
CacheContext = _sourceCacheContext,
Log = restoreLogger,
MachineWideSettings = new XPlatMachineWideSetting(),
PreLoadedRequestProviders = providers
// Sources : No need to pass it, because SourceRepositories contains the already built SourceRepository objects
};
var restoreRequests = await RestoreRunner.GetRequests(restoreContext);
var restoreResult = await RestoreRunner.RunWithoutCommitAsync(restoreRequests, restoreContext, cancellationToken);
var result = new RestoreResult
{
RestoreResultPairs = restoreResult
};
return result;
}
/// <inheritdoc cref="IPackageUpdateIO.CommitAsync(IPackageUpdateIO.RestoreResult, CancellationToken)"/>
public async Task CommitAsync(IPackageUpdateIO.RestoreResult restorePreviewResult, CancellationToken none)
{
var restoreResult = (RestoreResult)restorePreviewResult;
foreach (var restoreResultPair in restoreResult.RestoreResultPairs)
{
await RestoreRunner.CommitAsync(restoreResultPair, CancellationToken.None);
}
}
/// <inheritdoc cref="IPackageUpdateIO.UpdatePackageReference(PackageSpec, IPackageUpdateIO.RestoreResult, List{string}, PackageToUpdate, ILogger)"/>
public void UpdatePackageReference(PackageSpec updatedPackageSpec, IPackageUpdateIO.RestoreResult restorePreviewResult, List<string> packageTfmAliases, PackageToUpdate packageToUpdate, ILogger logger)
{
PackageDependency packageDependency = new PackageDependency(packageToUpdate.Id, packageToUpdate.NewVersion);
var restoreResult = (RestoreResult)restorePreviewResult;
var restoreResultPair = restoreResult.RestoreResultPairs.Single(pair =>
string.Equals(pair.SummaryRequest.Request.Project.FilePath, updatedPackageSpec.FilePath, StringComparison.OrdinalIgnoreCase));
if (!AddPackageReferenceCommandRunner.TryFindResolvedVersion(packageTfmAliases,
packageDependency.Id,
restoreResultPair.Result,
logger,
out NuGetVersion resolvedVersion))
{
return;
}
// Generate the LibraryDependency using the same logic as AddPackageReferenceCommandRunner
var libraryDependency = AddPackageReferenceCommandRunner.GenerateLibraryDependency(
updatedPackageSpec,
customPackagesPath: null,
packageDependency,
resolvedVersion);
// MSBuildUtility only updated CPM Directory.Packages.props when "noVersion" is false.
const bool noVersion = false;
// Determine whether to add package reference conditionally or unconditionally
if (packageTfmAliases.Count == updatedPackageSpec.TargetFrameworks.Count)
{
// package is used by all project TFMs (no condition)
_msbuildUtility.AddPackageReference(updatedPackageSpec.FilePath, libraryDependency, noVersion);
}
else
{
_msbuildUtility.AddPackageReferencePerTFM(updatedPackageSpec.FilePath, libraryDependency, packageTfmAliases, noVersion);
}
}
/// <inheritdoc cref="IPackageUpdateIO.GetLatestVersionAsync(string, bool, IReadOnlyList{string}?, ILogger, CancellationToken)"/>
public async Task<NuGetVersion?> GetLatestVersionAsync(
string packageId,
bool includePrerelease,
IReadOnlyList<string>? allowedSources,
ILogger logger,
CancellationToken cancellationToken)
{
return await GetLatestVersionAsync(
packageId,
includePrerelease,
allowedSources,
allowedVersions: null,
logger,
cancellationToken);
}
/// <inheritdoc cref="IPackageUpdateIO.GetLatestVersionAsync(string, bool, IReadOnlyList{string}?, VersionRange?, ILogger, CancellationToken)"/>
public async Task<NuGetVersion?> GetLatestVersionAsync(
string packageId,
bool includePrerelease,
IReadOnlyList<string>? allowedSources,
VersionRange? allowedVersions,
ILogger logger,
CancellationToken cancellationToken)
{
var sources = GetSourcesForPackage(packageId, allowedSources);
var lookups = new Task<NuGetVersion?>[sources.Count];
for (int source = 0; source < sources.Count; source++)
{
SourceRepository sourceRepository = sources[source];
// If package source is a local folder feed, it might not actually be async
lookups[source] = Task.Run(() => FindHighestPackageVersionAsync(sourceRepository, packageId, includePrerelease, allowedVersions, logger, cancellationToken));
}
await Task.WhenAll(lookups);
NuGetVersion? highestVersion = null;
foreach (var task in lookups)
{
if (task.Result != null)
{
if (highestVersion == null || task.Result > highestVersion)
{
highestVersion = task.Result;
}
}
}
return highestVersion;
}
/// <inheritdoc cref="IPackageUpdateIO.GetKnownVulnerabilitiesAsync(ILogger, CancellationToken)"/>
public async Task<IReadOnlyList<IReadOnlyDictionary<string, IReadOnlyList<PackageVulnerabilityInfo>>>> GetKnownVulnerabilitiesAsync(ILogger logger, CancellationToken cancellationToken)
{
IReadOnlyList<PackageSource>? auditSources = _sourceProvider.LoadAuditSources()?.Where(s => s.IsEnabled).ToList();
if (auditSources is null || auditSources.Count == 0)
{
auditSources = _enabledSources;
}
var tasks = new List<Task<GetVulnerabilityInfoResult?>>(auditSources.Count);
foreach (var auditSource in auditSources)
{
tasks.Add(Task.Run(async () =>
{
var sourceRepository = Repository.Factory.GetCoreV3(auditSource.Source);
var vulnerabilityResource = await sourceRepository.GetResourceAsync<IVulnerabilityInfoResource>(cancellationToken);
if (vulnerabilityResource is not null)
{
var vulnerabilities = await vulnerabilityResource.GetVulnerabilityInfoAsync(_sourceCacheContext, logger, cancellationToken);
return vulnerabilities;
}
return null;
}, cancellationToken));
}
List<IReadOnlyDictionary<string, IReadOnlyList<PackageVulnerabilityInfo>>> allVulnerabilities = new();
foreach (var task in tasks)
{
var result = await task;
if (result is not null)
{
if (result.KnownVulnerabilities?.Count > 0)
{
foreach (var vulnDict in result.KnownVulnerabilities)
{
allVulnerabilities.Add(vulnDict);
}
}
}
}
return allVulnerabilities;
}
/// <inheritdoc cref="IPackageUpdateIO.GetNonVulnerableAsync(string, IReadOnlyList{string}?, NuGetVersion, ILogger, IReadOnlyList{IReadOnlyDictionary{string, IReadOnlyList{PackageVulnerabilityInfo}}}, CancellationToken)"/>
public async Task<NuGetVersion?> GetNonVulnerableAsync(
string packageId,
IReadOnlyList<string>? allowedSources,
NuGetVersion minVersion,
ILogger logger,
IReadOnlyList<IReadOnlyDictionary<string, IReadOnlyList<PackageVulnerabilityInfo>>> knownVulnerabilities,
CancellationToken cancellationToken)
{
var sources = GetSourcesForPackage(packageId, allowedSources);
var lookups = new Task<NuGetVersion?>[sources.Count];
for (int source = 0; source < sources.Count; source++)
{
SourceRepository sourceRepository = sources[source];
// If package source is a local folder feed, it might not actually be async
lookups[source] = Task.Run(() => FindLowestNonVulnerablePackageVersionAsync(sourceRepository, packageId, minVersion, knownVulnerabilities, logger, cancellationToken));
}
await Task.WhenAll(lookups);
NuGetVersion? lowestNonVulnerableVersion = null;
foreach (var task in lookups)
{
if (task.Result != null)
{
if (lowestNonVulnerableVersion == null || task.Result < lowestNonVulnerableVersion)
{
lowestNonVulnerableVersion = task.Result;
}
}
}
return lowestNonVulnerableVersion;
}
public PackageSourceMapping GetPackageSourceMapping()
{
return PackageSourceMapping.GetPackageSourceMapping(_settings);
}
private List<SourceRepository> GetSourcesForPackage(string packageId, IReadOnlyList<string>? allowedSources)
{
IReadOnlyList<PackageSource> packageSources;
// Apply package source mapping if enabled
if (allowedSources is not null)
{
if (allowedSources.Count == 0)
{
throw new ArgumentException("The allowedSources list must contain at least one source if specified.", nameof(allowedSources));
}
List<PackageSource> sourceMappedSources = new List<PackageSource>(allowedSources.Count);
sourceMappedSources.AddRange(_enabledSources.Where(ps => allowedSources.Contains(ps.Name, StringComparer.OrdinalIgnoreCase)));
packageSources = sourceMappedSources;
}
else
{
packageSources = _enabledSources;
}
var sources = new List<SourceRepository>(packageSources.Count);
for (int i = 0; i < packageSources.Count; i++)
{
SourceRepository sourceRepository = _cachingSourceProvider.CreateRepository(packageSources[i]);
sources.Add(sourceRepository);
}
return sources;
}
private async Task<NuGetVersion?>? FindLowestNonVulnerablePackageVersionAsync(
SourceRepository source,
string packageId,
NuGetVersion minVersion,
IReadOnlyList<IReadOnlyDictionary<string, IReadOnlyList<PackageVulnerabilityInfo>>> knownVulnerabilities,
ILogger logger,
CancellationToken cancellationToken)
{
var packageMetadataResource = await source.GetResourceAsync<PackageMetadataResource>(cancellationToken);
var packageDetails = await packageMetadataResource.GetMetadataAsync(
packageId,
includePrerelease: false,
includeUnlisted: false,
_sourceCacheContext,
logger,
cancellationToken);
if (packageDetails is null || !packageDetails.Any())
{
return null;
}
var versions = packageDetails
.Select(p => p.Identity)
.Where(p => p.Version >= minVersion && !PackageHasKnownVulnerability(p))
.Select(p => p.Version);
VersionRange versionRange = new VersionRange(minVersion, includeMinVersion: true, maxVersion: null, includeMaxVersion: true);
NuGetVersion? result = versionRange.FindBestMatch(versions);
return result;
bool PackageHasKnownVulnerability(PackageIdentity package)
{
foreach (var sourceVulnerabilities in knownVulnerabilities)
{
if (sourceVulnerabilities.TryGetValue(packageId, out var vulnerabilities))
{
foreach (var vulnerability in vulnerabilities)
{
if (vulnerability.Versions.Satisfies(package.Version))
{
return true;
}
}
}
}
return false;
}
}
private async Task<NuGetVersion?> FindHighestPackageVersionAsync(
SourceRepository source,
string packageId,
bool includePrerelease,
VersionRange? allowedVersions,
ILogger logger,
CancellationToken cancellationToken)
{
var packageMetadataResource = await source.GetResourceAsync<PackageMetadataResource>(cancellationToken);
var packageDetails = await packageMetadataResource.GetMetadataAsync(
packageId,
includePrerelease: includePrerelease,
includeUnlisted: false,
_sourceCacheContext,
logger,
cancellationToken);
if (packageDetails is null || !packageDetails.Any())
{
return null;
}
NuGetVersion? highestVersion = packageDetails
.Select(p => p.Identity.Version)
.Where(version => allowedVersions == null || allowedVersions.Satisfies(version))
.Max();
return highestVersion;
}
/// <inheritdoc cref="IPackageUpdateIO.GetProjectAssetsFileAsync(DependencyGraphSpec, string, ILogger, CancellationToken)"/>
public async Task<LockFile> GetProjectAssetsFileAsync(
DependencyGraphSpec dgSpec,
string projectPath,
ILogger logger,
CancellationToken cancellationToken)
{
var previewRestoreResult = (RestoreResult)await PreviewUpdatePackageReferenceAsync(dgSpec, NullLogger.Instance, cancellationToken);
if (!previewRestoreResult.Success)
{
logger.LogError("Restore failed");
throw new NotSupportedException();
}
var restoreResultPair = previewRestoreResult.RestoreResultPairs.Single(pair =>
string.Equals(pair.SummaryRequest.Request.Project.FilePath, projectPath, StringComparison.OrdinalIgnoreCase));
LockFile? assetsFile = restoreResultPair.Result.LockFile;
if (assetsFile is null)
{
var packageSpec = dgSpec.GetProjectSpec(projectPath);
var assetsFilePath = Path.Combine(packageSpec.RestoreMetadata.OutputPath, LockFileFormat.AssetsFileName);
assetsFile = new LockFileFormat().Read(assetsFilePath);
}
return assetsFile;
}
internal class RestoreResult : IPackageUpdateIO.RestoreResult
{
internal required IReadOnlyList<RestoreResultPair> RestoreResultPairs { get; init; }
public override bool Success => RestoreResultPairs.All(pair => pair.Result.Success);
}
}