From 291d623d2dadbe2177b0d0b3ade0b72919b344d3 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 29 May 2026 13:32:35 -0700 Subject: [PATCH 01/12] Skip full Build for AOT publish, use Compile instead When publishing with PublishAot=true, the implicit Build step was producing a full self-contained managed deployment (apphost, DLLs, deps.json, runtimeconfig, runtime pack files) in the output directory. This output is completely unused by the AOT pipeline, which reads from @(IntermediateAssembly) in obj\, not from bin\ output. This change introduces _PublishAotBuildAlternative that runs Compile (plus resource and satellite assembly targets) instead of full Build for AOT publish. This eliminates the confusing managed artifacts from the output directory while preserving all AOT publish functionality. The target list for AOT publish is: - BuildOnlySettings: configures build-time settings - PrepareForBuild: creates output directories - PrepareResources: compiles .resx to .resources files - Compile: produces IL assembly in obj\ (includes ResolveReferences) - CreateSatelliteAssemblies: generates culture-specific resource DLLs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../targets/Microsoft.NET.Publish.targets | 21 ++- .../GivenThatWeWantToPublishAnAotApp.cs | 126 ++++++++++++++++++ 2 files changed, 145 insertions(+), 2 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets index 95703653526e..e331824d2388 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets @@ -148,19 +148,36 @@ Copyright (c) .NET Foundation. All rights reserved. <_PublishNoBuildAlternativeDependsOn>$(_BeforePublishNoBuildTargets);$(_CorePublishTargets) + + + <_BeforePublishAotBuildTargets Condition="'$(PublishAot)' == 'true'"> + BuildOnlySettings; + PrepareForBuild; + PrepareResources; + Compile; + CreateSatelliteAssemblies; + + <_PublishAotBuildAlternativeDependsOn>$(_BeforePublishAotBuildTargets);$(_CorePublishTargets) + + + DependsOnTargets="_PublishBuildAlternative;_PublishAotBuildAlternative;_PublishNoBuildAlternative" > - <_BeforePublishAotBuildTargets Condition="'$(PublishAot)' == 'true'"> + pack files) in the output directory that would confuse users and automation. + Set UseAotOptimizedPublish=false to restore the previous behavior of running full Build. --> + true + <_BeforePublishAotBuildTargets Condition="'$(UseAotOptimizedPublish)' == 'true'"> BuildOnlySettings; PrepareForBuild; PrepareResources; @@ -164,11 +166,11 @@ Copyright (c) .NET Foundation. All rights reserved. f.Equals($"{projectName}.runtimeconfig.json", StringComparison.OrdinalIgnoreCase), "the output directory should not contain runtimeconfig.json from Build"); + // Also verify no runtime pack assemblies leaked from a self-contained Build + managedBuildArtifacts.Should().NotContain(f => f.Equals("System.Private.CoreLib.dll", StringComparison.OrdinalIgnoreCase), + "the output directory should not contain runtime pack assemblies from Build"); + managedBuildArtifacts.Should().NotContain(f => f.Equals("coreclr.dll", StringComparison.OrdinalIgnoreCase) || + f.Equals("libcoreclr.so", StringComparison.OrdinalIgnoreCase) || + f.Equals("libcoreclr.dylib", StringComparison.OrdinalIgnoreCase), + "the output directory should not contain the CoreCLR runtime from Build"); } [RequiresMSBuildVersionFact("17.0.0.32901")] From bb44d4df57c71373b8641caed0fde30dd004e1c8 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Wed, 8 Jul 2026 16:05:54 -0700 Subject: [PATCH 03/12] Update MSBuild version attribute for tests --- .../GivenThatWeWantToPublishAnAotApp.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs index f88a132bc8f8..e7b2806ab3a8 100644 --- a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs +++ b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs @@ -1390,7 +1390,8 @@ private static bool IsNativeImage(string path) } } - [RequiresMSBuildVersionFact("17.0.0.32901")] + [TestMethod] + [RequiresMSBuildVersion("17.0.0.32901")] public void NativeAot_publish_does_not_produce_managed_build_output() { // AOT publish should not create managed build artifacts (apphost, .dll, .deps.json, @@ -1448,7 +1449,8 @@ public void NativeAot_publish_does_not_produce_managed_build_output() "the output directory should not contain the CoreCLR runtime from Build"); } - [RequiresMSBuildVersionFact("17.0.0.32901")] + [TestMethod] + [RequiresMSBuildVersion("17.0.0.32901")] public void NativeAot_publish_with_project_reference_succeeds() { // Verify that AOT publish still builds project references correctly via From eee5d46198d3143b40351b5b8da95a3f667e5185 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 10 Jul 2026 10:50:41 -0700 Subject: [PATCH 04/12] Generate static web assets manifest in AOT-optimized publish path The AOT-optimized publish path (UseAotOptimizedPublish) runs Compile instead of full Build, which skips PrepareForRun and therefore GenerateStaticWebAssetsManifest. Web SDK projects publishing with PublishAot then failed because Microsoft.NET.Sdk.StaticWebAssets.References.targets expects the build manifest (staticwebassets.build.json) to exist during publish. Add an _AdditionalAotBuildAlternativeTargets extension point to the AOT build-alternative and have the Static Web Assets SDK contribute GenerateStaticWebAssetsManifest to it. This fixes the failing TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps and Publish_WithJson tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Targets/Microsoft.NET.Sdk.StaticWebAssets.targets | 7 +++++++ .../targets/Microsoft.NET.Publish.targets | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets b/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets index 5956ae0470de..a01086fea067 100644 --- a/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets +++ b/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets @@ -303,6 +303,13 @@ Copyright (c) .NET Foundation. All rights reserved. $(GenerateStaticWebAssetsManifestDependsOn);ResolveBuildStaticWebAssets + + <_AdditionalAotBuildAlternativeTargets>$(_AdditionalAotBuildAlternativeTargets);GenerateStaticWebAssetsManifest + + DependsOnTargets="$(_BeforePublishAotBuildTargets);$(_AdditionalAotBuildAlternativeTargets);$(_CorePublishTargets)" /> Date: Fri, 10 Jul 2026 14:17:27 -0700 Subject: [PATCH 05/12] Record properties before Publish in Web AOT trimming test TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps recorded properties using the default AfterBuild anchor. The AOT-optimized publish path runs Compile instead of full Build, so AfterBuild never runs and PropertyValues.txt was never written. Record before Publish (which always runs), matching the pattern already used by the AOT tests in GivenThatWeWantToPublishAnAotApp.cs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs b/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs index bab3646ab117..d5fc1a1d0745 100644 --- a/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs +++ b/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs @@ -84,6 +84,9 @@ public void TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps(string targetFra var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName); testProject.RecordProperties("NETCoreSdkPortableRuntimeIdentifier"); + // AOT publish runs Compile instead of full Build, so the AfterBuild target (the default + // anchor for recording properties) never runs. Record before Publish, which always runs. + testProject.RecordPropertiesBeforeTarget("Publish"); testProject.AdditionalProperties["PublishAot"] = "true"; testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "true"; testProject.PropertiesToRecord.Add("PublishTrimmed"); From 03326fa01907bc8db71a23be03572ec3b95a7799 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Mon, 13 Jul 2026 14:02:50 -0700 Subject: [PATCH 06/12] Record properties before PrepareForPublish to capture pre-trimming TrimMode Recording before Publish captured TrimMode after PrepareForILLink defaulted it to "full", failing the TrimMode == "" assertion. PrepareForPublish runs after Compile but before publish-time trimming, matching the pre-trimming values the original AfterBuild anchor observed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs b/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs index d5fc1a1d0745..53a020a2efea 100644 --- a/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs +++ b/test/Microsoft.NET.Sdk.Web.Tests/PublishTests.cs @@ -85,8 +85,10 @@ public void TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps(string targetFra var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName); testProject.RecordProperties("NETCoreSdkPortableRuntimeIdentifier"); // AOT publish runs Compile instead of full Build, so the AfterBuild target (the default - // anchor for recording properties) never runs. Record before Publish, which always runs. - testProject.RecordPropertiesBeforeTarget("Publish"); + // anchor for recording properties) never runs. Record before PrepareForPublish, which + // runs after Compile but before publish-time trimming (PrepareForILLink) defaults + // TrimMode to "full" - matching the pre-trimming values the original AfterBuild anchor saw. + testProject.RecordPropertiesBeforeTarget("PrepareForPublish"); testProject.AdditionalProperties["PublishAot"] = "true"; testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "true"; testProject.PropertiesToRecord.Add("PublishTrimmed"); From 2726fa22e99075e14914e9acba62197244198dbc Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Tue, 18 Aug 2026 11:16:28 -0700 Subject: [PATCH 07/12] Address AOT publish review feedback Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../general/publish-build-optimization.md | 4 +- .../targets/Microsoft.NET.Publish.targets | 6 +- .../GivenThatWeWantToPublishAnAotApp.cs | 65 ++++++++++++------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/documentation/general/publish-build-optimization.md b/documentation/general/publish-build-optimization.md index 2e2d6b301c4b..b90c08ab49e4 100644 --- a/documentation/general/publish-build-optimization.md +++ b/documentation/general/publish-build-optimization.md @@ -31,7 +31,7 @@ Key insight: All post-processing steps read from `@(IntermediateAssembly)` (obj) ### PublishAot (Native AOT) -**Status: Optimized (this PR)** +**Status: Optimized** - `IlcCompile` reads `@(IntermediateAssembly)` from `obj\` and produces a native binary - The full `Build` was running a self-contained deployment to `bin\\\\`, @@ -91,7 +91,7 @@ applies when the outermost mode is optimized: ### What Changed -Starting in .NET 10, `dotnet publish` with `PublishAot=true` no longer runs a full `Build` +Starting in .NET 11, `dotnet publish` with `PublishAot=true` no longer runs a full `Build` before publish. Instead, it runs only `Compile` (and resource/satellite assembly targets). ### Impact diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets index 5e7337ec6eab..5aa3f0faf382 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets @@ -170,10 +170,8 @@ Copyright (c) .NET Foundation. All rights reserved. + It runs after Compile so contributors can rely on the intermediate assembly, and before the core + publish targets that consume its outputs. --> diff --git a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs index e7b2806ab3a8..6912f91e8136 100644 --- a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs +++ b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs @@ -1390,6 +1390,44 @@ private static bool IsNativeImage(string path) } } + [TestMethod] + public void Aot_optimized_publish_targets_are_reviewed_when_CoreBuildDependsOn_changes() + { + var testProject = CreateHelloWorldTestProject(ToolsetInfo.CurrentTargetFramework, "CoreBuildDependsOn", true); + testProject.RecordProperties("CoreBuildDependsOn"); + var testAsset = TestAssetsManager.CreateTestProject(testProject); + + new BuildCommand(testAsset) + .Execute() + .Should().Pass(); + + var coreBuildDependsOn = testProject + .GetPropertyValues(testAsset.TestRoot, ToolsetInfo.CurrentTargetFramework)["CoreBuildDependsOn"] + .Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + + coreBuildDependsOn.Should().Equal( + "_CheckForBuildWithNoBuild", + "BuildOnlySettings", + "PrepareForBuild", + "PreBuildEvent", + "ResolveReferences", + "PrepareResources", + "ResolveKeySource", + "Compile", + "ExportWindowsMDFile", + "UnmanagedUnregistration", + "GenerateSerializationAssemblies", + "CreateSatelliteAssemblies", + "GenerateManifests", + "GetTargetPath", + "PrepareForRun", + "UnmanagedRegistration", + "IncrementalClean", + "PostBuildEvent", + "GenerateBuildDependencyFile", + "GenerateBuildRuntimeConfigurationFiles"); + } + [TestMethod] [RequiresMSBuildVersion("17.0.0.32901")] public void NativeAot_publish_does_not_produce_managed_build_output() @@ -1412,7 +1450,7 @@ public void NativeAot_publish_does_not_produce_managed_build_output() var publishCommand = new PublishCommand(testAsset); publishCommand - .Execute("/p:UseCurrentRuntimeIdentifier=true", "/p:SelfContained=true") + .Execute() .Should().Pass(); var buildProperties = testProject.GetPropertyValues(testAsset.TestRoot, ToolsetInfo.CurrentTargetFramework); @@ -1424,29 +1462,10 @@ public void NativeAot_publish_does_not_produce_managed_build_output() File.Exists(publishedExe).Should().BeTrue("the AOT-compiled native binary should exist in the publish directory"); IsNativeImage(publishedExe).Should().BeTrue("the published binary should be a native image"); - // The output directory (parent of publish\) should NOT contain managed build artifacts. - // It may contain subdirectories like native\ and publish\, but should not have managed - // apphost, .dll, .deps.json, .runtimeconfig.json, or runtime pack files. + // The output directory (parent of publish\) should only contain subdirectories. var outputDirectory = Path.GetDirectoryName(publishDirectory); - var managedBuildArtifacts = Directory.GetFiles(outputDirectory) - .Select(Path.GetFileName) - .ToList(); - - managedBuildArtifacts.Should().NotContain(f => f.Equals($"{projectName}{Constants.ExeSuffix}", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain a managed apphost executable from Build"); - managedBuildArtifacts.Should().NotContain(f => f.Equals($"{projectName}.dll", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain a managed assembly from Build"); - managedBuildArtifacts.Should().NotContain(f => f.Equals($"{projectName}.deps.json", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain deps.json from Build"); - managedBuildArtifacts.Should().NotContain(f => f.Equals($"{projectName}.runtimeconfig.json", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain runtimeconfig.json from Build"); - // Also verify no runtime pack assemblies leaked from a self-contained Build - managedBuildArtifacts.Should().NotContain(f => f.Equals("System.Private.CoreLib.dll", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain runtime pack assemblies from Build"); - managedBuildArtifacts.Should().NotContain(f => f.Equals("coreclr.dll", StringComparison.OrdinalIgnoreCase) || - f.Equals("libcoreclr.so", StringComparison.OrdinalIgnoreCase) || - f.Equals("libcoreclr.dylib", StringComparison.OrdinalIgnoreCase), - "the output directory should not contain the CoreCLR runtime from Build"); + Directory.GetFiles(outputDirectory).Should().BeEmpty( + "the AOT-optimized publish should not produce Build output files"); } [TestMethod] From 6aa48ddc013f1fa87314d66d45574ca547c8cef8 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Tue, 18 Aug 2026 14:35:47 -0700 Subject: [PATCH 08/12] Record CoreBuildDependsOn in inner build Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 56297b38-2478-484c-aaa5-f0c06ab91e34 --- .../GivenThatWeWantToPublishAnAotApp.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs index 6912f91e8136..92890086b65b 100644 --- a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs +++ b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs @@ -1395,6 +1395,7 @@ public void Aot_optimized_publish_targets_are_reviewed_when_CoreBuildDependsOn_c { var testProject = CreateHelloWorldTestProject(ToolsetInfo.CurrentTargetFramework, "CoreBuildDependsOn", true); testProject.RecordProperties("CoreBuildDependsOn"); + testProject.RecordPropertiesBeforeTarget("Compile"); var testAsset = TestAssetsManager.CreateTestProject(testProject); new BuildCommand(testAsset) From e631fd3a854fa77ae3d6b5c0d7c735f0b928768a Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Wed, 19 Aug 2026 15:07:26 -0700 Subject: [PATCH 09/12] Read CoreBuildDependsOn as MSBuild values Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 56297b38-2478-484c-aaa5-f0c06ab91e34 --- .../GivenThatWeWantToPublishAnAotApp.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs index 92890086b65b..8283538c70ef 100644 --- a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs +++ b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs @@ -1394,19 +1394,18 @@ private static bool IsNativeImage(string path) public void Aot_optimized_publish_targets_are_reviewed_when_CoreBuildDependsOn_changes() { var testProject = CreateHelloWorldTestProject(ToolsetInfo.CurrentTargetFramework, "CoreBuildDependsOn", true); - testProject.RecordProperties("CoreBuildDependsOn"); - testProject.RecordPropertiesBeforeTarget("Compile"); var testAsset = TestAssetsManager.CreateTestProject(testProject); - new BuildCommand(testAsset) + var getValuesCommand = new GetValuesCommand(testAsset, "CoreBuildDependsOn") + { + ShouldCompile = false + }; + + getValuesCommand .Execute() .Should().Pass(); - var coreBuildDependsOn = testProject - .GetPropertyValues(testAsset.TestRoot, ToolsetInfo.CurrentTargetFramework)["CoreBuildDependsOn"] - .Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); - - coreBuildDependsOn.Should().Equal( + getValuesCommand.GetValues().Should().Equal( "_CheckForBuildWithNoBuild", "BuildOnlySettings", "PrepareForBuild", From dc80b915b4e2813b3992e15d46c53e84886ae4be Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 21 Aug 2026 10:43:17 -0700 Subject: [PATCH 10/12] Restrict optimized publish to Native AOT Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 56297b38-2478-484c-aaa5-f0c06ab91e34 --- .../general/publish-build-optimization.md | 4 +++- .../targets/Microsoft.NET.Publish.targets | 7 ++++--- .../GivenThatWeWantToPublishAnAotApp.cs | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/documentation/general/publish-build-optimization.md b/documentation/general/publish-build-optimization.md index b90c08ab49e4..7d3227a9bc62 100644 --- a/documentation/general/publish-build-optimization.md +++ b/documentation/general/publish-build-optimization.md @@ -39,6 +39,7 @@ Key insight: All post-processing steps read from `@(IntermediateAssembly)` (obj) - **None of these files are used** by the AOT pipeline - **Optimization**: Replace `Build` with `Compile` (plus resource/satellite targets) - **Opt-out**: Set `UseAotOptimizedPublish=false` to restore full Build behavior +- `UseAotOptimizedPublish` has no effect when `PublishAot` is not enabled Target chain for optimized AOT publish: ``` @@ -101,7 +102,8 @@ before publish. Instead, it runs only `Compile` (and resource/satellite assembly `BeforeTargets="Build"` / `AfterTargets="Build"`, they will not run. - **Workaround**: Attach your targets to `BeforeTargets="Publish"` / `AfterTargets="Publish"`, or to `BeforeTargets="Compile"` / `AfterTargets="Compile"` instead. -- **PostBuildEvent**: Will not execute during AOT publish (consistent with `--no-build` behavior). +- **PreBuildEvent / PostBuildEvent**: These will not execute during AOT publish (consistent + with `--no-build` behavior). - **Third-party NuGet Build hooks**: Targets from NuGet packages that hook into `Build` will be skipped (consistent with `--no-build` behavior). - **Output directory**: `bin\\\\` will no longer contain managed apphost, diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets index 5aa3f0faf382..7b5ff775e542 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets @@ -155,7 +155,8 @@ Copyright (c) .NET Foundation. All rights reserved. pack files) in the output directory that would confuse users and automation. Set UseAotOptimizedPublish=false to restore the previous behavior of running full Build. --> true - <_BeforePublishAotBuildTargets Condition="'$(UseAotOptimizedPublish)' == 'true'"> + <_UseAotOptimizedPublish Condition="'$(PublishAot)' == 'true' and '$(UseAotOptimizedPublish)' == 'true'">true + <_BeforePublishAotBuildTargets Condition="'$(_UseAotOptimizedPublish)' == 'true'"> BuildOnlySettings; PrepareForBuild; PrepareResources; @@ -165,7 +166,7 @@ Copyright (c) .NET Foundation. All rights reserved. + { + project.Root.Add(XElement.Parse(""" + + + + """)); + }); + + new PublishCommand(testAsset) + .Execute() + .Should().Pass() + .And.HaveStdOutContaining("Build target executed"); + } + [TestMethod] [RequiresMSBuildVersion("17.0.0.32901")] public void NativeAot_publish_does_not_produce_managed_build_output() From bfcf675108cc5576d7b2a80dd2971da4b0a5593e Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Mon, 24 Aug 2026 14:22:51 -0700 Subject: [PATCH 11/12] Generalize optimized publish setting Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 56297b38-2478-484c-aaa5-f0c06ab91e34 --- .../general/publish-build-optimization.md | 12 +++++----- .../Microsoft.NET.Sdk.StaticWebAssets.targets | 6 ++--- .../targets/Microsoft.NET.Publish.targets | 23 ++++++++++--------- .../GivenThatWeWantToPublishAnAotApp.cs | 6 ++--- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/documentation/general/publish-build-optimization.md b/documentation/general/publish-build-optimization.md index 7d3227a9bc62..efafc791930d 100644 --- a/documentation/general/publish-build-optimization.md +++ b/documentation/general/publish-build-optimization.md @@ -38,8 +38,8 @@ Key insight: All post-processing steps read from `@(IntermediateAssembly)` (obj) including apphost, managed DLLs, deps.json, runtimeconfig.json, and runtime pack files - **None of these files are used** by the AOT pipeline - **Optimization**: Replace `Build` with `Compile` (plus resource/satellite targets) -- **Opt-out**: Set `UseAotOptimizedPublish=false` to restore full Build behavior -- `UseAotOptimizedPublish` has no effect when `PublishAot` is not enabled +- **Opt-out**: Set `UseOptimizedPublish=false` to restore full Build behavior +- `UseOptimizedPublish` currently has no effect when `PublishAot` is not enabled Target chain for optimized AOT publish: ``` @@ -116,14 +116,14 @@ To restore the previous behavior of running a full `Build` before AOT publish, s ```xml - false + false ``` Or pass it on the command line: ``` -dotnet publish /p:UseAotOptimizedPublish=false +dotnet publish /p:UseOptimizedPublish=false ``` ## Future Work @@ -133,7 +133,7 @@ The same optimization could be applied to `PublishTrimmed`, `PublishReadyToRun`, `@(IntermediateAssembly)` and resolved references rather than `Build` output. Each mode would need: -1. Its own condition check (e.g., `UseTrimmingOptimizedPublish`) +1. Compatibility analysis for Build hooks used by projects and NuGet packages 2. The same target chain: `BuildOnlySettings → PrepareForBuild → PrepareResources → Compile → CreateSatelliteAssemblies` -3. Tests verifying no managed artifacts in the output directory +3. Tests verifying the expected build and publish outputs 4. Documentation of the breaking change for that mode diff --git a/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets b/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets index a01086fea067..b72da2d4f3c2 100644 --- a/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets +++ b/src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets @@ -303,12 +303,12 @@ Copyright (c) .NET Foundation. All rights reserved. $(GenerateStaticWebAssetsManifestDependsOn);ResolveBuildStaticWebAssets - - <_AdditionalAotBuildAlternativeTargets>$(_AdditionalAotBuildAlternativeTargets);GenerateStaticWebAssetsManifest + <_AdditionalOptimizedPublishTargets>$(_AdditionalOptimizedPublishTargets);GenerateStaticWebAssetsManifest - true - <_UseAotOptimizedPublish Condition="'$(PublishAot)' == 'true' and '$(UseAotOptimizedPublish)' == 'true'">true - <_BeforePublishAotBuildTargets Condition="'$(_UseAotOptimizedPublish)' == 'true'"> + Set UseOptimizedPublish=false to restore the previous behavior of running full Build. --> + true + + <_UseOptimizedPublish Condition="'$(PublishAot)' == 'true' and '$(UseOptimizedPublish)' == 'true'">true + <_BeforeOptimizedPublishTargets Condition="'$(_UseOptimizedPublish)' == 'true'"> BuildOnlySettings; PrepareForBuild; PrepareResources; Compile; CreateSatelliteAssemblies; - + - - + + DependsOnTargets="_PublishBuildAlternative;_PublishOptimizedBuildAlternative;_PublishNoBuildAlternative" >