-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathPackagesLockFileUtilities.cs
618 lines (538 loc) · 31.9 KB
/
PackagesLockFileUtilities.cs
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// 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.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.ProjectModel.ProjectLockFile;
using NuGet.Shared;
namespace NuGet.ProjectModel
{
public static class PackagesLockFileUtilities
{
public static bool IsNuGetLockFileEnabled(PackageSpec project)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
var restorePackagesWithLockFile = project.RestoreMetadata?.RestoreLockProperties.RestorePackagesWithLockFile;
return MSBuildStringUtility.IsTrue(restorePackagesWithLockFile) || File.Exists(GetNuGetLockFilePath(project));
}
public static string GetNuGetLockFilePath(PackageSpec project)
{
if (project.RestoreMetadata == null || project.BaseDirectory == null)
{
// RestoreMetadata or project BaseDirectory is not set which means it's probably called through test.
return null;
}
var path = project.RestoreMetadata.RestoreLockProperties.NuGetLockFilePath;
if (!string.IsNullOrEmpty(path))
{
return Path.Combine(project.BaseDirectory, path);
}
var projectName = Path.GetFileNameWithoutExtension(project.RestoreMetadata.ProjectPath);
return GetNuGetLockFilePath(project.BaseDirectory, projectName);
}
public static string GetNuGetLockFilePath(string baseDirectory, string projectName)
{
if (!string.IsNullOrEmpty(projectName))
{
var path = Path.Combine(baseDirectory, "packages." + projectName.Replace(' ', '_') + ".lock.json");
if (File.Exists(path))
{
return path;
}
}
return Path.Combine(baseDirectory, PackagesLockFileFormat.LockFileName);
}
[Obsolete("This method is obsolete. Call IsLockFileValid instead.")]
public static bool IsLockFileStillValid(DependencyGraphSpec dgSpec, PackagesLockFile nuGetLockFile)
{
return IsLockFileValid(dgSpec, nuGetLockFile).IsValid;
}
/// <summary>
/// The lock file will get invalidated if one or more of the below are true
/// 1. The target frameworks list of the current project was updated.
/// 2. The runtime list of the current project waw updated.
/// 3. The packages of the current project were updated.
/// 4. The packages of the dependent projects were updated.
/// 5. The framework list of the dependent projects were updated with frameworks incompatible with the main project framework.
/// 6. If the version of the <paramref name="nuGetLockFile"/> is larger than the current tools <see cref="PackagesLockFileFormat.PackagesLockFileVersion"/>.
/// </summary>
/// <param name="dgSpec">The <see cref="DependencyGraphSpec"/> for the new project defintion.</param>
/// <param name="nuGetLockFile">The current <see cref="PackagesLockFile"/>.</param>
/// <returns>Returns LockFileValidityWithInvalidReasons object with IsValid set to true if the lock file is valid false otherwise.
/// The second return type is a localized message that indicates in further detail the reason for the inconsistency.</returns>
public static LockFileValidationResult IsLockFileValid(DependencyGraphSpec dgSpec, PackagesLockFile nuGetLockFile)
{
if (dgSpec == null)
throw new ArgumentNullException(nameof(dgSpec));
if (nuGetLockFile == null)
throw new ArgumentNullException(nameof(nuGetLockFile));
List<string> invalidReasons = new List<string>();
// Current tools know how to read only previous formats including the current
if (PackagesLockFileFormat.PackagesLockFileVersion < nuGetLockFile.Version)
{
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_IncompatibleLockFileVersion,
PackagesLockFileFormat.PackagesLockFileVersion
));
return new LockFileValidationResult(false, invalidReasons);
}
var uniqueName = dgSpec.Restore.First();
var project = dgSpec.GetProjectSpec(uniqueName);
// Validate all the direct dependencies
NuGetFramework[] lockFileFrameworks = nuGetLockFile.Targets
.Where(t => t.TargetFramework != null)
.Select(t => t.TargetFramework)
.Distinct()
.ToArray();
if (project.TargetFrameworks.Count != lockFileFrameworks.Length)
{
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_MismatchedTargetFrameworks,
string.Join(",", project.TargetFrameworks.Select(e => e.FrameworkName.GetShortFolderName())),
string.Join(",", lockFileFrameworks.Select(e => e.GetShortFolderName()))
));
}
else
{
// Validate the runtimes for the current project did not change.
var projectRuntimesKeys = project.RuntimeGraph.Runtimes.Select(r => r.Key).Where(k => k != null);
var lockFileRuntimes = nuGetLockFile.Targets.Select(t => t.RuntimeIdentifier).Where(r => r != null).Distinct();
if (!projectRuntimesKeys.OrderedEquals(
lockFileRuntimes,
x => x,
StringComparer.InvariantCultureIgnoreCase,
StringComparer.InvariantCultureIgnoreCase))
{
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_RuntimeIdentifiersChanged,
string.Join(";", projectRuntimesKeys.OrderBy(e => e)),
string.Join(";", lockFileRuntimes.OrderBy(e => e))
));
}
foreach (var framework in project.TargetFrameworks)
{
var target = nuGetLockFile.Targets.FirstOrDefault(
t => EqualityUtility.EqualsWithNullCheck(t.TargetFramework, framework.FrameworkName));
if (target == null)
{
// a new target found in the dgSpec so invalidate existing lock file.
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_NewTargetFramework,
framework.FrameworkName.GetShortFolderName())
);
continue;
}
IEnumerable<LockFileDependency> directDependencies = target.Dependencies.Where(dep => dep.Type == PackageDependencyType.Direct);
(var hasProjectDependencyChanged, var pmessage) = HasDirectPackageDependencyChanged(framework.Dependencies, directDependencies, target.TargetFramework);
if (hasProjectDependencyChanged)
{
// lock file is out of sync
invalidReasons.Add(pmessage);
}
var transitiveDependenciesEnforcedByCentralVersions = target.Dependencies.Where(dep => dep.Type == PackageDependencyType.CentralTransitive).ToList();
var transitiveDependencies = target.Dependencies.Where(dep => dep.Type == PackageDependencyType.Transitive).ToList();
(var hasTransitiveDependencyChanged, var tmessage) = HasProjectTransitiveDependencyChanged(framework.CentralPackageVersions, transitiveDependenciesEnforcedByCentralVersions, transitiveDependencies);
if (hasTransitiveDependencyChanged)
{
// lock file is out of sync
invalidReasons.Add(tmessage);
}
}
// Validate all P2P references
foreach (var restoreMetadataFramework in project.RestoreMetadata.TargetFrameworks)
{
var target = nuGetLockFile.Targets.FirstOrDefault(
t => EqualityUtility.EqualsWithNullCheck(t.TargetFramework, restoreMetadataFramework.FrameworkName));
if (target == null)
continue;
var queue = new Queue<Tuple<string, string>>();
var visitedP2PReference = new HashSet<string>();
foreach (var projectReference in restoreMetadataFramework.ProjectReferences)
{
if (visitedP2PReference.Add(projectReference.ProjectUniqueName))
{
PackageSpec spec = dgSpec.GetProjectSpec(projectReference.ProjectUniqueName);
queue.Enqueue(new Tuple<string, string>(spec.Name, projectReference.ProjectUniqueName));
while (queue.Count > 0)
{
var projectNames = queue.Dequeue();
var p2pUniqueName = projectNames.Item2;
var p2pProjectName = projectNames.Item1;
var projectDependency = target.Dependencies.FirstOrDefault(
dep => dep.Type == PackageDependencyType.Project &&
StringComparer.OrdinalIgnoreCase.Equals(dep.Id, p2pProjectName));
if (projectDependency == null)
{
// new direct project dependency.
// If there are changes in the P2P2P references, they will be caught in HasP2PDependencyChanged.
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectReferenceAdded,
p2pProjectName,
target.TargetFramework.GetShortFolderName()
));
continue;
}
var p2pSpec = dgSpec.GetProjectSpec(p2pUniqueName);
if (p2pSpec != null)
{
TargetFrameworkInformation p2pSpecTargetFrameworkInformation = default;
if (p2pSpec.RestoreMetadata.ProjectStyle == ProjectStyle.PackagesConfig || p2pSpec.RestoreMetadata.ProjectStyle == ProjectStyle.Unknown)
{
// Skip compat check and dependency check for non PR projects.
// Projects that are not PR do not undergo compat checks by NuGet and do not contribute anything transitively.
p2pSpecTargetFrameworkInformation = p2pSpec.TargetFrameworks.FirstOrDefault();
}
else
{
// This does not consider ATF.
p2pSpecTargetFrameworkInformation = NuGetFrameworkUtility.GetNearest(p2pSpec.TargetFrameworks, restoreMetadataFramework.FrameworkName, e => e.FrameworkName);
}
// No compatible framework found
if (p2pSpecTargetFrameworkInformation != null)
{
// We need to compare the main framework only. Ignoring fallbacks.
var p2pSpecProjectRestoreMetadataFrameworkInfo = p2pSpec.RestoreMetadata.TargetFrameworks.FirstOrDefault(
t => NuGetFramework.Comparer.Equals(p2pSpecTargetFrameworkInformation.FrameworkName, t.FrameworkName));
if (p2pSpecProjectRestoreMetadataFrameworkInfo != null)
{
(var hasChanged, var message) = HasP2PDependencyChanged(p2pSpecTargetFrameworkInformation.Dependencies, p2pSpecProjectRestoreMetadataFrameworkInfo.ProjectReferences, projectDependency, dgSpec);
if (hasChanged)
{
// P2P transitive package dependencies have changed
invalidReasons.Add(message);
}
foreach (var reference in p2pSpecProjectRestoreMetadataFrameworkInfo.ProjectReferences)
{
// Do not add private assets for processing.
if (visitedP2PReference.Add(reference.ProjectUniqueName) && reference.PrivateAssets != LibraryIncludeFlags.All)
{
var referenceSpec = dgSpec.GetProjectSpec(reference.ProjectUniqueName);
queue.Enqueue(new Tuple<string, string>(referenceSpec.Name, reference.ProjectUniqueName));
}
}
}
else // This should never happen.
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, Strings.PackagesLockFile_RestoreMetadataMissingTfms));
}
}
else
{
invalidReasons.Add(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectReferenceHasNoCompatibleTargetFramework,
p2pProjectName,
restoreMetadataFramework.FrameworkName.GetShortFolderName()
));
}
}
else // This can't happen. When adding the queue, the referenceSpec HAS to be discovered. If the project is otherwise missing, it will be discovered in HasP2PDependencyChanged
{
throw new Exception(string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_UnableToLoadPackagespec,
p2pUniqueName));
}
}
}
}
}
}
bool isLockFileValid = invalidReasons.Count == 0;
return new LockFileValidationResult(isLockFileValid, invalidReasons);
}
/// <summary>Compares two lock files to check if the structure is the same (all values are the same, other
/// than SHA hash), and matches dependencies so the caller can easily compare SHA hashes.</summary>
/// <param name="expected">The expected lock file structure. Usuaully generated from the project.</param>
/// <param name="actual">The lock file that was loaded from the file on disk.</param>
/// <returns>A <see cref="LockFileValidityWithMatchedResults"/>.</returns>
public static LockFileValidityWithMatchedResults IsLockFileStillValid(PackagesLockFile expected, PackagesLockFile actual)
{
if (expected == null)
{
throw new ArgumentNullException(nameof(expected));
}
if (actual == null)
{
throw new ArgumentNullException(nameof(actual));
}
// do quick checks for obvious structure differences
if (expected.Version != actual.Version)
{
return LockFileValidityWithMatchedResults.Invalid;
}
if (expected.Targets.Count != actual.Targets.Count)
{
return LockFileValidityWithMatchedResults.Invalid;
}
foreach (var expectedTarget in expected.Targets)
{
PackagesLockFileTarget actualTarget = null;
for (var i = 0; i < actual.Targets.Count; i++)
{
if (actual.Targets[i].TargetFramework == expectedTarget.TargetFramework)
{
if (actualTarget == null)
{
actualTarget = actual.Targets[i];
}
else
{
// more than 1? possible bug or bad hand edited lock file.
return LockFileValidityWithMatchedResults.Invalid;
}
}
if (actualTarget == null)
{
return LockFileValidityWithMatchedResults.Invalid;
}
if (actualTarget.Dependencies.Count != expectedTarget.Dependencies.Count)
{
return LockFileValidityWithMatchedResults.Invalid;
}
}
}
// no obvious structure difference, so start trying to match individual dependencies
var matchedDependencies = new List<KeyValuePair<LockFileDependency, LockFileDependency>>();
var isLockFileStillValid = true;
var dependencyComparer = LockFileDependencyComparerWithoutContentHash.Default;
foreach (PackagesLockFileTarget expectedTarget in expected.Targets)
{
PackagesLockFileTarget actualTarget = actual.Targets.Single(t => t.TargetFramework == expectedTarget.TargetFramework);
// Duplicate dependencies list so we can remove matches to validate that all dependencies were matched
var actualDependencies = new Dictionary<LockFileDependency, LockFileDependency>(
actualTarget.Dependencies.Count,
dependencyComparer);
foreach (LockFileDependency actualDependency in actualTarget.Dependencies)
{
actualDependencies.Add(actualDependency, actualDependency);
}
foreach (LockFileDependency expectedDependency in expectedTarget.Dependencies)
{
if (actualDependencies.TryGetValue(expectedDependency, out var actualDependency))
{
matchedDependencies.Add(new KeyValuePair<LockFileDependency, LockFileDependency>(expectedDependency, actualDependency));
actualDependencies.Remove(actualDependency);
}
else
{
return LockFileValidityWithMatchedResults.Invalid;
}
}
if (actualDependencies.Count != 0)
{
return LockFileValidityWithMatchedResults.Invalid;
}
}
return new LockFileValidityWithMatchedResults(isLockFileStillValid, matchedDependencies);
}
private static (bool, string) HasDirectPackageDependencyChanged(IEnumerable<LibraryDependency> newDependencies, IEnumerable<LockFileDependency> lockFileDependencies, NuGetFramework nuGetFramework)
{
// If the count is not the same, something has changed.
// Otherwise the N^2 walk below determines whether anything has changed.
var newPackageDependencies = newDependencies.Where(dep => dep.LibraryRange.TypeConstraint == LibraryDependencyTarget.Package);
var newPackageDependenciesCount = newPackageDependencies.Count();
var lockFileDependenciesCount = lockFileDependencies.Count();
if (newPackageDependenciesCount != lockFileDependenciesCount)
{
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_PackageReferencesHaveChanged,
nuGetFramework.GetShortFolderName(),
lockFileDependenciesCount > 0 ? string.Join(", ", lockFileDependencies.Select(e => e.Id + ":" + e.RequestedVersion.ToNormalizedString()).OrderBy(dep => dep)) : Strings.None,
newPackageDependenciesCount > 0 ? string.Join(", ", newPackageDependencies.Select(e => e.LibraryRange.Name + ":" + e.LibraryRange.VersionRange.ToNormalizedString()).OrderBy(dep => dep)) : Strings.None)
);
}
foreach (var dependency in newPackageDependencies)
{
var lockFileDependency = lockFileDependencies.FirstOrDefault(d => StringComparer.OrdinalIgnoreCase.Equals(d.Id, dependency.Name));
if (lockFileDependency == null)
{
// dependency has changed and lock file is out of sync.
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_PackageReferenceAdded,
dependency.Name,
nuGetFramework.GetShortFolderName())
);
}
if (!EqualityUtility.EqualsWithNullCheck(lockFileDependency.RequestedVersion, dependency.LibraryRange.VersionRange))
{
// dependency has changed and lock file is out of sync.
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_PackageReferenceVersionChanged,
dependency.Name,
lockFileDependency.RequestedVersion.ToNormalizedString(),
dependency.LibraryRange.VersionRange.ToNormalizedString())
);
}
}
// no dependency changed. Lock file is still valid.
return (false, string.Empty);
}
private static (bool, string) HasP2PDependencyChanged(IEnumerable<LibraryDependency> newDependencies, IEnumerable<ProjectRestoreReference> projectRestoreReferences, LockFileDependency projectDependency, DependencyGraphSpec dgSpec)
{
// If the count is not the same, something has changed.
// Otherwise we N^2 walk below determines whether anything has changed.
var transitivelyFlowingDependencies = newDependencies.Where(
dep => dep.LibraryRange.TypeConstraint == LibraryDependencyTarget.Package && dep.SuppressParent != LibraryIncludeFlags.All);
var transitivelyFlowingProjectReferences = projectRestoreReferences.Where(e => e.PrivateAssets != LibraryIncludeFlags.All);
var transitiveDependencies = transitivelyFlowingDependencies.Count() + transitivelyFlowingProjectReferences.Count();
if (transitiveDependencies != projectDependency.Dependencies.Count)
{
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectReferencesHasChange,
projectDependency.Id,
transitiveDependencies > 0 ? string.Join(",", transitivelyFlowingDependencies.Select(dep => dep.Name).Concat(projectRestoreReferences.Select(dep => dep.ProjectUniqueName)).OrderBy(dep => dep)) : Strings.None,
projectDependency.Dependencies.Count > 0 ? string.Join(",", projectDependency.Dependencies.Select(dep => dep.Id).OrderBy(dep => dep)) : Strings.None
)
);
}
foreach (var dependency in transitivelyFlowingDependencies)
{
var matchedP2PLibrary = projectDependency.Dependencies.FirstOrDefault(dep => StringComparer.OrdinalIgnoreCase.Equals(dep.Id, dependency.Name));
if (matchedP2PLibrary == null || !EqualityUtility.EqualsWithNullCheck(matchedP2PLibrary.VersionRange, dependency.LibraryRange.VersionRange))
{
// P2P dependency has changed and lock file is out of sync.
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectReferenceDependenciesHasChanged,
projectDependency.Id
)
);
}
}
foreach (var dependency in transitivelyFlowingProjectReferences)
{
var referenceSpec = dgSpec.GetProjectSpec(dependency.ProjectUniqueName);
var matchedP2PLibrary = projectDependency.Dependencies.FirstOrDefault(dep => StringComparer.OrdinalIgnoreCase.Equals(dep.Id, referenceSpec.Name));
if (matchedP2PLibrary == null) // Do not check the version for the projects, or else https://github.com/nuget/home/issues/7935
{
// P2P dependency has changed and lock file is out of sync.
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectReferenceDependenciesHasChanged,
projectDependency.Id
)
);
}
}
// no dependency changed. Lock file is still valid.
return (false, string.Empty);
}
/// <summary>
/// The method will return true if:
/// 1. If a transitive dependency from the lock file is now added to the central file.
/// or
/// 1. If there is a mistmatch between the RequestedVersion of a lock file dependency marked as CentralTransitive and the the version specified in the central package management file.
/// or
/// 2. If a central version that is a transitive dependency is removed from CPVM the lock file is invalidated.
/// </summary>
private static (bool, string) HasProjectTransitiveDependencyChanged(
IReadOnlyDictionary<string, CentralPackageVersion> centralPackageVersions,
IList<LockFileDependency> lockFileCentralTransitiveDependencies,
IList<LockFileDependency> lockTransitiveDependencies)
{
// Transitive dependencies moved to be centraly managed will invalidate the lock file
LockFileDependency dependency = lockTransitiveDependencies.FirstOrDefault(dep => centralPackageVersions.ContainsKey(dep.Id));
if (dependency != null)
{
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectTransitiveDependencyChanged,
dependency.Id
)
);
}
foreach (var lockFileDependencyEnforcedByCPV in lockFileCentralTransitiveDependencies)
{
if (centralPackageVersions.TryGetValue(lockFileDependencyEnforcedByCPV.Id, out var centralPackageVersion))
{
if (centralPackageVersion != null && !EqualityUtility.EqualsWithNullCheck(lockFileDependencyEnforcedByCPV.RequestedVersion, centralPackageVersion.VersionRange))
{
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_ProjectTransitiveDependencyVersionChanged,
lockFileDependencyEnforcedByCPV.RequestedVersion.ToNormalizedString(),
centralPackageVersion.VersionRange.ToNormalizedString()
)
);
}
continue;
}
// The central version was removed
return (true,
string.Format(
CultureInfo.CurrentCulture,
Strings.PackagesLockFile_CentralPackageVersionRemoved,
lockFileDependencyEnforcedByCPV.Id
)
);
}
return (false, string.Empty);
}
/// <summary>
/// A class to return information about lock file validity
/// </summary>
public class LockFileValidityWithMatchedResults
{
/// <summary>
/// True if the lock file had the expected structure (all values expected, other than content hash)
/// </summary>
public bool IsValid { get; }
/// <summary>
/// A list of matched dependencies, so content sha can easily be checked.
/// </summary>
public IReadOnlyList<KeyValuePair<LockFileDependency, LockFileDependency>> MatchedDependencies { get; }
public LockFileValidityWithMatchedResults(bool isValid, IReadOnlyList<KeyValuePair<LockFileDependency, LockFileDependency>> matchedDependencies)
{
IsValid = isValid;
MatchedDependencies = matchedDependencies;
}
public static readonly LockFileValidityWithMatchedResults Invalid =
new LockFileValidityWithMatchedResults(isValid: false, matchedDependencies: null);
}
}
/// <summary>
/// A class to return information about lock file validity with invalid reasons.
/// </summary>
public class LockFileValidationResult
{
/// <summary>
/// True if the packages.lock.json file dependencies match project.assets.json file dependencies
/// </summary>
public bool IsValid { get; }
/// <summary>
/// A list of reasons why lock file is invalid
/// </summary>
public IReadOnlyList<string> InvalidReasons { get; }
public LockFileValidationResult(bool isValid, IReadOnlyList<string> invalidReasons)
{
IsValid = isValid;
InvalidReasons = invalidReasons;
}
}
}