-
Notifications
You must be signed in to change notification settings - Fork 695
/
ResolverUtility.cs
528 lines (460 loc) · 22.1 KB
/
ResolverUtility.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
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
namespace NuGet.DependencyResolver
{
public static class ResolverUtility
{
public static Task<GraphItem<RemoteResolveResult>> FindLibraryCachedAsync(
LibraryRange libraryRange,
NuGetFramework framework,
string runtimeIdentifier,
RemoteWalkContext context,
CancellationToken cancellationToken)
{
LibraryRangeCacheKey key = new(libraryRange, framework);
return context.FindLibraryEntryCache.GetOrAddAsync(key,
static state => FindLibraryEntryAsync(state.LibraryRange, state.framework, state.runtimeIdentifier, state.context, state.cancellationToken),
(key.LibraryRange, framework, runtimeIdentifier, context, cancellationToken),
cancellationToken);
}
public static async Task<GraphItem<RemoteResolveResult>> FindLibraryEntryAsync(
LibraryRange libraryRange,
NuGetFramework framework,
string runtimeIdentifier,
RemoteWalkContext context,
CancellationToken cancellationToken)
{
GraphItem<RemoteResolveResult> graphItem = null;
var currentCacheContext = context.CacheContext;
IList<IRemoteDependencyProvider> remoteDependencyProviders = context.FilterDependencyProvidersForLibrary(libraryRange);
if (libraryRange.TypeConstraintAllows(LibraryDependencyTarget.Package))
{
LogIfPackageSourceMappingIsEnabled(libraryRange.Name, context, remoteDependencyProviders);
}
// Try up to two times to get the package. The second
// retry will refresh the cache if a package is listed
// but fails to download. This can happen if the feed prunes
// the package.
for (var i = 0; i < 2 && graphItem == null; i++)
{
var match = await FindLibraryMatchAsync(
libraryRange,
framework,
runtimeIdentifier,
remoteDependencyProviders,
context.LocalLibraryProviders,
context.ProjectLibraryProviders,
context.LockFileLibraries,
currentCacheContext,
context.Logger,
cancellationToken);
if (match == null)
{
return CreateUnresolvedResult(libraryRange);
}
else
{
try
{
graphItem = await CreateGraphItemAsync(match, framework, currentCacheContext, context.Logger, cancellationToken);
}
catch (InvalidCacheProtocolException) when (i == 0)
{
// 1st failure, invalidate the cache and try again.
// Clear the on disk and memory caches during the next request.
currentCacheContext = currentCacheContext.WithRefreshCacheTrue();
}
catch (PackageNotFoundProtocolException ex) when (match.Provider.IsHttp && match.Provider.Source != null)
{
// 2nd failure, the feed is likely corrupt or removing packages too fast to keep up with.
var message = string.Format(CultureInfo.CurrentCulture,
Strings.Error_PackageNotFoundWhenExpected,
match.Provider.Source,
ex.PackageIdentity.ToString());
throw new FatalProtocolException(message, ex);
}
}
}
return graphItem;
}
private static async Task<GraphItem<RemoteResolveResult>> CreateGraphItemAsync(
RemoteMatch match,
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken)
{
LibraryDependencyInfo dependencies;
// For local matches such as projects get the dependencies from the LocalLibrary property.
var localMatch = match as LocalMatch;
if (localMatch != null)
{
dependencies = LibraryDependencyInfo.Create(
localMatch.LocalLibrary.Identity,
framework,
localMatch.LocalLibrary.Dependencies);
}
else
{
// Look up the dependencies from the source, this will download the package if needed.
dependencies = await match.Provider.GetDependenciesAsync(
match.Library,
framework,
cacheContext,
logger,
cancellationToken);
}
// Copy the original identity to the remote match.
// This ensures that the correct casing is used for
// the id/version.
match.Library = dependencies.Library;
return new GraphItem<RemoteResolveResult>(match.Library)
{
Data = new RemoteResolveResult
{
Match = match,
Dependencies = dependencies.Dependencies.ToList()
},
};
}
public static async Task<RemoteMatch> FindLibraryMatchAsync(
LibraryRange libraryRange,
NuGetFramework framework,
string runtimeIdentifier,
IEnumerable<IRemoteDependencyProvider> remoteProviders,
IEnumerable<IRemoteDependencyProvider> localProviders,
IEnumerable<IDependencyProvider> projectProviders,
IDictionary<LockFileCacheKey, IList<LibraryIdentity>> lockFileLibraries,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken)
{
var projectMatch = await FindProjectMatchAsync(libraryRange, framework, projectProviders, cancellationToken);
if (projectMatch != null)
{
return projectMatch;
}
if (libraryRange.VersionRange == null)
{
return null;
}
// The resolution below is only for package types
if (!libraryRange.TypeConstraintAllows(LibraryDependencyTarget.Package))
{
return null;
}
var targetFramework = framework;
if (framework is AssetTargetFallbackFramework)
{
targetFramework = (framework as AssetTargetFallbackFramework).RootFramework;
}
var key = new LockFileCacheKey(targetFramework, runtimeIdentifier);
// This is only applicable when packages has to be resolved from packages.lock.json file
if (lockFileLibraries.TryGetValue(key, out var libraries))
{
var library = libraries.FirstOrDefault(lib => StringComparer.OrdinalIgnoreCase.Equals(lib.Name, libraryRange.Name));
if (library != null)
{
// check for the exact library through local repositories
var localMatch = await FindLibraryByVersionAsync(library, framework, localProviders, cacheContext, logger, cancellationToken);
if (localMatch != null
&& localMatch.Library.Version.Equals(library.Version))
{
return localMatch;
}
// if not found in local repositories, then check the remote repositories
var remoteMatch = await FindLibraryByVersionAsync(library, framework, remoteProviders, cacheContext, logger, cancellationToken);
if (remoteMatch != null
&& remoteMatch.Library.Version.Equals(library.Version))
{
// Only return the result if the version is same as defined in packages.lock.json file
return remoteMatch;
}
}
// it should never come to this, but as a fail-safe if it ever fails to resolve a package from lock file when
// it has to... then fail restore.
return null;
}
return await FindPackageLibraryMatchAsync(libraryRange, framework, remoteProviders, localProviders, cacheContext, logger, cancellationToken);
}
/// <summary>
/// Resolves the library from the given sources. Note that it does not download the package.
/// </summary>
/// <param name="cache">Cache of requests per library</param>
/// <param name="libraryRange">The library requested</param>
/// <param name="remoteWalkContext">remote Providers (all sources, including file sources)</param>
/// <param name="cancellationToken">cancellation token</param>
/// <returns>The requested range and remote match.</returns>
public static Task<Tuple<LibraryRange, RemoteMatch>> FindPackageLibraryMatchCachedAsync(
LibraryRange libraryRange,
RemoteWalkContext remoteWalkContext,
CancellationToken cancellationToken)
{
return remoteWalkContext.ResolvePackageLibraryMatchCache.GetOrAddAsync(libraryRange,
static state => ResolvePackageLibraryMatchAsync(state.libraryRange, state.remoteWalkContext, state.cancellationToken),
(libraryRange, remoteWalkContext, cancellationToken),
cancellationToken);
}
private static async Task<Tuple<LibraryRange, RemoteMatch>> ResolvePackageLibraryMatchAsync(LibraryRange libraryRange, RemoteWalkContext remoteWalkContext, CancellationToken cancellationToken)
{
IList<IRemoteDependencyProvider> remoteDependencyProviders = remoteWalkContext.FilterDependencyProvidersForLibrary(libraryRange);
if (libraryRange.TypeConstraintAllows(LibraryDependencyTarget.Package))
{
LogIfPackageSourceMappingIsEnabled(libraryRange.Name, remoteWalkContext, remoteDependencyProviders);
}
var match = await FindPackageLibraryMatchAsync(libraryRange, NuGetFramework.AnyFramework, remoteDependencyProviders, remoteWalkContext.LocalLibraryProviders, remoteWalkContext.CacheContext, remoteWalkContext.Logger, cancellationToken);
if (match == null)
{
match = CreateUnresolvedMatch(libraryRange);
}
return new Tuple<LibraryRange, RemoteMatch>(libraryRange, match);
}
private static async Task<RemoteMatch> FindPackageLibraryMatchAsync(LibraryRange libraryRange, NuGetFramework framework, IEnumerable<IRemoteDependencyProvider> remoteProviders, IEnumerable<IRemoteDependencyProvider> localProviders, SourceCacheContext cacheContext, ILogger logger, CancellationToken cancellationToken)
{
// The resolution below is only for package types
if (!libraryRange.TypeConstraintAllows(LibraryDependencyTarget.Package))
{
return null;
}
if (libraryRange.VersionRange.IsFloating)
{
// For snapshot dependencies, get the version remotely first.
var remoteMatch = await FindLibraryByVersionAsync(libraryRange, framework, remoteProviders, cacheContext, logger, cancellationToken);
if (remoteMatch != null)
{
// Try to see if the specific version found on the remote exists locally. This avoids any unnecessary
// remote access incase we already have it in the cache/local packages folder.
var localMatch = await FindLibraryByVersionAsync(remoteMatch.Library, framework, localProviders, cacheContext, logger, cancellationToken);
if (localMatch != null
&& localMatch.Library.Version.Equals(remoteMatch.Library.Version))
{
// If we have a local match, and it matches the version *exactly* then use it.
return localMatch;
}
// We found something locally, but it wasn't an exact match
// for the resolved remote match.
}
return remoteMatch;
}
else
{
// Check for the specific version locally.
var localMatch = await FindLibraryByVersionAsync(libraryRange, framework, localProviders, cacheContext, logger, cancellationToken);
if (localMatch != null
&& localMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
{
// We have an exact match so use it.
return localMatch;
}
// Either we found a local match but it wasn't the exact version, or
// we didn't find a local match.
var remoteMatch = await FindLibraryByVersionAsync(libraryRange, framework, remoteProviders, cacheContext, logger, cancellationToken);
if (remoteMatch != null
&& localMatch == null)
{
// There wasn't any local match for the specified version but there was a remote match.
// See if that version exists locally.
localMatch = await FindLibraryByVersionAsync(remoteMatch.Library, framework, remoteProviders, cacheContext, logger, cancellationToken);
}
if (localMatch != null
&& remoteMatch != null)
{
// We found a match locally and remotely, so pick the better version
// in relation to the specified version.
if (libraryRange.VersionRange.IsBetter(
current: localMatch.Library.Version,
considering: remoteMatch.Library.Version))
{
return remoteMatch;
}
else
{
return localMatch;
}
}
// Prefer local over remote generally.
return localMatch ?? remoteMatch;
}
}
public static Task<RemoteMatch> FindProjectMatchAsync(
LibraryRange libraryRange,
NuGetFramework framework,
IEnumerable<IDependencyProvider> projectProviders,
CancellationToken cancellationToken)
{
RemoteMatch result = null;
// Check if projects are allowed for this dependency
if (libraryRange.TypeConstraintAllowsAnyOf(
(LibraryDependencyTarget.Project | LibraryDependencyTarget.ExternalProject)))
{
foreach (var provider in projectProviders)
{
if (provider.SupportsType(libraryRange.TypeConstraint))
{
var match = provider.GetLibrary(libraryRange, framework);
if (match != null)
{
result = new LocalMatch
{
LocalLibrary = match,
Library = match.Identity,
LocalProvider = provider,
Provider = new LocalDependencyProvider(provider),
Path = match.Path,
};
}
}
}
}
return Task.FromResult<RemoteMatch>(result);
}
public static async Task<RemoteMatch> FindLibraryByVersionAsync(
LibraryRange libraryRange,
NuGetFramework framework,
IEnumerable<IRemoteDependencyProvider> providers,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken token)
{
if (libraryRange.VersionRange.IsFloating)
{
// Don't optimize the non http path for floating versions or we'll miss things
return await FindLibraryFromSourcesAsync(
libraryRange,
providers,
framework,
cacheContext,
logger,
token);
}
// Try the non http sources first
var nonHttpMatch = await FindLibraryFromSourcesAsync(
libraryRange,
providers.Where(p => !p.IsHttp),
framework,
cacheContext,
logger,
token);
// If we found an exact match then use it
if (nonHttpMatch != null && nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
{
return nonHttpMatch;
}
// Otherwise try the http sources
var httpMatch = await FindLibraryFromSourcesAsync(
libraryRange,
providers.Where(p => p.IsHttp),
framework,
cacheContext,
logger,
token);
// Pick the best match of the 2
if (libraryRange.VersionRange.IsBetter(
nonHttpMatch?.Library?.Version,
httpMatch?.Library.Version))
{
return httpMatch;
}
return nonHttpMatch;
}
private static async Task<RemoteMatch> FindLibraryFromSourcesAsync(
LibraryRange libraryRange,
IEnumerable<IRemoteDependencyProvider> providers,
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken token)
{
var tasks = new List<Task<RemoteMatch>>();
foreach (var provider in providers)
{
tasks.Add(FindLibraryFromProviderAsync(provider, libraryRange, framework, cacheContext, logger, token));
}
RemoteMatch bestMatch = null;
while (tasks.Count > 0)
{
var task = await Task.WhenAny(tasks);
tasks.Remove(task);
var match = await task;
// If we found an exact match then use it.
// This allows us to shortcircuit slow feeds even if there's an exact match
if (!libraryRange.VersionRange.IsFloating &&
match?.Library?.Version != null &&
libraryRange.VersionRange.IsMinInclusive &&
match.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
{
return match;
}
// Otherwise just find the best out of the matches
if (libraryRange.VersionRange.IsBetter(
current: bestMatch?.Library?.Version,
considering: match?.Library?.Version))
{
bestMatch = match;
}
}
static async Task<RemoteMatch> FindLibraryFromProviderAsync(IRemoteDependencyProvider provider, LibraryRange libraryRange,
NuGetFramework framework, SourceCacheContext cacheContext, ILogger logger, CancellationToken token)
{
var library = await provider.FindLibraryAsync(libraryRange, framework, cacheContext, logger, token);
if (library != null)
{
return new RemoteMatch
{
Provider = provider,
Library = library
};
}
return null;
}
return bestMatch;
}
private static GraphItem<RemoteResolveResult> CreateUnresolvedResult(LibraryRange libraryRange)
{
var match = CreateUnresolvedMatch(libraryRange);
return new GraphItem<RemoteResolveResult>(match.Library)
{
Data = new RemoteResolveResult()
{
Match = match,
Dependencies = RemoteResolveResult.EmptyDependencies
}
};
}
private static RemoteMatch CreateUnresolvedMatch(LibraryRange libraryRange)
{
return new RemoteMatch()
{
Library = new LibraryIdentity()
{
Name = libraryRange.Name,
Type = LibraryType.Unresolved,
Version = libraryRange.VersionRange?.MinVersion
},
Path = null,
Provider = null
};
}
private static void LogIfPackageSourceMappingIsEnabled(string packageName, RemoteWalkContext context, IList<IRemoteDependencyProvider> remoteDependencyProviders)
{
if (context.PackageSourceMapping?.IsEnabled == true)
{
if (remoteDependencyProviders.Count == 0)
context.Logger.LogDebug(string.Format(CultureInfo.CurrentCulture, Strings.Log_NoMatchingSourceFoundForPackage, packageName));
else
context.Logger.LogDebug(string.Format(CultureInfo.CurrentCulture, Strings.Log_MatchingSourceFoundForPackage, packageName, string.Join(",", remoteDependencyProviders.Select(provider => provider.Source.Name))));
}
}
}
}