build(winui3): post-build target to strip WindowsDesktop.App from runtimeconfig

Adds a StripWindowsDesktopAppFromRuntimeConfig MSBuild target that
runs after GenerateBuildRuntimeConfigurationFiles and rewrites
TeamsISO.runtimeconfig.json to drop the implicit
Microsoft.WindowsDesktop.App framework reference. The .NET 8 SDK adds
that framework for any -windows TFM, but WinUI 3 doesn't use it and
including it in the runtimeconfig contributes to the broken
unpackaged activation path (one of the three suspects in the migration
plan's Phase 3).

Build log confirms the strip on every build:
  Stripped Microsoft.WindowsDesktop.App from .../TeamsISO.runtimeconfig.json

Verified the runtimeconfig now reads:
  { "name": "Microsoft.NETCore.App", "version": "8.0.0" }
  (no WindowsDesktop.App entry)

This didn't resolve the activation dialog on its own, but it's a
required step for any unpackaged WinUI 3 build and the next debugging
session can rule it out as a contributing cause.
This commit is contained in:
Zac Gaetano 2026-05-13 00:21:33 -04:00
parent 2909d8b1d7
commit 2f9f7092ed

View file

@ -107,4 +107,36 @@
<Content Include="Assets\Fonts\JetBrainsMono.ttf" />
</ItemGroup>
<!--
Post-build runtimeconfig patch. .NET 8 SDK adds Microsoft.WindowsDesktop.App
as an implicit framework reference for any -windows target framework moniker.
WinUI 3 doesn't use that framework; including it in runtimeconfig.json
forces the .NET host to resolve and load WindowsDesktop.App at startup,
which contributes to the WindowsAppSDK activation chain on unpackaged
apps. This target rewrites the generated runtimeconfig.json to drop the
WindowsDesktop.App entry so the host loads only NETCore.App.
The target runs after the framework-dependent build copies the
runtimeconfig.json to the output dir, before the bin/Debug/.../win-x64/
directory is final. It's a string-level rewrite — sufficient because the
JSON shape is stable across SDK versions and the WindowsDesktop.App
entry has a deterministic indent + sibling structure.
-->
<Target Name="StripWindowsDesktopAppFromRuntimeConfig" AfterTargets="GenerateBuildRuntimeConfigurationFiles">
<PropertyGroup>
<_RuntimeConfigPath>$(OutDir)$(AssemblyName).runtimeconfig.json</_RuntimeConfigPath>
</PropertyGroup>
<PropertyGroup Condition="Exists('$(_RuntimeConfigPath)')">
<_RuntimeConfigContent>$([System.IO.File]::ReadAllText('$(_RuntimeConfigPath)'))</_RuntimeConfigContent>
<_PatchedContent>$([System.Text.RegularExpressions.Regex]::Replace($(_RuntimeConfigContent), ',\s*\{\s*&quot;name&quot;:\s*&quot;Microsoft\.WindowsDesktop\.App&quot;[^\}]*\}', ''))</_PatchedContent>
</PropertyGroup>
<WriteLinesToFile Condition="Exists('$(_RuntimeConfigPath)') and '$(_RuntimeConfigContent)' != '$(_PatchedContent)'"
File="$(_RuntimeConfigPath)"
Lines="$(_PatchedContent)"
Overwrite="true"/>
<Message Condition="Exists('$(_RuntimeConfigPath)') and '$(_RuntimeConfigContent)' != '$(_PatchedContent)'"
Text="Stripped Microsoft.WindowsDesktop.App from $(_RuntimeConfigPath)"
Importance="high"/>
</Target>
</Project>