Skip to content

Commit

Permalink
ILC: Allow OOB reference to upgrade framework assembly (#109988)
Browse files Browse the repository at this point in the history
* ILC: Allow OOB reference to upgrade framework assembly
* Log error for SPC
  • Loading branch information
sbomer authored Nov 21, 2024
1 parent eb456e6 commit 36903dc
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -99,16 +100,20 @@ public override bool Execute()
var list = new List<ITaskItem>();
var assembliesToSkipPublish = new List<ITaskItem>();
var satelliteAssemblies = new List<ITaskItem>();
var nativeAotFrameworkAssembliesToUse = new HashSet<string>();
var nativeAotFrameworkAssembliesToUse = new Dictionary<string, ITaskItem>();

foreach (ITaskItem taskItem in SdkAssemblies)
{
nativeAotFrameworkAssembliesToUse.Add(Path.GetFileName(taskItem.ItemSpec));
var fileName = Path.GetFileName(taskItem.ItemSpec);
if (!nativeAotFrameworkAssembliesToUse.ContainsKey(fileName))
nativeAotFrameworkAssembliesToUse.Add(fileName, taskItem);
}

foreach (ITaskItem taskItem in FrameworkAssemblies)
{
nativeAotFrameworkAssembliesToUse.Add(Path.GetFileName(taskItem.ItemSpec));
var fileName = Path.GetFileName(taskItem.ItemSpec);
if (!nativeAotFrameworkAssembliesToUse.ContainsKey(fileName))
nativeAotFrameworkAssembliesToUse.Add(fileName, taskItem);
}

foreach (ITaskItem taskItem in Assemblies)
Expand Down Expand Up @@ -153,8 +158,21 @@ public override bool Execute()

// Remove any assemblies whose implementation we want to come from NativeAOT's package.
// Currently that's System.Private.* SDK assemblies and a bunch of framework assemblies.
if (nativeAotFrameworkAssembliesToUse.Contains(assemblyFileName))
if (nativeAotFrameworkAssembliesToUse.TryGetValue(assemblyFileName, out ITaskItem frameworkItem))
{
if (GetFileVersion(itemSpec).CompareTo(GetFileVersion(frameworkItem.ItemSpec)) > 0)
{
if (assemblyFileName == "System.Private.CoreLib.dll")
{
Log.LogError($"Overriding System.Private.CoreLib.dll with a newer version is not supported. Attempted to use {itemSpec} instead of {frameworkItem.ItemSpec}.");
}
else
{
// Allow OOB references with higher version to take precedence over the framework assemblies.
list.Add(taskItem);
}
}

assembliesToSkipPublish.Add(taskItem);
continue;
}
Expand Down Expand Up @@ -196,6 +214,12 @@ public override bool Execute()
SatelliteAssemblies = satelliteAssemblies.ToArray();

return true;

static Version GetFileVersion(string path)
{
var versionInfo = FileVersionInfo.GetVersionInfo(path);
return new Version(versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart, versionInfo.FilePrivatePart);
}
}
}
}

0 comments on commit 36903dc

Please sign in to comment.