From 78030f8f00701220f4b5c62faa9bff4804394a81 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 13 Sep 2023 19:01:24 +0200 Subject: [PATCH 1/3] Add escape hatch for PublishAot without RuntimeIdentifier. Fixes #33414. This adds an escape hatch for a sanity check when trying to use PublishAot without a RuntimeIdentifier, because the check is not valid when building universal apps for macOS and Mac Catalyst (the netX-macos and netX-maccatalyst target frameworks). When building such an app, the project file will set RuntimeIdentifiers (plural): net8.0-macos osx-x64;osx-arm and then during the build, the macOS SDK will run two inner builds, with RuntimeIdentifiers unset, and RuntimeIdentifier set to each of the rids (and at the end merge the result into a single app). The problem is that the outer build, where RuntimeIdentifiers is set, but RuntimeIdentifier isn't, triggers the sanity check, and that fails the build. I attempted to unset PublishAot for the outer build, but that doesn't work for two reasons: * It can't be unset if customers pass /p:PublishAot=true on the command line. * The package restore won't restore the NativeAOT packages nor include them in the build, and thus effectively NativeAOT is turned off for the inner builds as well, even if we set PublishAot=true for them. So instead add an escape hatch for the sanity check. Fixes https://github.com/dotnet/sdk/issues/33414. --- ...oft.NET.RuntimeIdentifierInference.targets | 2 +- .../RuntimeIdentifiersTests.cs | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets index 72880b4b84ea..91eb3012aaee 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets @@ -221,7 +221,7 @@ Copyright (c) .NET Foundation. All rights reserved. ResourceName="ImplicitRuntimeIdentifierResolutionForPublishPropertyFailed" FormatArguments="PublishSingleFile"/> - diff --git a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs index ff4103c46fd3..6b5870eef000 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs @@ -380,5 +380,48 @@ public void PublishSuccessfullyWithRIDRequiringPropertyAndRuntimeIdentifiersNoRu .Should() .Pass(); } + + [Fact] + public void PublishFailsWithPublishAotAndNoRuntimeIdentifier() + { + var targetFramework = ToolsetInfo.CurrentTargetFramework; + var testProject = new TestProject() + { + IsExe = true, + TargetFrameworks = targetFramework + }; + + testProject.AdditionalProperties["PublishAot"] = "true"; + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); + publishCommand + .Execute() + .Should() + .Fail() + .And + .HaveStdOutContaining("NETSDK1191"); + } + + [Fact] + public void PublishSuccessfullyWithPublishAotAndNoRuntimeIdentifierWithEscapeHatch() + { + var targetFramework = ToolsetInfo.CurrentTargetFramework; + var testProject = new TestProject() + { + IsExe = true, + TargetFrameworks = targetFramework + }; + + testProject.AdditionalProperties["PublishAot"] = "true"; + testProject.AdditionalProperties["AllowPublishAotWithoutRuntimeIdentifier"] = "true"; + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); + publishCommand + .Execute() + .Should() + .Pass(); + } } } From 557947173e11f8e0d18745138464d8a7bae6c593 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 14 Sep 2023 10:10:26 +0200 Subject: [PATCH 2/3] Avoid inferring the runtime identifier. --- .../Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs index 6b5870eef000..d96a186b37f1 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs @@ -392,6 +392,7 @@ public void PublishFailsWithPublishAotAndNoRuntimeIdentifier() }; testProject.AdditionalProperties["PublishAot"] = "true"; + testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "false"; // Make sure the RuntimeIdentifier isn't inferred var testAsset = _testAssetsManager.CreateTestProject(testProject); var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); @@ -415,6 +416,7 @@ public void PublishSuccessfullyWithPublishAotAndNoRuntimeIdentifierWithEscapeHat testProject.AdditionalProperties["PublishAot"] = "true"; testProject.AdditionalProperties["AllowPublishAotWithoutRuntimeIdentifier"] = "true"; + testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "false"; // Make sure the RuntimeIdentifier isn't inferred var testAsset = _testAssetsManager.CreateTestProject(testProject); var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); From 0111b2f7c19cc892a900e2f06f7d68779b8ff9cf Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 14 Sep 2023 13:26:59 +0200 Subject: [PATCH 3/3] Remove tests, they don't do anything useful. These new properties requires a downstream SDK to do the right thing, they won't work out-of-the box. --- .../RuntimeIdentifiersTests.cs | 45 ------------------- 1 file changed, 45 deletions(-) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs index d96a186b37f1..ff4103c46fd3 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs @@ -380,50 +380,5 @@ public void PublishSuccessfullyWithRIDRequiringPropertyAndRuntimeIdentifiersNoRu .Should() .Pass(); } - - [Fact] - public void PublishFailsWithPublishAotAndNoRuntimeIdentifier() - { - var targetFramework = ToolsetInfo.CurrentTargetFramework; - var testProject = new TestProject() - { - IsExe = true, - TargetFrameworks = targetFramework - }; - - testProject.AdditionalProperties["PublishAot"] = "true"; - testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "false"; // Make sure the RuntimeIdentifier isn't inferred - var testAsset = _testAssetsManager.CreateTestProject(testProject); - - var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); - publishCommand - .Execute() - .Should() - .Fail() - .And - .HaveStdOutContaining("NETSDK1191"); - } - - [Fact] - public void PublishSuccessfullyWithPublishAotAndNoRuntimeIdentifierWithEscapeHatch() - { - var targetFramework = ToolsetInfo.CurrentTargetFramework; - var testProject = new TestProject() - { - IsExe = true, - TargetFrameworks = targetFramework - }; - - testProject.AdditionalProperties["PublishAot"] = "true"; - testProject.AdditionalProperties["AllowPublishAotWithoutRuntimeIdentifier"] = "true"; - testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "false"; // Make sure the RuntimeIdentifier isn't inferred - var testAsset = _testAssetsManager.CreateTestProject(testProject); - - var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); - publishCommand - .Execute() - .Should() - .Pass(); - } } }