-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ReferenceTable.cs
3230 lines (2852 loc) · 158 KB
/
ReferenceTable.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Build.Eventing;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks.AssemblyDependency;
using Microsoft.Build.Utilities;
using FrameworkNameVersioning = System.Runtime.Versioning.FrameworkName;
using SystemProcessorArchitecture = System.Reflection.ProcessorArchitecture;
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// A table of references.
/// </summary>
internal sealed class ReferenceTable
{
/// <summary>version 4.0</summary>
private static readonly Version s_targetFrameworkVersion_40 = new Version("4.0");
/// <summary>
/// A mapping of a framework identifier to the most current redist list on the system based on the target framework identifier on the moniker.
/// This is used to determine if an assembly is in a redist list for the framework targeted by the moniker.
/// </summary>
private static readonly Dictionary<string, Tuple<RedistList, string>> s_monikerToHighestRedistList = new Dictionary<string, Tuple<RedistList, string>>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Reference simple names that were resolved by an external entity to RAR.
/// </summary>
private readonly HashSet<string> _externallyResolvedPrimaryReferences = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
/// <summary>The table of remapped assemblies. Used for Unification.</summary>
private IEnumerable<DependentAssembly> _remappedAssemblies = Enumerable.Empty<DependentAssembly>();
/// <summary>If true, then search for dependencies.</summary>
private readonly bool _findDependencies;
/// <summary>
/// Should version be ignored for framework primary references
/// </summary>
private readonly bool _ignoreVersionForFrameworkReferences;
/// <summary>If true, then search for satellite files.</summary>
private readonly bool _findSatellites;
/// <summary>If true, then search for serialization assembly files.</summary>
private readonly bool _findSerializationAssemblies;
/// <summary>If true, then search for related files.</summary>
private readonly bool _findRelatedFiles;
/// <summary>
/// If true, then force framework assembly version check against the target framework version
/// If false, the default behavior is to disable version checks for target framework versions 4.5 and above.
/// </summary>
private readonly bool _checkAssemblyVersionAgainstTargetFrameworkVersion;
/// <summary>Path to the FX.</summary>
private readonly string[] _frameworkPaths;
/// <summary>The allowed assembly extensions.</summary>
private readonly string[] _allowedAssemblyExtensions;
/// <summary>These are companion files that typically travel with assemblies</summary>
private readonly string[] _relatedFileExtensions;
/// <summary>
/// Locations where sdks are installed. K:SDKName v: Resolved Reference item
/// </summary>
private readonly Dictionary<string, ITaskItem> _resolvedSDKReferences;
/// <summary>Path to installed assembly XML tables.</summary>
private readonly InstalledAssemblies _installedAssemblies;
/// <summary>Like x86 or IA64\AMD64, the processor architecture being targetted.</summary>
private readonly SystemProcessorArchitecture _targetProcessorArchitecture;
/// <summary>Delegate used for checking for the existence of a file.</summary>
private readonly FileExists _fileExists;
/// <summary>Delegate used for checking for the existence of a directory.</summary>
private readonly DirectoryExists _directoryExists;
/// <summary>Delegate used for getting directories.</summary>
private readonly GetDirectories _getDirectories;
/// <summary>Delegate used for getting assembly names.</summary>
private readonly GetAssemblyName _getAssemblyName;
/// <summary>Delegate used for finding dependencies of a file.</summary>
private readonly GetAssemblyMetadata _getAssemblyMetadata;
/// <summary>Delegate used to get the image runtime version of a file</summary>
private readonly GetAssemblyRuntimeVersion _getRuntimeVersion;
#if FEATURE_WIN32_REGISTRY
/// <summary> Delegate to get the base registry key for AssemblyFoldersEx</summary>
private OpenBaseKey _openBaseKey;
#endif
/// <summary>Version of the runtime we are targeting</summary>
private readonly Version _targetedRuntimeVersion;
/// <summary>
/// Delegate used to get the machineType from the PE header of the dll.
/// </summary>
private readonly ReadMachineTypeFromPEHeader _readMachineTypeFromPEHeader;
/// <summary>
/// Is the file a winMD file
/// </summary>
private readonly IsWinMDFile _isWinMDFile;
/// <summary>Version of the framework targeted by this project.</summary>
private readonly Version _projectTargetFramework;
/// <summary>
/// Target framework moniker we are targeting.
/// </summary>
private readonly FrameworkNameVersioning _targetFrameworkMoniker;
/// <summary>
/// Logging helper to allow the logging of meessages from the Reference Table.
/// </summary>
private readonly TaskLoggingHelper _log;
/// <summary>
/// List of framework directories which are the highest on the machine
/// </summary>
private readonly string[] _latestTargetFrameworkDirectories;
/// <summary>
/// Should dependencies be set to copy local if the parent reference is in the GAC
/// </summary>
private readonly bool _copyLocalDependenciesWhenParentReferenceInGac;
private readonly bool _doNotCopyLocalIfInGac;
/// <summary>
/// Shoould the framework attribute version mismatch be ignored.
/// </summary>
private readonly bool _ignoreFrameworkAttributeVersionMismatch;
/// <summary>
/// Delegate to determine if an assembly name is in the GAC.
/// </summary>
private readonly GetAssemblyPathInGac _getAssemblyPathInGac;
/// <summary>
/// Should a warning or error be emitted on architecture mismatch
/// </summary>
private readonly WarnOrErrorOnTargetArchitectureMismatchBehavior _warnOrErrorOnTargetArchitectureMismatch = WarnOrErrorOnTargetArchitectureMismatchBehavior.Warning;
private readonly ConcurrentDictionary<string, AssemblyMetadata> _assemblyMetadataCache;
/// <summary>
/// When we exclude an assembly from resolution because it is part of out exclusion list we need to let the user know why this is.
/// There can be a number of reasons each for un-resolving a reference, these reasons are encapsulated by a different black list. We need to log a specific message
/// depending on which black list we have found the offending assembly in. This delegate allows one to tie a set of logging messages to a black list so that when we
/// discover an assembly in the black list we can log the correct message.
/// </summary>
internal delegate void LogExclusionReason(bool displayPrimaryReferenceMessage, AssemblyNameExtension assemblyName, Reference reference, ITaskItem referenceItem, string targetedFramework);
// Offset to the PE header
private const int PEOFFSET = 0x3c;
// PEHeader
private const int PEHEADER = 0x00004550;
#if FEATURE_WIN32_REGISTRY
/// <summary>
/// Construct.
/// </summary>
/// <param name="buildEngine"></param>
/// <param name="findDependencies">If true, then search for dependencies.</param>
/// <param name="findSatellites">If true, then search for satellite files.</param>
/// <param name="findSerializationAssemblies">If true, then search for serialization assembly files.</param>
/// <param name="findRelatedFiles">If true, then search for related files.</param>
/// <param name="searchPaths">Paths to search for dependent assemblies on.</param>
/// <param name="relatedFileExtensions"></param>
/// <param name="candidateAssemblyFiles">List of literal assembly file names to be considered when SearchPaths has {CandidateAssemblyFiles}.</param>
/// <param name="resolvedSDKItems">Resolved sdk items</param>
/// <param name="frameworkPaths">Path to the FX.</param>
/// <param name="installedAssemblies">Installed assembly XML tables.</param>
/// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64, the processor architecture being targetted.</param>
/// <param name="fileExists">Delegate used for checking for the existence of a file.</param>
/// <param name="directoryExists">Delegate used for files.</param>
/// <param name="getDirectories">Delegate used for getting directories.</param>
/// <param name="getAssemblyName">Delegate used for getting assembly names.</param>
/// <param name="getAssemblyMetadata">Delegate used for finding dependencies of a file.</param>
/// <param name="getRegistrySubKeyNames">Used to get registry subkey names.</param>
/// <param name="getRegistrySubKeyDefaultValue">Used to get registry default values.</param>
/// <param name="openBaseKey"></param>
/// <param name="unresolveFrameworkAssembliesFromHigherFrameworks"></param>
/// <param name="assemblyMetadataCache">Cache of metadata already read from paths.</param>
/// <param name="allowedAssemblyExtensions"></param>
/// <param name="getRuntimeVersion"></param>
/// <param name="targetedRuntimeVersion">Version of the runtime to target.</param>
/// <param name="projectTargetFramework">Version of the framework targeted by the project.</param>
/// <param name="targetFrameworkMoniker">Target framework moniker we are targeting.</param>
/// <param name="log">Logging helper to allow the logging of meessages from the Reference Table.</param>
/// <param name="latestTargetFrameworkDirectories"></param>
/// <param name="copyLocalDependenciesWhenParentReferenceInGac"></param>
/// <param name="doNotCopyLocalIfInGac"></param>
/// <param name="getAssemblyPathInGac"></param>
/// <param name="isWinMDFile"></param>
/// <param name="ignoreVersionForFrameworkReferences"></param>
/// <param name="readMachineTypeFromPEHeader"></param>
/// <param name="warnOrErrorOnTargetArchitectureMismatch"></param>
/// <param name="ignoreFrameworkAttributeVersionMismatch"></param>
#else
/// <summary>
/// Construct.
/// </summary>
/// <param name="buildEngine"></param>
/// <param name="findDependencies">If true, then search for dependencies.</param>
/// <param name="findSatellites">If true, then search for satellite files.</param>
/// <param name="findSerializationAssemblies">If true, then search for serialization assembly files.</param>
/// <param name="findRelatedFiles">If true, then search for related files.</param>
/// <param name="searchPaths">Paths to search for dependent assemblies on.</param>
/// <param name="relatedFileExtensions"></param>
/// <param name="candidateAssemblyFiles">List of literal assembly file names to be considered when SearchPaths has {CandidateAssemblyFiles}.</param>
/// <param name="resolvedSDKItems">Resolved sdk items</param>
/// <param name="frameworkPaths">Path to the FX.</param>
/// <param name="installedAssemblies">Installed assembly XML tables.</param>
/// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64, the processor architecture being targetted.</param>
/// <param name="fileExists">Delegate used for checking for the existence of a file.</param>
/// <param name="directoryExists">Delegate used for files.</param>
/// <param name="getDirectories">Delegate used for getting directories.</param>
/// <param name="getAssemblyName">Delegate used for getting assembly names.</param>
/// <param name="getAssemblyMetadata">Delegate used for finding dependencies of a file.</param>
/// <param name="unresolveFrameworkAssembliesFromHigherFrameworks"></param>
/// <param name="assemblyMetadataCache">Cache of metadata already read from paths.</param>
/// <param name="allowedAssemblyExtensions"></param>
/// <param name="getRuntimeVersion"></param>
/// <param name="targetedRuntimeVersion">Version of the runtime to target.</param>
/// <param name="projectTargetFramework">Version of the framework targeted by the project.</param>
/// <param name="targetFrameworkMoniker">Target framework moniker we are targeting.</param>
/// <param name="log">Logging helper to allow the logging of meessages from the Reference Table.</param>
/// <param name="latestTargetFrameworkDirectories"></param>
/// <param name="copyLocalDependenciesWhenParentReferenceInGac"></param>
/// <param name="doNotCopyLocalIfInGac"></param>
/// <param name="getAssemblyPathInGac"></param>
/// <param name="isWinMDFile"></param>
/// <param name="ignoreVersionForFrameworkReferences"></param>
/// <param name="readMachineTypeFromPEHeader"></param>
/// <param name="warnOrErrorOnTargetArchitectureMismatch"></param>
/// <param name="ignoreFrameworkAttributeVersionMismatch"></param>
#endif
internal ReferenceTable
(
IBuildEngine buildEngine,
bool findDependencies,
bool findSatellites,
bool findSerializationAssemblies,
bool findRelatedFiles,
string[] searchPaths,
string[] allowedAssemblyExtensions,
string[] relatedFileExtensions,
string[] candidateAssemblyFiles,
ITaskItem[] resolvedSDKItems,
string[] frameworkPaths,
InstalledAssemblies installedAssemblies,
System.Reflection.ProcessorArchitecture targetProcessorArchitecture,
FileExists fileExists,
DirectoryExists directoryExists,
GetDirectories getDirectories,
GetAssemblyName getAssemblyName,
GetAssemblyMetadata getAssemblyMetadata,
#if FEATURE_WIN32_REGISTRY
GetRegistrySubKeyNames getRegistrySubKeyNames,
GetRegistrySubKeyDefaultValue getRegistrySubKeyDefaultValue,
OpenBaseKey openBaseKey,
#endif
GetAssemblyRuntimeVersion getRuntimeVersion,
Version targetedRuntimeVersion,
Version projectTargetFramework,
FrameworkNameVersioning targetFrameworkMoniker,
TaskLoggingHelper log,
string[] latestTargetFrameworkDirectories,
bool copyLocalDependenciesWhenParentReferenceInGac,
bool doNotCopyLocalIfInGac,
GetAssemblyPathInGac getAssemblyPathInGac,
IsWinMDFile isWinMDFile,
bool ignoreVersionForFrameworkReferences,
ReadMachineTypeFromPEHeader readMachineTypeFromPEHeader,
WarnOrErrorOnTargetArchitectureMismatchBehavior warnOrErrorOnTargetArchitectureMismatch,
bool ignoreFrameworkAttributeVersionMismatch,
bool unresolveFrameworkAssembliesFromHigherFrameworks,
ConcurrentDictionary<string, AssemblyMetadata> assemblyMetadataCache)
{
_log = log;
_findDependencies = findDependencies;
_findSatellites = findSatellites;
_findSerializationAssemblies = findSerializationAssemblies;
_findRelatedFiles = findRelatedFiles;
_frameworkPaths = frameworkPaths;
_allowedAssemblyExtensions = allowedAssemblyExtensions;
_relatedFileExtensions = relatedFileExtensions;
_installedAssemblies = installedAssemblies;
_targetProcessorArchitecture = targetProcessorArchitecture;
_fileExists = fileExists;
_directoryExists = directoryExists;
_getDirectories = getDirectories;
_getAssemblyName = getAssemblyName;
_getAssemblyMetadata = getAssemblyMetadata;
_getRuntimeVersion = getRuntimeVersion;
_projectTargetFramework = projectTargetFramework;
_targetedRuntimeVersion = targetedRuntimeVersion;
#if FEATURE_WIN32_REGISTRY
_openBaseKey = openBaseKey;
#endif
_targetFrameworkMoniker = targetFrameworkMoniker;
_latestTargetFrameworkDirectories = latestTargetFrameworkDirectories;
_copyLocalDependenciesWhenParentReferenceInGac = copyLocalDependenciesWhenParentReferenceInGac;
_doNotCopyLocalIfInGac = doNotCopyLocalIfInGac;
_getAssemblyPathInGac = getAssemblyPathInGac;
_isWinMDFile = isWinMDFile;
_readMachineTypeFromPEHeader = readMachineTypeFromPEHeader;
_warnOrErrorOnTargetArchitectureMismatch = warnOrErrorOnTargetArchitectureMismatch;
_ignoreFrameworkAttributeVersionMismatch = ignoreFrameworkAttributeVersionMismatch;
_assemblyMetadataCache = assemblyMetadataCache;
// Set condition for when to check assembly version against the target framework version
_checkAssemblyVersionAgainstTargetFrameworkVersion = unresolveFrameworkAssembliesFromHigherFrameworks || ((_projectTargetFramework ?? ReferenceTable.s_targetFrameworkVersion_40) <= ReferenceTable.s_targetFrameworkVersion_40);
// Convert the list of installed SDK's to a dictionary for faster lookup
_resolvedSDKReferences = new Dictionary<string, ITaskItem>(StringComparer.OrdinalIgnoreCase);
_ignoreVersionForFrameworkReferences = ignoreVersionForFrameworkReferences;
if (resolvedSDKItems != null)
{
foreach (ITaskItem resolvedSDK in resolvedSDKItems)
{
string sdkName = resolvedSDK.GetMetadata("SDKName");
if (sdkName.Length > 0)
{
_resolvedSDKReferences[sdkName] = resolvedSDK;
}
}
}
// Compile searchpaths into fast resolver array.
Resolvers = AssemblyResolution.CompileSearchPaths
(
buildEngine,
searchPaths,
candidateAssemblyFiles,
targetProcessorArchitecture,
frameworkPaths,
fileExists,
getAssemblyName,
#if FEATURE_WIN32_REGISTRY
getRegistrySubKeyNames,
getRegistrySubKeyDefaultValue,
openBaseKey,
#endif
installedAssemblies,
getRuntimeVersion,
targetedRuntimeVersion,
getAssemblyPathInGac,
log
);
}
/// <summary>
/// Set of resolvers the reference table uses.
/// </summary>
internal Resolver[] Resolvers { get; }
/// <summary>
/// Get a table of all vertices.
/// </summary>
internal Dictionary<AssemblyNameExtension, Reference> References { get; private set; } = new Dictionary<AssemblyNameExtension, Reference>(AssemblyNameComparer.GenericComparer);
/// <summary>
/// If assemblies have been marked for exclusion this contains the list of their full names
/// This may be null
/// </summary>
internal List<string> ListOfExcludedAssemblies { get; private set; }
/// <summary>
/// Indicates that at least one reference was <see cref="Reference.ExternallyResolved"/> and
/// we skipped finding its dependencies as a result.
/// </summary>
/// <remarks>
/// This is currently used to perform a shallow search for System.Runtime/netstandard usage
/// within the externally resolved graph.
/// </remarks>
internal bool SkippedFindingExternallyResolvedDependencies { get; private set; }
/// <summary>
/// Force dependencies to be walked even when a reference is marked with ExternallyResolved=true
/// metadata.
/// </summary>
/// <remarks>
/// This is currently used to ensure that we suggest appropriate binding redirects for
/// assembly version conflicts within an externally resolved graph.
/// </remarks>
internal bool FindDependenciesOfExternallyResolvedReferences { get; set; }
/// <summary>
/// Adds a reference to the table.
/// </summary>
/// <param name="assemblyName">The assembly name to be used as a key.</param>
/// <param name="reference">The reference to add.</param>
internal void AddReference(AssemblyNameExtension assemblyName, Reference reference)
{
ErrorUtilities.VerifyThrow(assemblyName.Name != null, "Got an empty assembly name.");
if (References.TryGetValue(assemblyName, out Reference referenceGoingToBeReplaced))
{
foreach (AssemblyRemapping pair in referenceGoingToBeReplaced.RemappedAssemblyNames())
{
reference.AddRemapping(pair.From, pair.To);
}
}
if (reference.FullPath.Length > 0)
{
// Saves effort and makes deduplication possible downstream
reference.NormalizeFullPath();
}
References[assemblyName] = reference;
}
/// <summary>
/// Find the reference that corresponds to the given path.
/// </summary>
/// <param name="assemblyName">The assembly name to find the reference for.</param>
/// <returns>'null' if no reference existed.</returns>
internal Reference GetReference(AssemblyNameExtension assemblyName)
{
ErrorUtilities.VerifyThrow(assemblyName.Name != null, "Got an empty assembly name.");
References.TryGetValue(assemblyName, out Reference referenceToReturn);
return referenceToReturn;
}
/// <summary>
/// Give an assembly file name, adjust a Reference to match it.
/// </summary>
/// <param name="reference">The reference to work on</param>
/// <param name="assemblyFileName">The path to the assembly file.</param>
/// <returns>The AssemblyName of assemblyFileName</returns>
private AssemblyNameExtension NameAssemblyFileReference
(
Reference reference,
string assemblyFileName
)
{
AssemblyNameExtension assemblyName = null;
if (!Path.IsPathRooted(assemblyFileName))
{
reference.FullPath = Path.GetFullPath(assemblyFileName);
}
else
{
reference.FullPath = assemblyFileName;
}
try
{
if (_fileExists(assemblyFileName))
{
assemblyName = _getAssemblyName(assemblyFileName);
if (assemblyName != null)
{
reference.ResolvedSearchPath = assemblyFileName;
}
}
else if (_directoryExists(assemblyFileName))
{
assemblyName = new AssemblyNameExtension("*directory*");
reference.AddError
(
new ReferenceResolutionException
(
ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("General.ExpectedFileGotDirectory", reference.FullPath),
null
)
);
reference.FullPath = String.Empty;
}
if (assemblyName == null)
{
reference.AddError
(
new DependencyResolutionException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("General.ExpectedFileMissing", reference.FullPath), null)
);
}
}
catch (BadImageFormatException e)
{
reference.AddError(new DependencyResolutionException(e.Message, e));
}
catch (UnauthorizedAccessException e)
{
// If this isn't a valid assembly, then record the exception and continue on
reference.AddError(new DependencyResolutionException(e.Message, e));
}
// If couldn't resolve the assemly name then just use the simple name extracted from
// the file name.
if (assemblyName == null)
{
string simpleName = Path.GetFileNameWithoutExtension(assemblyFileName);
assemblyName = new AssemblyNameExtension(simpleName);
}
return assemblyName;
}
/// <summary>
/// Given a list of task items, add them all to this table and make them the only primary items.
/// </summary>
/// <param name="referenceAssemblyFiles">The task items which contain file names to add.</param>
/// <param name="referenceAssemblyNames">The task items which contain fusion names to add.</param>
/// <param name="exceptions">Exceptions encountered while setting primary items. Exceptions are logged, but it doesn't stop the resolution process.</param>
private void SetPrimaryItems
(
ITaskItem[] referenceAssemblyFiles,
ITaskItem[] referenceAssemblyNames,
List<Exception> exceptions
)
{
// Loop over the referenceAssemblyFiles provided and add each one that doesn't exist.
// Set the primary flag to 'true'.
if (referenceAssemblyFiles != null)
{
foreach (ITaskItem i in referenceAssemblyFiles)
{
SetPrimaryFileItem(i);
}
}
// Loop over the referenceAssemblyNames provided and add each one that doesn't exist.
// Set the primary flag to 'true'.
if (referenceAssemblyNames != null)
{
foreach (ITaskItem n in referenceAssemblyNames)
{
Exception e = SetPrimaryAssemblyReferenceItem(n);
if (e != null)
{
exceptions.Add(e);
}
}
}
}
/// <summary>
/// Given an item that refers to a assembly name, make it a primary reference.
/// </summary>
/// <param name="referenceAssemblyName">The task item which contain fusion names to add.</param>
/// <returns>Resulting exception containing resolution failure details, if any: too costly to throw it.</returns>
private Exception SetPrimaryAssemblyReferenceItem
(
ITaskItem referenceAssemblyName
)
{
// Get the desired executable extension.
string executableExtension = referenceAssemblyName.GetMetadata(ItemMetadataNames.executableExtension);
// Get the assembly name, if possible.
string itemSpec = referenceAssemblyName.ItemSpec;
string rawFileNameCandidate = itemSpec;
AssemblyNameExtension assemblyName = null;
string fusionName = referenceAssemblyName.GetMetadata(ItemMetadataNames.fusionName);
bool result = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyName, ItemMetadataNames.IgnoreVersionForFrameworkReference, out bool metadataFound);
bool ignoreVersionForFrameworkReference;
if (metadataFound)
{
ignoreVersionForFrameworkReference = result;
}
else
{
ignoreVersionForFrameworkReference = _ignoreVersionForFrameworkReferences;
}
TryConvertToAssemblyName(itemSpec, fusionName, ref assemblyName);
// Figure out the specific version value.
bool wantSpecificVersion = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyName, ItemMetadataNames.specificVersion, out bool foundSpecificVersionMetadata);
bool isSimpleName = (assemblyName?.IsSimpleName == true);
// Create the reference.
var reference = new Reference(_isWinMDFile, _fileExists, _getRuntimeVersion);
reference.MakePrimaryAssemblyReference(referenceAssemblyName, wantSpecificVersion, executableExtension);
// Escape simple names.
// 1) If the itemSpec for the task is already a simple name
// 2) We have found the metadata and it is specifically set to false
if (assemblyName != null && (isSimpleName || (foundSpecificVersionMetadata && !wantSpecificVersion)))
{
assemblyName = new AssemblyNameExtension
(
AssemblyNameExtension.EscapeDisplayNameCharacters(assemblyName.Name)
);
isSimpleName = assemblyName.IsSimpleName;
}
// Set the HintPath if there is one.
reference.HintPath = referenceAssemblyName.GetMetadata(ItemMetadataNames.hintPath);
if (assemblyName != null && !wantSpecificVersion && !isSimpleName && reference.HintPath.Length == 0)
{
// Check to see if the assemblyname is in the framework list just use that fusion name
if (_installedAssemblies != null && ignoreVersionForFrameworkReference)
{
AssemblyEntry entry = _installedAssemblies.FindHighestVersionInRedistList(assemblyName);
if (entry != null)
{
assemblyName = entry.AssemblyNameExtension.Clone();
}
}
}
if (assemblyName != null && _installedAssemblies != null && !wantSpecificVersion && reference.HintPath.Length == 0)
{
AssemblyNameExtension remappedExtension = _installedAssemblies.RemapAssemblyExtension(assemblyName);
if (remappedExtension != null)
{
reference.AddRemapping(assemblyName.CloneImmutable(), remappedExtension.CloneImmutable());
assemblyName = remappedExtension;
}
}
// Embed Interop Types aka "NOPIAs" support is not available for Fx < 4.0
// So, we just ignore this setting on down-level platforms
if (_projectTargetFramework != null && _projectTargetFramework >= s_targetFrameworkVersion_40)
{
reference.EmbedInteropTypes = MetadataConversionUtilities.TryConvertItemMetadataToBool
(
referenceAssemblyName,
ItemMetadataNames.embedInteropTypes
);
}
// Set the AssemblyFolderKey if there is one.
reference.AssemblyFolderKey = referenceAssemblyName.GetMetadata(ItemMetadataNames.assemblyFolderKey);
// It's possible, especially in cases where the fusion name was passed in through the item
// that we'll have a better (more information) fusion name once we know the assembly path.
try
{
ResolveReference(assemblyName, rawFileNameCandidate, reference);
if (reference.IsResolved)
{
AssemblyNameExtension possiblyBetterAssemblyName;
try
{
// This may throw if, for example, the culture embedded in the assembly's manifest
// is not recognised by AssemblyName.GetAssemblyName
possiblyBetterAssemblyName = _getAssemblyName(reference.FullPath);
}
catch (ArgumentException)
{
// Give up trying to get a better name
possiblyBetterAssemblyName = null;
}
// Use the better name if it exists.
if (possiblyBetterAssemblyName?.Name != null)
{
assemblyName = possiblyBetterAssemblyName;
}
}
}
catch (BadImageFormatException e)
{
// If this isn't a valid assembly, then record the exception and continue on
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (FileNotFoundException e) // Why isn't this covered in NotExpectedException?
{
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (FileLoadException e)
{
// Managed assembly was found but could not be loaded.
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
reference.AddError(new BadImageReferenceException(e.Message, e));
}
// If there is still no assembly name then this is a case where the assembly metadata
// just doesn't contain an assembly name. We want to try to tolerate this because
// mscorlib.dll (sometimes?) doesn't contain an assembly name.
if (assemblyName == null)
{
if (!reference.IsResolved)
{
// The file doesn't exist and the reference was unresolved, there's nothing we can do at this point.
// Return, rather than throw, the exception, as in some situations it can happen thousands of times.
return new InvalidReferenceAssemblyNameException(referenceAssemblyName.ItemSpec);
}
assemblyName = new AssemblyNameExtension
(
AssemblyNameExtension.EscapeDisplayNameCharacters(reference.FileNameWithoutExtension)
);
}
// Check to see if this is a prereq assembly.
if (_installedAssemblies == null)
{
reference.IsPrerequisite = false;
}
else
{
_installedAssemblies.GetInfo
(
assemblyName,
out _,
out bool isPrerequisite,
out bool? isRedistRoot,
out string redistName
);
reference.IsPrerequisite = isPrerequisite;
reference.IsRedistRoot = isRedistRoot;
reference.RedistName = redistName;
}
AddReference(assemblyName, reference);
if (reference.ExternallyResolved)
{
_externallyResolvedPrimaryReferences.Add(assemblyName.Name);
}
return null;
}
/// <summary>
/// Attempts to convert an itemSpec and fusionName into an assembly name.
/// AssemblyName is left unchanged if conversion wasn't possible.
/// </summary>
private static void TryConvertToAssemblyName(string itemSpec, string fusionName, ref AssemblyNameExtension assemblyName)
{
// FusionName is used if available; otherwise use itemspec.
string finalName = string.IsNullOrEmpty(fusionName) ? itemSpec : fusionName;
bool pathRooted = false;
try
{
pathRooted = Path.IsPathRooted(finalName);
}
catch (ArgumentException)
{
/* Eat this because it has invalid chars in to and cannot be a path, maybe it can be parsed as a fusion name.*/
}
if (!pathRooted)
{
// Now try to convert to an AssemblyName.
try
{
assemblyName = new AssemblyNameExtension(finalName, true /*throw if not valid*/);
}
catch (FileLoadException)
{
// Not a valid AssemblyName. Maybe its a file name.
TryGatherAssemblyNameEssentials(finalName, ref assemblyName);
return;
}
}
else
{
// Maybe the string has a fusion name inside of it.
TryGatherAssemblyNameEssentials(finalName, ref assemblyName);
}
}
/// <summary>
/// Given a string that may be a fusion name, try to gather the four essential properties:
/// Name
/// Version
/// PublicKeyToken
/// Culture
/// </summary>
/// <param name="fusionName"></param>
/// <param name="assemblyName"></param>
private static void TryGatherAssemblyNameEssentials(string fusionName, ref AssemblyNameExtension assemblyName)
{
int firstComma = fusionName.IndexOf(',');
if (firstComma == -1)
{
return;
}
string name = fusionName.Substring(0, firstComma);
string version = null;
string publicKeyToken = null;
string culture = null;
TryGetAssemblyNameComponent(fusionName, "Version", ref version);
TryGetAssemblyNameComponent(fusionName, "PublicKeyToken", ref publicKeyToken);
TryGetAssemblyNameComponent(fusionName, "Culture", ref culture);
if (version == null || publicKeyToken == null || culture == null)
{
return;
}
string newFusionName = String.Format(CultureInfo.InvariantCulture,
"{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
name, version, culture, publicKeyToken);
// Now try to convert to an AssemblyName.
try
{
assemblyName = new AssemblyNameExtension(newFusionName, true /* throw if not valid */);
}
catch (FileLoadException)
{
// Not a valid AssemblyName. Maybe it's a file name.
// TryGatherAssemblyNameEssentials
return;
}
}
/// <summary>
/// Attempt to get one field out of an assembly name.
/// </summary>
private static void TryGetAssemblyNameComponent(string fusionName, string component, ref string value)
{
int position = fusionName.IndexOf(component + "=", StringComparison.Ordinal);
if (position == -1)
{
return;
}
position += component.Length + 1;
int nextDelimiter = fusionName.IndexOfAny(new[] { ',', ' ' }, position);
if (nextDelimiter == -1)
{
value = fusionName.Substring(position);
}
else
{
value = fusionName.Substring(position, nextDelimiter - position);
}
}
/// <summary>
/// Given an item that refers to a file name, make it a primary reference.
/// </summary>
private void SetPrimaryFileItem(ITaskItem referenceAssemblyFile)
{
try
{
// Create the reference.
var reference = new Reference(_isWinMDFile, _fileExists, _getRuntimeVersion);
string itemSpec = referenceAssemblyFile.ItemSpec;
bool hasSpecificVersionMetadata = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyFile, ItemMetadataNames.specificVersion);
reference.MakePrimaryAssemblyReference
(
referenceAssemblyFile,
hasSpecificVersionMetadata,
Path.GetExtension(itemSpec)
);
AssemblyNameExtension assemblyName = NameAssemblyFileReference
(
reference,
itemSpec // Contains the assembly file name.
);
// Embed Interop Types aka "NOPIAs" support is not available for Fx < 4.0
// So, we just ignore this setting on down-level platforms
if (_projectTargetFramework >= s_targetFrameworkVersion_40)
{
reference.EmbedInteropTypes = MetadataConversionUtilities.TryConvertItemMetadataToBool
(
referenceAssemblyFile,
ItemMetadataNames.embedInteropTypes
);
}
AddReference(assemblyName, reference);
if (reference.ExternallyResolved)
{
_externallyResolvedPrimaryReferences.Add(assemblyName.Name);
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
throw new InvalidParameterValueException("AssemblyFiles", referenceAssemblyFile.ItemSpec, e.Message);
}
}
/// <summary>
/// Find related files like .pdbs and .xmls
/// </summary>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindRelatedFiles
(
Reference reference
)
{
string baseName = reference.FullPathWithoutExtension;
// Look for companion files like pdbs and xmls that ride along with
// assemblies.
foreach (string companionExtension in _relatedFileExtensions)
{
string companionFile = baseName + companionExtension;
if (_fileExists(companionFile))
{
reference.AddRelatedFileExtension(companionExtension);
}
}
// Native Winmd files may have a companion dll beside it.
if (reference.IsWinMDFile && !reference.IsManagedWinMDFile)
{
var companionFile = baseName + ".dll";
if (reference.IsPrimary)
{
var implementationFile = reference.PrimarySourceItem.GetMetadata(ItemMetadataNames.winmdImplmentationFile);
// Static library projects can produce a .winmd with an associated .lib, but that is not
// a real ImplementationAssembly--it would fail downstream when trying to read its PE header.
if (!String.IsNullOrEmpty(implementationFile) && Path.GetExtension(implementationFile) == ".dll")
{
companionFile = Path.Combine(Path.GetDirectoryName(baseName), implementationFile);
}
}
if (_fileExists(companionFile))
{
reference.ImplementationAssembly = companionFile;
}
}
}
/// <summary>
/// Find satellite assemblies.
/// </summary>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindSatellites
(
Reference reference
)
{
try
{
// If the directory doesn't exist (which is possible in the situation
// where we were passed in a pre-resolved reference from a P2P reference
// that hasn't actually been built yet), then GetDirectories will throw.
// Avoid that by just short-circuiting here.
if (!_directoryExists(reference.DirectoryName))
{
return;
}
string[] subDirectories = _getDirectories(reference.DirectoryName, "*");
string sateliteFilename = subDirectories.Length > 0
? reference.FileNameWithoutExtension + ".resources.dll"
: string.Empty;
foreach (string subDirectory in subDirectories)
{
// Is there a candidate satellite in that folder?
string cultureName = Path.GetFileName(subDirectory);
if (CultureInfoCache.IsValidCultureString(cultureName))
{
string satelliteAssembly = Path.Combine(subDirectory, sateliteFilename);
if (_fileExists(satelliteAssembly))
{
// This is valid satellite assembly.
reference.AddSatelliteFile(Path.Combine(cultureName, sateliteFilename));
}
}
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
_log?.LogErrorFromResources("ResolveAssemblyReference.ProblemFindingSatelliteAssemblies", reference.FullPath, e.Message);
}
}
/// <summary>
/// Find serialization assemblies.
/// </summary>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindSerializationAssemblies
(
Reference reference
)
{