|
5 | 5 | using Microsoft.Build.Utilities; |
6 | 6 | using System; |
7 | 7 | using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
8 | 9 | using System.IO; |
9 | 10 | using System.Reflection.Metadata; |
10 | 11 | using System.Reflection.PortableExecutable; |
@@ -99,16 +100,20 @@ public override bool Execute() |
99 | 100 | var list = new List<ITaskItem>(); |
100 | 101 | var assembliesToSkipPublish = new List<ITaskItem>(); |
101 | 102 | var satelliteAssemblies = new List<ITaskItem>(); |
102 | | - var nativeAotFrameworkAssembliesToUse = new HashSet<string>(); |
| 103 | + var nativeAotFrameworkAssembliesToUse = new Dictionary<string, ITaskItem>(); |
103 | 104 |
|
104 | 105 | foreach (ITaskItem taskItem in SdkAssemblies) |
105 | 106 | { |
106 | | - nativeAotFrameworkAssembliesToUse.Add(Path.GetFileName(taskItem.ItemSpec)); |
| 107 | + var fileName = Path.GetFileName(taskItem.ItemSpec); |
| 108 | + if (!nativeAotFrameworkAssembliesToUse.ContainsKey(fileName)) |
| 109 | + nativeAotFrameworkAssembliesToUse.Add(fileName, taskItem); |
107 | 110 | } |
108 | 111 |
|
109 | 112 | foreach (ITaskItem taskItem in FrameworkAssemblies) |
110 | 113 | { |
111 | | - nativeAotFrameworkAssembliesToUse.Add(Path.GetFileName(taskItem.ItemSpec)); |
| 114 | + var fileName = Path.GetFileName(taskItem.ItemSpec); |
| 115 | + if (!nativeAotFrameworkAssembliesToUse.ContainsKey(fileName)) |
| 116 | + nativeAotFrameworkAssembliesToUse.Add(fileName, taskItem); |
112 | 117 | } |
113 | 118 |
|
114 | 119 | foreach (ITaskItem taskItem in Assemblies) |
@@ -153,8 +158,21 @@ public override bool Execute() |
153 | 158 |
|
154 | 159 | // Remove any assemblies whose implementation we want to come from NativeAOT's package. |
155 | 160 | // Currently that's System.Private.* SDK assemblies and a bunch of framework assemblies. |
156 | | - if (nativeAotFrameworkAssembliesToUse.Contains(assemblyFileName)) |
| 161 | + if (nativeAotFrameworkAssembliesToUse.TryGetValue(assemblyFileName, out ITaskItem frameworkItem)) |
157 | 162 | { |
| 163 | + if (GetFileVersion(itemSpec).CompareTo(GetFileVersion(frameworkItem.ItemSpec)) > 0) |
| 164 | + { |
| 165 | + if (assemblyFileName == "System.Private.CoreLib.dll") |
| 166 | + { |
| 167 | + Log.LogError($"Overriding System.Private.CoreLib.dll with a newer version is not supported. Attempted to use {itemSpec} instead of {frameworkItem.ItemSpec}."); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + // Allow OOB references with higher version to take precedence over the framework assemblies. |
| 172 | + list.Add(taskItem); |
| 173 | + } |
| 174 | + } |
| 175 | + |
158 | 176 | assembliesToSkipPublish.Add(taskItem); |
159 | 177 | continue; |
160 | 178 | } |
@@ -196,6 +214,12 @@ public override bool Execute() |
196 | 214 | SatelliteAssemblies = satelliteAssemblies.ToArray(); |
197 | 215 |
|
198 | 216 | return true; |
| 217 | + |
| 218 | + static Version GetFileVersion(string path) |
| 219 | + { |
| 220 | + var versionInfo = FileVersionInfo.GetVersionInfo(path); |
| 221 | + return new Version(versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart, versionInfo.FilePrivatePart); |
| 222 | + } |
199 | 223 | } |
200 | 224 | } |
201 | 225 | } |
0 commit comments