Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect the profile attribute on runtime packs #39402

Merged
merged 8 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "9.0.100-preview.1.24101.2",
"dotnet": "9.0.100-preview.3.24175.24",
"runtimes": {
"dotnet": [
"$(VSRedistCommonNetCoreSharedFrameworkx6490PackageVersion)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ var runtimeRequiredByDeployment

runtimeFramework.SetMetadata(MetadataKeys.Version, runtimeFrameworkVersion);
runtimeFramework.SetMetadata(MetadataKeys.FrameworkName, knownFrameworkReference.Name);
runtimeFramework.SetMetadata("Profile", knownFrameworkReference.Profile);

runtimeFrameworks.Add(runtimeFramework);
}
Expand Down
53 changes: 51 additions & 2 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolveRuntimePackAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ResolveRuntimePackAssets : TaskBase

public ITaskItem[] SatelliteResourceLanguages { get; set; } = Array.Empty<ITaskItem>();

public ITaskItem[] RuntimeFrameworks { get; set; }

public bool DesignTimeBuild { get; set; }

public bool DisableTransitiveFrameworkReferenceDownloads { get; set; }
Expand All @@ -27,6 +29,18 @@ protected override void ExecuteCore()
{
var runtimePackAssets = new List<ITaskItem>();

// Find any RuntimeFrameworks that matches with FrameworkReferences, so that we can apply that RuntimeFrameworks profile to the corresponding RuntimePack.
// This is done in 2 parts, First part (see comments for 2nd part further below), we match the RuntimeFramework with the FrameworkReference by using the following metadata.
// RuntimeFrameworks.GetMetadata("FrameworkName")==FrameworkReferences.ItemSpec AND RuntimeFrameworks.GetMetadata("Profile") is not empty
// For example, A WinForms app that uses useWindowsForms (and useWPF will be set to false) has the following values that will result in a match of the below RuntimeFramework.
// FrameworkReferences with an ItemSpec "Microsoft.WindowsDesktop.App.WindowsForms" will match with
// RuntimeFramework with an ItemSpec => "Microsoft.WindowsDesktop.App", GetMetadata("FrameworkName") => "Microsoft.WindowsDesktop.App.WindowsForms", GetMetadata("Profile") => "WindowsForms"
List<ITaskItem> matchingRuntimeFrameworks = RuntimeFrameworks != null ? FrameworkReferences
.SelectMany(fxReference => RuntimeFrameworks.Where(rtFx =>
fxReference.ItemSpec.Equals(rtFx.GetMetadata(MetadataKeys.FrameworkName), StringComparison.OrdinalIgnoreCase) &&
!string.IsNullOrEmpty(rtFx.GetMetadata("Profile"))))
.ToList() : null;

HashSet<string> frameworkReferenceNames = new(FrameworkReferences.Select(item => item.ItemSpec), StringComparer.OrdinalIgnoreCase);

foreach (var unavailableRuntimePack in UnavailableRuntimePacks)
Expand Down Expand Up @@ -55,6 +69,15 @@ protected override void ExecuteCore()
}
}

// For any RuntimeFrameworks that matches with FrameworkReferences, we can apply that RuntimeFrameworks profile to the corresponding RuntimePack.
// This is done in 2 parts, second part (see comments for 1st part above), Matches the RuntimeFramework with the ResolvedRuntimePacks by comparing the following metadata.
// RuntimeFrameworks.ItemSpec == ResolvedRuntimePacks.GetMetadata("FrameworkName")
// For example, A WinForms app that uses useWindowsForms (and useWPF will be set to false) has the following values that will result in a match of the below RuntimeFramework
// matchingRTReference.GetMetadata("Profile") will be "WindowsForms". 'Profile' will be an empty string if no matching RuntimeFramework is found
HashSet<string> profiles = matchingRuntimeFrameworks?
.Where(matchingRTReference => runtimePack.GetMetadata("FrameworkName").Equals(matchingRTReference.ItemSpec))
.Select(matchingRTReference => matchingRTReference.GetMetadata("Profile")).ToHashSet() ?? [];

