From 1c78e4313e8dc240587f95938e8dcb298d1379d4 Mon Sep 17 00:00:00 2001 From: Viktor Hofer Date: Thu, 20 Jan 2022 10:09:14 +0100 Subject: [PATCH] Fix build break because of missing metdata (#8364) * Fix build break because of missing metdata In the previous change I didn't notice that the binplacing logic depends on metadata passing in being preserved in the items being returned. Because of that, I revert the collection change from List to HashSet and instead manually iterate through the list. The binplacing logic depends on duplicates being allowed versus the inner build project logic explicitly doesn't want duplicates to avoid unnecessary inner builds, hence adding a switch for it. * Update Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets * Update ChooseBestTargetFrameworksTask.cs --- .../src/ChooseBestTargetFrameworksTask.cs | 24 +++++++++++-------- ...et.Build.Tasks.TargetFramework.Sdk.targets | 3 ++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/ChooseBestTargetFrameworksTask.cs b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/ChooseBestTargetFrameworksTask.cs index 0f5eaf8aa29..f708e98f2ff 100644 --- a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/ChooseBestTargetFrameworksTask.cs +++ b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/ChooseBestTargetFrameworksTask.cs @@ -4,13 +4,14 @@ using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using System.Collections.Generic; +using System.Linq; namespace Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk { public class ChooseBestTargetFrameworksTask : BuildTask { [Required] - public string[] BuildTargetFrameworks { get; set; } + public ITaskItem[] BuildTargetFrameworks { get; set; } [Required] public string RuntimeGraph { get; set; } @@ -18,26 +19,29 @@ public class ChooseBestTargetFrameworksTask : BuildTask [Required] public string[] SupportedTargetFrameworks { get; set; } + // Returns distinct items only. Compares the include values. Metadata is ignored. + public bool Distinct { get; set; } + [Output] - public string[] BestTargetFrameworks { get; set; } + public ITaskItem[] BestTargetFrameworks { get; set; } public override bool Execute() { - var bestTargetFrameworkList = new HashSet(BuildTargetFrameworks.Length); + var bestTargetFrameworkList = new List(BuildTargetFrameworks.Length); var targetframeworkResolver = new TargetFrameworkResolver(RuntimeGraph); - foreach (string buildTargetFramework in BuildTargetFrameworks) + foreach (ITaskItem buildTargetFramework in BuildTargetFrameworks) { - string bestTargetFramework = targetframeworkResolver.GetBestSupportedTargetFramework(SupportedTargetFrameworks, buildTargetFramework); - if (bestTargetFramework != null) + string bestTargetFramework = targetframeworkResolver.GetBestSupportedTargetFramework(SupportedTargetFrameworks, buildTargetFramework.ItemSpec); + if (bestTargetFramework != null && (!Distinct || !bestTargetFrameworkList.Any(b => b.ItemSpec == bestTargetFramework))) { - bestTargetFrameworkList.Add(bestTargetFramework); + var item = new TaskItem(bestTargetFramework); + buildTargetFramework.CopyMetadataTo(item); + bestTargetFrameworkList.Add(item); } } - BestTargetFrameworks = new string[bestTargetFrameworkList.Count]; - bestTargetFrameworkList.CopyTo(BestTargetFrameworks); - + BestTargetFrameworks = bestTargetFrameworkList.ToArray(); return !Log.HasLoggedErrors; } } diff --git a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets index 00e27e09c67..4aa1555c3bb 100644 --- a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets +++ b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets @@ -46,7 +46,8 @@ + RuntimeGraph="$(RuntimeGraph)" + Distinct="true">