Skip to content

Commit 6b47b53

Browse files
authored
Rewrite how we swap assemblies out as part of NativeAOT to specifically check that they're coming from the runtime pack instead of checking file version numbers and using that as a heuristic for the same thing. (#111944)
1 parent 87e9f1d commit 6b47b53

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

src/coreclr/tools/aot/ILCompiler.Build.Tasks/ComputeManagedAssembliesToCompileToNative.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public override bool Execute()
120120
{
121121
// In the case of disk-based assemblies, this holds the file path
122122
string itemSpec = taskItem.ItemSpec;
123+
string assemblyFileName = Path.GetFileName(itemSpec);
124+
bool isFromRuntimePack = taskItem.GetMetadata("NuGetPackageId")?.StartsWith("Microsoft.NETCore.App.Runtime.", StringComparison.OrdinalIgnoreCase) == true;
123125

124126
// Skip the native apphost (whose name ends up colliding with the native output binary) and supporting libraries
125127
if (itemSpec.EndsWith(DotNetAppHostExecutableName, StringComparison.OrdinalIgnoreCase) || itemSpec.Contains(DotNetHostFxrLibraryName) || itemSpec.Contains(DotNetHostPolicyLibraryName))
@@ -128,55 +130,39 @@ public override bool Execute()
128130
continue;
129131
}
130132

131-
// Prototype aid - remove the native CoreCLR runtime pieces from the publish folder
132-
if (itemSpec.IndexOf("microsoft.netcore.app", StringComparison.OrdinalIgnoreCase) != -1 && (itemSpec.Contains("\\native\\") || itemSpec.Contains("/native/")))
133+
if (isFromRuntimePack && taskItem.GetMetadata("AssetType")?.Equals("native", StringComparison.OrdinalIgnoreCase) == true)
133134
{
135+
// Skip the native components of the runtime pack, we don't need them for NativeAOT.
134136
assembliesToSkipPublish.Add(taskItem);
135137
continue;
136138
}
137139

138-
var assemblyFileName = Path.GetFileName(itemSpec);
139-
140-
if (assemblyFileName == "WindowsBase.dll")
141-
{
142-
// There are two instances of WindowsBase.dll, one small one, in the NativeAOT framework
143-
// and real one in WindowsDesktop SDK. We want to make sure that if both are present,
144-
// we will use the one from WindowsDesktop SDK, and not from NativeAOT framework.
145-
foreach (ITaskItem taskItemToSkip in FrameworkAssemblies)
146-
{
147-
if (Path.GetFileName(taskItemToSkip.ItemSpec) == assemblyFileName)
148-
{
149-
assembliesToSkipPublish.Add(taskItemToSkip);
150-
break;
151-
}
152-
}
153-
154-
assembliesToSkipPublish.Add(taskItem);
155-
list.Add(taskItem);
156-
continue;
157-
}
158-
159140
// Remove any assemblies whose implementation we want to come from NativeAOT's package.
160141
// Currently that's System.Private.* SDK assemblies and a bunch of framework assemblies.
161142
if (nativeAotFrameworkAssembliesToUse.TryGetValue(assemblyFileName, out ITaskItem frameworkItem))
162143
{
163-
if (GetFileVersion(itemSpec).CompareTo(GetFileVersion(frameworkItem.ItemSpec)) > 0)
144+
// If the assembly is part of the Microsoft.NETCore.App.Runtime runtime pack, we want to swap it with the corresponding package from the NativeAOT SDK.
145+
// Otherwise we want to use the assembly the user has referenced.
146+
if (!isFromRuntimePack)
164147
{
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-
}
148+
// The assembly was overridden by an OOB package through standard .NET SDK conflict resolution.
149+
// Use that version instead of swapping to the NativeAOT one.
150+
list.Add(taskItem);
151+
}
152+
else if (assemblyFileName == "System.Private.CoreLib.dll" && GetFileVersion(itemSpec).CompareTo(GetFileVersion(frameworkItem.ItemSpec)) > 0)
153+
{
154+
// Validate that we aren't trying to use an older NativeAOT package against a newer non-NativeAOT runtime pack.
155+
// That's not supported.
156+
Log.LogError($"Overriding System.Private.CoreLib.dll with a newer version is not supported. Attempted to use {itemSpec} instead of {frameworkItem.ItemSpec}.");
174157
}
175158

176159
assembliesToSkipPublish.Add(taskItem);
177160
continue;
178161
}
179162

163+
// For all other files, check if they are managed assemblies.
164+
// If they're managed, skip publishing them and categorize them correctly as inputs to ILC.
165+
// If they're not managed assemblies, then they're native dependencies. Allow them to be published.
180166
try
181167
{
182168
using (FileStream moduleStream = File.OpenRead(itemSpec))

0 commit comments

Comments
 (0)