Skip to content

Commit

Permalink
Fix build break because of missing metdata (#8364)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ViktorHofer authored Jan 20, 2022
1 parent cbbbaba commit 1c78e43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,44 @@
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; }

[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<string>(BuildTargetFrameworks.Length);
var bestTargetFrameworkList = new List<ITaskItem>(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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@

<ChooseBestTargetFrameworksTask BuildTargetFrameworks="@(_BuildTargetFrameworkWithTargetOS);$(AdditionalBuildTargetFrameworks)"
SupportedTargetFrameworks="$(TargetFrameworks)"
RuntimeGraph="$(RuntimeGraph)">
RuntimeGraph="$(RuntimeGraph)"
Distinct="true">
<Output TaskParameter="BestTargetFrameworks" ItemName="_BestTargetFramework" />
</ChooseBestTargetFrameworksTask>

Expand Down

0 comments on commit 1c78e43

Please sign in to comment.