Skip to content

Commit 6e76b8f

Browse files
Copilotdsplaisted
andcommitted
Move LoadPrunePackageDataFromNearestFramework to CacheKey and move useFrameworkPackageData logic into TryLoadPackagesToPruneForVersion
Co-authored-by: dsplaisted <[email protected]>
1 parent 272d2e9 commit 6e76b8f

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/GetPackagesToPrune.cs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ class CacheKey
4848
public string TargetFrameworkIdentifier { get; set; }
4949
public string TargetFrameworkVersion { get; set; }
5050
public HashSet<string> FrameworkReferences { get; set; }
51+
public bool LoadPrunePackageDataFromNearestFramework { get; set; }
5152

5253
public override bool Equals(object obj) => obj is CacheKey key &&
5354
TargetFrameworkIdentifier == key.TargetFrameworkIdentifier &&
5455
TargetFrameworkVersion == key.TargetFrameworkVersion &&
55-
FrameworkReferences.SetEquals(key.FrameworkReferences);
56+
FrameworkReferences.SetEquals(key.FrameworkReferences) &&
57+
LoadPrunePackageDataFromNearestFramework == key.LoadPrunePackageDataFromNearestFramework;
5658
public override int GetHashCode()
5759
{
5860
#if NET
@@ -63,6 +65,7 @@ public override int GetHashCode()
6365
{
6466
hashCode.Add(frameworkReference);
6567
}
68+
hashCode.Add(LoadPrunePackageDataFromNearestFramework);
6669
return hashCode.ToHashCode();
6770
#else
6871
int hashCode = 1436330440;
@@ -73,6 +76,7 @@ public override int GetHashCode()
7376
{
7477
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(frameworkReference);
7578
}
79+
hashCode = hashCode * -1521134295 + LoadPrunePackageDataFromNearestFramework.GetHashCode();
7680
return hashCode;
7781
#endif
7882
}
@@ -107,7 +111,8 @@ protected override void ExecuteCore()
107111
{
108112
TargetFrameworkIdentifier = TargetFrameworkIdentifier,
109113
TargetFrameworkVersion = TargetFrameworkVersion,
110-
FrameworkReferences = runtimeFrameworks.ToHashSet()
114+
FrameworkReferences = runtimeFrameworks.ToHashSet(),
115+
LoadPrunePackageDataFromNearestFramework = LoadPrunePackageDataFromNearestFramework
111116
};
112117

113118
// Cache framework package values per build
@@ -118,12 +123,12 @@ protected override void ExecuteCore()
118123
return;
119124
}
120125

121-
PackagesToPrune = LoadPackagesToPrune(key, TargetingPackRoots, PrunePackageDataRoot, Log, AllowMissingPrunePackageData, LoadPrunePackageDataFromNearestFramework);
126+
PackagesToPrune = LoadPackagesToPrune(key, TargetingPackRoots, PrunePackageDataRoot, Log, AllowMissingPrunePackageData);
122127

123128
BuildEngine4.RegisterTaskObject(key, PackagesToPrune, RegisteredTaskObjectLifetime.Build, true);
124129
}
125130

