Skip to content

Commit 1eae1d9

Browse files
moooyoYu Leng (from Dev Box)
andauthored
[AOT] Remove rd.xml from CmdPal (#41031)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Actually, we don't need to use rd.xml to preserve type. We can add attribute to do the same things. Tested in this build: https://microsoft.visualstudio.com/Dart/_build/results?buildId=127875892&view=artifacts&pathAsName=false&type=publishedArtifacts <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #40633 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Yu Leng (from Dev Box) <[email protected]>
1 parent bc134b3 commit 1eae1d9

File tree

9 files changed

+54
-27
lines changed

9 files changed

+54
-27
lines changed

src/modules/cmdpal/Microsoft.CmdPal.Core.ViewModels/CommandContextItemViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.CmdPal.Core.ViewModels.Models;
67
using Microsoft.CommandPalette.Extensions;
8+
using Microsoft.CommandPalette.Extensions.Toolkit;
79

810
namespace Microsoft.CmdPal.Core.ViewModels;
911

12+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
1013
public partial class CommandContextItemViewModel(ICommandContextItem contextItem, WeakReference<IPageContext> context) : CommandItemViewModel(new(contextItem), context), IContextItemViewModel
1114
{
1215
private readonly KeyChord nullKeyChord = new(0, 0, 0);

src/modules/cmdpal/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.CmdPal.Core.ViewModels.Messages;
67
using Microsoft.CmdPal.Core.ViewModels.Models;
78
using Microsoft.CommandPalette.Extensions;
89
using Microsoft.CommandPalette.Extensions.Toolkit;
910

1011
namespace Microsoft.CmdPal.Core.ViewModels;
1112

13+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
1214
public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBarContext
1315
{
1416
public ExtensionObject<ICommandItem> Model => _commandItemModel;

src/modules/cmdpal/Microsoft.CmdPal.Core.ViewModels/IContextItemViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Linq;
89
using System.Text;
910
using System.Threading.Tasks;
1011

1112
namespace Microsoft.CmdPal.Core.ViewModels;
1213

14+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
1315
public interface IContextItemViewModel
1416
{
1517
}

src/modules/cmdpal/Microsoft.CmdPal.Core.ViewModels/SeparatorContextItemViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.CmdPal.Core.ViewModels;
67
using Microsoft.CommandPalette.Extensions;
78

89
namespace Microsoft.CmdPal.Core.ViewModels;
910

11+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
1012
public partial class SeparatorContextItemViewModel() : IContextItemViewModel, ISeparatorContextItem
1113
{
1214
}

src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public App()
6464

6565
this.InitializeComponent();
6666

67+
// Ensure types used in XAML are preserved for AOT compilation
68+
TypePreservation.PreserveTypes();
69+
6770
NativeEventWaiter.WaitForEventLoop(
6871
"Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd", () =>
6972
{

src/modules/cmdpal/Microsoft.CmdPal.UI/Converters/ContextItemTemplateSelector.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.CmdPal.Core.ViewModels;
67
using Microsoft.UI.Xaml;
78
using Microsoft.UI.Xaml.Controls;
@@ -10,6 +11,7 @@
1011

1112
namespace Microsoft.CmdPal.UI;
1213

14+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
1315
internal sealed partial class ContextItemTemplateSelector : DataTemplateSelector
1416
{
1517
public DataTemplate? Default { get; set; }

src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
<ProjectCapability Include="Msix" />
110110
</ItemGroup>
111111

112-
<ItemGroup>
113-
<RdXmlFile Include="rd.xml" />
114-
</ItemGroup>
115-
116112
<ItemGroup>
117113
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
118114
<ProjectReference Include="..\ext\Microsoft.CmdPal.Ext.ClipboardHistory\Microsoft.CmdPal.Ext.ClipboardHistory.csproj" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using Microsoft.UI.Xaml;
7+
using Microsoft.UI.Xaml.Controls;
8+
9+
namespace Microsoft.CmdPal.UI;
10+
11+
/// <summary>
12+
/// This class ensures types used in XAML are preserved during AOT compilation.
13+
/// Framework types cannot have attributes added directly to their definitions since they're external types.
14+
/// Application types that require runtime type checking should also be preserved here if needed.
15+
/// </summary>
16+
internal static class TypePreservation
17+
{
18+
/// <summary>
19+
/// This method ensures critical types are preserved for AOT compilation.
20+
/// These types are used dynamically in XAML and would otherwise be trimmed.
21+
/// </summary>
22+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Microsoft.UI.Xaml.Controls.FontIconSource))]
23+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Microsoft.UI.Xaml.Controls.PathIcon))]
24+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Microsoft.UI.Xaml.DataTemplate))]
25+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Microsoft.UI.Xaml.Controls.DataTemplateSelector))]
26+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Microsoft.UI.Xaml.Controls.ListViewItem))]
27+
public static void PreserveTypes()
28+
{
29+
// This method exists only to hold the DynamicDependency attributes above.
30+
// It must be called to ensure the types are not trimmed during AOT compilation.
31+
32+
// Note: We cannot add [DynamicallyAccessedMembers] directly to framework types
33+
// since we don't own their source code. DynamicDependency is the correct approach
34+
// for preserving external types that are used dynamically (e.g., in XAML).
35+
36+
// For application types that require runtime type checking (e.g., in template selectors),
37+
// prefer adding [DynamicallyAccessedMembers] attributes directly on the type definitions.
38+
// Only use DynamicDependency here for types we cannot modify directly.
39+
}
40+
}

src/modules/cmdpal/Microsoft.CmdPal.UI/rd.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)