From 68e147bb3a61d17f0a4ee518b711b61df1958f2d Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Tue, 6 Jun 2023 23:00:02 +0000 Subject: [PATCH 1/6] ProcessFrameworkReferences if tool packs are required --- .../ProcessFrameworkReferences.cs | 59 ++++++++++++------- ...T.Sdk.FrameworkReferenceResolution.targets | 6 +- .../GivenThatWeWantToRunILLink.cs | 26 ++++++++ 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs index 02d50f672f2f..57030c250b83 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -123,17 +123,14 @@ public class ProcessFrameworkReferences : TaskBase private Version _normalizedTargetFrameworkVersion; - protected override void ExecuteCore() + void AddPacksForFrameworkReferences( + List packagesToDownload, + List runtimeFrameworks, + List targetingPacks, + List runtimePacks, + List unavailableRuntimePacks + ) { - // Perf optimization: If there are no FrameworkReference items, then don't do anything - // (This means that if you don't have any direct framework references, you won't get any transitive ones either - if (FrameworkReferences == null || FrameworkReferences.Length == 0) - { - return; - } - - _normalizedTargetFrameworkVersion = NormalizeVersion(new Version(TargetFrameworkVersion)); - var knownFrameworkReferencesForTargetFramework = KnownFrameworkReferences .Select(item => new KnownFrameworkReference(item)) @@ -156,12 +153,6 @@ protected override void ExecuteCore() var frameworkReferenceMap = FrameworkReferences.ToDictionary(fr => fr.ItemSpec, StringComparer.OrdinalIgnoreCase); - List packagesToDownload = new List(); - List runtimeFrameworks = new List(); - List targetingPacks = new List(); - List runtimePacks = new List(); - List unavailableRuntimePacks = new List(); - HashSet unrecognizedRuntimeIdentifiers = new HashSet(StringComparer.OrdinalIgnoreCase); bool windowsOnlyErrorLogged = false; @@ -363,6 +354,32 @@ var runtimeRequiredByDeployment runtimeFrameworks.Add(runtimeFramework); } } + } + + protected override void ExecuteCore() + { + List packagesToDownload = null; + List runtimeFrameworks = null; + List targetingPacks = null; + List runtimePacks = null; + List unavailableRuntimePacks = null; + + // Perf optimization: If there are no FrameworkReference items, then don't do anything + // (This means that if you don't have any direct framework references, you won't get any transitive ones either + if (FrameworkReferences != null && FrameworkReferences.Length != 0) + { + _normalizedTargetFrameworkVersion = NormalizeVersion(new Version(TargetFrameworkVersion)); + + packagesToDownload = new List(); + runtimeFrameworks = new List(); + targetingPacks = new List(); + runtimePacks = new List(); + unavailableRuntimePacks = new List(); + AddPacksForFrameworkReferences(packagesToDownload, runtimeFrameworks, targetingPacks, runtimePacks, unavailableRuntimePacks); + } + + _normalizedTargetFrameworkVersion ??= NormalizeVersion(new Version(TargetFrameworkVersion)); + packagesToDownload ??= new List(); List implicitPackageReferences = new List(); @@ -413,22 +430,22 @@ var runtimeRequiredByDeployment PackagesToDownload = packagesToDownload.Distinct(new PackageToDownloadComparer()).ToArray(); } - if (runtimeFrameworks.Any()) + if (runtimeFrameworks?.Any() == true) { RuntimeFrameworks = runtimeFrameworks.ToArray(); } - if (targetingPacks.Any()) + if (targetingPacks?.Any() == true) { TargetingPacks = targetingPacks.ToArray(); } - if (runtimePacks.Any()) + if (runtimePacks?.Any() == true) { RuntimePacks = runtimePacks.ToArray(); } - if (unavailableRuntimePacks.Any()) + if (unavailableRuntimePacks?.Any() == true) { UnavailableRuntimePacks = unavailableRuntimePacks.ToArray(); } diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets index d4a5897a1744..45e3b557da7f 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets @@ -60,7 +60,11 @@ Copyright (c) .NET Foundation. All rights reserved. + Condition="'@(FrameworkReference)' != '' Or + '$(_RequiresILLinkPack)' == 'true' Or + '$(PublishReadyToRun)' == 'true' Or + '$(PublishAot)' == 'true' Or + '$(UsingMicrosoftNETSdkWebAssembly)' == 'true'"> Date: Wed, 7 Jun 2023 16:41:10 +0000 Subject: [PATCH 2/6] Fix test failures - Don't publish single-file test for unsupported net472 TFM - Expect single-file to fail adding ILLink pack reference for unupported TFMs - Adjust TFM to check single-file error behavior when publishing library --- .../GivenThatWeWantToPublishAHelloWorldProject.cs | 2 +- .../GivenThatWeWantToPublishASingleFileApp.cs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs index db04eb8aeacd..48dc86f99ea4 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs @@ -1104,7 +1104,7 @@ public void It_publishes_with_implicit_rid_with_rid_specific_properties(string e var testProject = new TestProject() { Name = "PublishImplicitRid", - TargetFrameworks = $"net472;{ToolsetInfo.CurrentTargetFramework}", + TargetFrameworks = ToolsetInfo.CurrentTargetFramework }; testProject.AdditionalProperties.Add("IsPublishable", "false"); var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: executeOptionsAndProperties); diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs index 932f191c433c..f5f6b8f17e89 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs @@ -152,7 +152,7 @@ public void It_errors_when_publishing_single_file_lib() var testProject = new TestProject() { Name = "ClassLib", - TargetFrameworks = "netstandard2.0", + TargetFrameworks = "netcoreapp3.0", IsExe = false, }; @@ -187,7 +187,9 @@ public void It_errors_when_targetting_netstandard() .Should() .Fail() .And - .HaveStdOutContaining(Strings.CanOnlyHaveSingleFileWithNetCoreApp) + // Single-file depends on ILLink analyzers, so fails early when trying + // to add the analyzer reference for an unsupported TFM. + .HaveStdOutContaining(Strings.ILLinkNoValidRuntimePackageError) .And .NotHaveStdOutContaining(Strings.CannotHaveSingleFileWithoutExecutable); } @@ -210,7 +212,9 @@ public void It_errors_when_targetting_netcoreapp_2_x() .Should() .Fail() .And - .HaveStdOutContaining(Strings.PublishSingleFileRequiresVersion30); + // Single-file depends on ILLink analyzers, so fails early when trying + // to add the analyzer reference for an unsupported TFM. + .HaveStdOutContaining(Strings.ILLinkNoValidRuntimePackageError); } [RequiresMSBuildVersionFact("16.8.0")] From c296167d72187dd356ea5f96a386e08f58dec002 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Wed, 7 Jun 2023 18:55:49 +0000 Subject: [PATCH 3/6] Fix another test --- .../Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs index c6ba1b23f527..89a697fb506a 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs @@ -1787,7 +1787,9 @@ public void It_warns_when_targeting_netcoreapp_2_x_illink() .Should() .Pass() .And - .HaveStdOutContaining(Strings.PublishTrimmedRequiresVersion30); + // Fails early when trying to add the illink pack reference for + // an unsupported TFM. + .HaveStdOutContaining(Strings.ILLinkNoValidRuntimePackageError); } private void SetMetadata(XDocument project, string assemblyName, string key, string value) From 81cc9af76ad4637c55b8786e11becf6e3c0ca495 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Wed, 7 Jun 2023 18:58:41 +0000 Subject: [PATCH 4/6] Fix another test --- .../GivenThatWeWantToRunILLink.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs index 89a697fb506a..684c5d80814f 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs @@ -1785,10 +1785,10 @@ public void It_warns_when_targeting_netcoreapp_2_x_illink() publishCommand.Execute($"/p:PublishTrimmed=true") .Should() - .Pass() + .Fail() .And - // Fails early when trying to add the illink pack reference for - // an unsupported TFM. + // Fails early when trying to add the illink pack for an + // unsupported TFM. .HaveStdOutContaining(Strings.ILLinkNoValidRuntimePackageError); } From 0739482fcb3d3dfc3bc2cca840bf6a4bc59c2358 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Thu, 13 Jul 2023 13:41:58 +0000 Subject: [PATCH 5/6] Fix build --- .../ProcessFrameworkReferences.cs | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs index b695652f6b0f..db6794618092 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs @@ -132,7 +132,8 @@ void AddPacksForFrameworkReferences( List runtimeFrameworks, List targetingPacks, List runtimePacks, - List unavailableRuntimePacks + List unavailableRuntimePacks, + out List knownRuntimePacksForTargetFramework ) { var knownFrameworkReferencesForTargetFramework = @@ -144,7 +145,7 @@ List unavailableRuntimePacks // Get known runtime packs from known framework references. // Only use items where the framework reference name matches the RuntimeFrameworkName. // This will filter out known framework references for "profiles", ie WindowsForms and WPF - var knownRuntimePacksForTargetFramework = + knownRuntimePacksForTargetFramework = knownFrameworkReferencesForTargetFramework .Where(kfr => kfr.Name.Equals(kfr.RuntimeFrameworkName, StringComparison.OrdinalIgnoreCase)) .Select(kfr => kfr.ToKnownRuntimePack()) @@ -367,6 +368,7 @@ protected override void ExecuteCore() List targetingPacks = null; List runtimePacks = null; List unavailableRuntimePacks = null; + List knownRuntimePacksForTargetFramework = null; // Perf optimization: If there are no FrameworkReference items, then don't do anything // (This means that if you don't have any direct framework references, you won't get any transitive ones either @@ -379,7 +381,13 @@ protected override void ExecuteCore() targetingPacks = new List(); runtimePacks = new List(); unavailableRuntimePacks = new List(); - AddPacksForFrameworkReferences(packagesToDownload, runtimeFrameworks, targetingPacks, runtimePacks, unavailableRuntimePacks); + AddPacksForFrameworkReferences( + packagesToDownload, + runtimeFrameworks, + targetingPacks, + runtimePacks, + unavailableRuntimePacks, + out knownRuntimePacksForTargetFramework); } _normalizedTargetFrameworkVersion ??= NormalizeVersion(new Version(TargetFrameworkVersion)); @@ -459,22 +467,25 @@ protected override void ExecuteCore() ImplicitPackageReferences = implicitPackageReferences.ToArray(); } - // Determine the known runtime identifier platforms based on all available Microsoft.NETCore.App packs - HashSet knownRuntimeIdentifierPlatforms = new HashSet(StringComparer.OrdinalIgnoreCase); - var netCoreAppPacks = knownRuntimePacksForTargetFramework.Where(krp => krp.Name.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase)); - foreach (KnownRuntimePack netCoreAppPack in netCoreAppPacks) + if (knownRuntimePacksForTargetFramework?.Any() == true) { - foreach (var runtimeIdentifier in netCoreAppPack.RuntimePackRuntimeIdentifiers.Split(';')) + // Determine the known runtime identifier platforms based on all available Microsoft.NETCore.App packs + HashSet knownRuntimeIdentifierPlatforms = new HashSet(StringComparer.OrdinalIgnoreCase); + var netCoreAppPacks = knownRuntimePacksForTargetFramework!.Where(krp => krp.Name.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase)); + foreach (KnownRuntimePack netCoreAppPack in netCoreAppPacks) { - int separator = runtimeIdentifier.LastIndexOf('-'); - string platform = separator < 0 ? runtimeIdentifier : runtimeIdentifier.Substring(0, separator); - knownRuntimeIdentifierPlatforms.Add(platform); + foreach (var runtimeIdentifier in netCoreAppPack.RuntimePackRuntimeIdentifiers.Split(';')) + { + int separator = runtimeIdentifier.LastIndexOf('-'); + string platform = separator < 0 ? runtimeIdentifier : runtimeIdentifier.Substring(0, separator); + knownRuntimeIdentifierPlatforms.Add(platform); + } } - } - if (knownRuntimeIdentifierPlatforms.Count > 0) - { - KnownRuntimeIdentifierPlatforms = knownRuntimeIdentifierPlatforms.ToArray(); + if (knownRuntimeIdentifierPlatforms.Count > 0) + { + KnownRuntimeIdentifierPlatforms = knownRuntimeIdentifierPlatforms.ToArray(); + } } } From 1a136dff689f32771f316f3a192818a56c32beed Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Thu, 13 Jul 2023 13:58:07 +0000 Subject: [PATCH 6/6] Limit scope of change to illink pack --- ...Microsoft.NET.Sdk.FrameworkReferenceResolution.targets | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets index dd987035f783..345bf8a69a9c 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets @@ -1,4 +1,4 @@ -