string runtimePackRoot = runtimePack.GetMetadata(MetadataKeys.PackageDirectory);

if (string.IsNullOrEmpty(runtimePackRoot) || !Directory.Exists(runtimePackRoot))
Expand Down Expand Up @@ -91,7 +114,7 @@ protected override void ExecuteCore()
{
var runtimePackAlwaysCopyLocal = runtimePack.HasMetadataValue(MetadataKeys.RuntimePackAlwaysCopyLocal, "true");

AddRuntimePackAssetsFromManifest(runtimePackAssets, runtimePackRoot, runtimeListPath, runtimePack, runtimePackAlwaysCopyLocal);
AddRuntimePackAssetsFromManifest(runtimePackAssets, runtimePackRoot, runtimeListPath, runtimePack, runtimePackAlwaysCopyLocal, profiles);
}
else
{
Expand All @@ -103,13 +126,39 @@ protected override void ExecuteCore()
}

private void AddRuntimePackAssetsFromManifest(List<ITaskItem> runtimePackAssets, string runtimePackRoot,
string runtimeListPath, ITaskItem runtimePack, bool runtimePackAlwaysCopyLocal)
string runtimeListPath, ITaskItem runtimePack, bool runtimePackAlwaysCopyLocal, HashSet<string> profiles)
{
var assetSubPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

XDocument frameworkListDoc = XDocument.Load(runtimeListPath);
// profile feature is only supported in net9.0 and later. We would ignore it for previous versions.
bool profileSupported = false;
string targetFrameworkVersion = frameworkListDoc.Root.Attribute("TargetFrameworkVersion")?.Value;
if (!string.IsNullOrEmpty(targetFrameworkVersion))
{
string[] parts = targetFrameworkVersion.Split('.');
if (parts.Length > 0 && int.TryParse(parts[0], out int versionNumber))
{
if (versionNumber >= 9)
{
profileSupported = true;
}
}
}
foreach (var fileElement in frameworkListDoc.Root.Elements("File"))
{
if (profileSupported && profiles.Count != 0)
{
var profileAttributeValue = fileElement.Attribute("Profile")?.Value;

var assemblyProfiles = profileAttributeValue?.Split(';');
if (profileAttributeValue == null || !assemblyProfiles.Any(p => profiles.Contains(p)))
{
// Assembly wasn't in the profile specified, so don't reference it
continue;
}
}

// Call GetFullPath to normalize slashes
string assetPath = Path.GetFullPath(Path.Combine(runtimePackRoot, fileElement.Attribute("Path").Value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Condition="'@(RuntimePack)' != ''">

<ResolveRuntimePackAssets FrameworkReferences="@(FrameworkReference)"
RuntimeFrameworks="@(RuntimeFramework)"
ResolvedRuntimePacks="@(ResolvedRuntimePack)"
UnavailableRuntimePacks="@(UnavailableRuntimePack)"
SatelliteResourceLanguages="$(SatelliteResourceLanguages)"
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.NET.Build.Tests/GivenFrameworkReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public void WindowsFormsFrameworkReference(bool selfContained)
TestFrameworkReferenceProfiles(
frameworkReferences: new[] { "Microsoft.WindowsDesktop.App.WindowsForms" },
expectedReferenceNames: new[] { "Microsoft.Win32.Registry", "System.Windows.Forms" },
notExpectedReferenceNames: new[] { "System.Windows.Presentation", "WindowsFormsIntegration" },
notExpectedReferenceNames: new[] { "WindowsFormsIntegration" },
selfContained);
}

Expand All @@ -899,7 +899,7 @@ public void WPFFrameworkReference(bool selfContained)
TestFrameworkReferenceProfiles(
frameworkReferences: new[] { "Microsoft.WindowsDesktop.App.WPF" },
expectedReferenceNames: new[] { "Microsoft.Win32.Registry", "System.Windows.Presentation" },
notExpectedReferenceNames: new[] { "System.Windows.Forms", "WindowsFormsIntegration" },
notExpectedReferenceNames: new[] { "WindowsFormsIntegration" },
selfContained);
}

Expand Down
Loading