126-
static TaskItem[] LoadPackagesToPrune(CacheKey key, string[] targetingPackRoots, string prunePackageDataRoot, Logger log, bool allowMissingPrunePackageData, bool loadPrunePackageDataFromNearestFramework)
131+
static TaskItem[] LoadPackagesToPrune(CacheKey key, string[] targetingPackRoots, string prunePackageDataRoot, Logger log, bool allowMissingPrunePackageData)
127132
{
128133
Dictionary<string, NuGetVersion> packagesToPrune = new();
129134

@@ -135,10 +140,6 @@ static TaskItem[] LoadPackagesToPrune(CacheKey key, string[] targetingPackRoots,
135140
return Array.Empty<TaskItem>();
136141
}
137142

138-
// Use hard-coded / generated "framework package data" for .NET 9 and lower, .NET Framework, and .NET Standard
139-
// Use bundled "prune package data" for .NET 10 and higher. During the redist build, this comes from targeting packs and is laid out in the PrunePackageData folder.
140-
bool useFrameworkPackageData = !key.TargetFrameworkIdentifier.Equals(".NETCoreApp") || targetFrameworkVersion.Major < 10;
141-
142143
// Call DefaultIfEmpty() so that target frameworks without framework references will load data
143144
foreach (var frameworkReference in key.FrameworkReferences.DefaultIfEmpty(""))
144145
{
@@ -152,23 +153,7 @@ static TaskItem[] LoadPackagesToPrune(CacheKey key, string[] targetingPackRoots,
152153
}
153154
log.LogMessage(MessageImportance.Low, $"Loading packages to prune for {key.TargetFrameworkIdentifier} {key.TargetFrameworkVersion} {frameworkReference}");
154155

155-
Dictionary<string, NuGetVersion> packagesForFrameworkReference;
156-
if (useFrameworkPackageData)
157-
{
158-
packagesForFrameworkReference = LoadPackagesToPruneFromFrameworkPackages(key.TargetFrameworkIdentifier, key.TargetFrameworkVersion, frameworkReference);
159-
if (packagesForFrameworkReference != null)
160-
{
161-
log.LogMessage("Loaded prune package data from framework packages");
162-
}
163-
else
164-
{
165-
log.LogMessage("Failed to load prune package data from framework packages");
166-
}
167-
}
168-
else
169-
{
170-
packagesForFrameworkReference = TryLoadPackagesToPruneForVersion(log, key.TargetFrameworkIdentifier, key.TargetFrameworkVersion, frameworkReference, targetingPackRoots, prunePackageDataRoot, loadPrunePackageDataFromNearestFramework);
171-
}
156+
Dictionary<string, NuGetVersion> packagesForFrameworkReference = TryLoadPackagesToPruneForVersion(log, key.TargetFrameworkIdentifier, key.TargetFrameworkVersion, frameworkReference, targetingPackRoots, prunePackageDataRoot, key.LoadPrunePackageDataFromNearestFramework);
172157

173158
if (packagesForFrameworkReference == null)
174159
{
@@ -265,31 +250,52 @@ static Dictionary<string, NuGetVersion> LoadPackagesToPruneFromTargetingPack(Log
265250

266251
static Dictionary<string, NuGetVersion> TryLoadPackagesToPruneForVersion(Logger log, string targetFrameworkIdentifier, string targetFrameworkVersion, string frameworkReference, string[] targetingPackRoots, string prunePackageDataRoot, bool loadPrunePackageDataFromNearestFramework)
267252
{
268-
log.LogMessage("Loading prune package data from PrunePackageData folder");
269-
var packages = LoadPackagesToPruneFromPrunePackageData(targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference, prunePackageDataRoot);
253+
var targetVersion = Version.Parse(targetFrameworkVersion);
254+
255+
// Use hard-coded / generated "framework package data" for .NET 9 and lower, .NET Framework, and .NET Standard
256+
// Use bundled "prune package data" for .NET 10 and higher. During the redist build, this comes from targeting packs and is laid out in the PrunePackageData folder.
257+
bool useFrameworkPackageData = !targetFrameworkIdentifier.Equals(".NETCoreApp") || targetVersion.Major < 10;
258+
259+
Dictionary<string, NuGetVersion> packages = null;
270260

271-
// For the version of the runtime that matches the current SDK version, we don't include the prune package data in the PrunePackageData folder. Rather,
272-
// we can load it from the targeting packs that are packaged with the SDK.
273-
if (packages == null)
261+
if (useFrameworkPackageData)
274262
{
275-
log.LogMessage("Failed to load prune package data from PrunePackageData folder, loading from targeting packs instead");
276-
packages = LoadPackagesToPruneFromTargetingPack(log, targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference, targetingPackRoots);
263+
packages = LoadPackagesToPruneFromFrameworkPackages(targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference);
264+
if (packages != null)
265+
{
266+
log.LogMessage("Loaded prune package data from framework packages");
267+
}
268+
else
269+
{
270+
log.LogMessage("Failed to load prune package data from framework packages");
271+
}
277272
}
278-
279-
// Fall back to framework packages data for older framework for WindowsDesktop if necessary
280-
// https://github.com/dotnet/windowsdesktop/issues/4904
281-
if (packages == null && frameworkReference.Equals("Microsoft.WindowsDesktop.App", StringComparison.OrdinalIgnoreCase))
273+
else
282274
{
283-
log.LogMessage("Failed to load prune package data for WindowsDesktop from targeting packs, loading from framework packages instead");
284-
packages = LoadPackagesToPruneFromFrameworkPackages(targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference,
285-
acceptNearestMatch: true);
275+
log.LogMessage("Loading prune package data from PrunePackageData folder");
276+
packages = LoadPackagesToPruneFromPrunePackageData(targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference, prunePackageDataRoot);
277+
278+
// For the version of the runtime that matches the current SDK version, we don't include the prune package data in the PrunePackageData folder. Rather,
279+
// we can load it from the targeting packs that are packaged with the SDK.
280+
if (packages == null)
281+
{
282+
log.LogMessage("Failed to load prune package data from PrunePackageData folder, loading from targeting packs instead");
283+
packages = LoadPackagesToPruneFromTargetingPack(log, targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference, targetingPackRoots);
284+
}
285+
286+
// Fall back to framework packages data for older framework for WindowsDesktop if necessary
287+
// https://github.com/dotnet/windowsdesktop/issues/4904
288+
if (packages == null && frameworkReference.Equals("Microsoft.WindowsDesktop.App", StringComparison.OrdinalIgnoreCase))
289+
{
290+
log.LogMessage("Failed to load prune package data for WindowsDesktop from targeting packs, loading from framework packages instead");
291+
packages = LoadPackagesToPruneFromFrameworkPackages(targetFrameworkIdentifier, targetFrameworkVersion, frameworkReference,
292+
acceptNearestMatch: true);
293+
}
286294
}
287295

288296
// If LoadPrunePackageDataFromNearestFramework is true and we still haven't found data, try the previous framework version
289297
if (packages == null && loadPrunePackageDataFromNearestFramework)
290298
{
291-
var targetVersion = Version.Parse(targetFrameworkVersion);
292-
293299
// If we can go to a lower version, recursively try it
294300
if (targetVersion.Major > MinSupportedFrameworkMajorVersion)
295301
{

0 commit comments

Comments
 (0)