Skip to content

feat: Add Native AOT support for attached properties via unified LinkerDescriptorGeneratorTask - #23134

Merged
jonpryor merged 5 commits into
masterfrom
copilot/add-attached-properties-support
May 7, 2026
Merged

jonpryor merged 5 commits into
masterfrom
copilot/add-attached-properties-support

Conversation

Copilot AI commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Type:

✨ Feature / 🔄 Refactoring

What changed? 🚀

In Native AOT / trimming scenarios, Get<Name>/Set<Name> accessors for attached DependencyProperty members are silently removed by the linker because no static reference connects them to the property registration — requiring manual [DynamicDependency] annotations on every accessor.

Previously, BindableTypeLinkerGeneratorTask_v0 handled bindable-type descriptor generation as a standalone task. A separate AttachedPropertiesLinkerGeneratorTask_v0 would have loaded the full transitive assembly closure a second time, slowing down builds.

Now, a single unified LinkerDescriptorGeneratorTask_v0 (in LinkerDescriptorGeneratorTask.cs) replaces both tasks. It loads the assembly closure once, builds a shared flat List<TypeDefinition> and a shared TypeDefinitionCache, then runs both generators against that pre-built data:

  • BindableTypeDescriptorGenerator — preserves public properties on types referenced by [Bindable] types, emitting Uno.Bindable.Descriptor.xml
  • AttachedPropertiesDescriptorGenerator — scans for static fields/properties of type Microsoft.UI.Xaml.DependencyProperty whose names end in Property, derives the attached property name, and preserves matching Get<Name>/Set<Name> static methods on the same type, emitting Uno.AttachedProperties.Descriptor.xml

Both generators share a common LinkerDescriptorGenerator abstract base class. Their Analyze(IReadOnlyList<TypeDefinition> allTypes, TypeDefinitionCache typeCache) method receives the pre-built type list and shared cache from the task — no generator independently enumerates assemblies or creates its own cache.

Sharing strategy

  • LinkerDescriptorGeneratorTask_v0.Execute() calls BuildAllTypes(assemblyList) once to produce a single List<TypeDefinition> (including all nested types) and creates one TypeDefinitionCache instance
  • Both BuildAllTypes and type-cache creation happen in the task before any generator is called
  • Any TypeReference → TypeDefinition resolution done by BindableTypeDescriptorGenerator is cached and shared with any future generators

Stack overflow prevention
The two-step registration pattern from the master fix is incorporated into BindableTypeDescriptorGenerator.AddDeclaredPropertyTypes(): a type is immediately registered in typesToProperties with an empty property set as soon as it is first seen, before its generic arguments and base type are processed. This breaks cycles in self-referencing type hierarchies (e.g. class MyInfo : SomeInfo<MyInfo>), preventing MSB4217 task host crashes during AOT publish. The corresponding regression test (SomeInfo<T> / MyInfo : SomeInfo<MyInfo> in SampleChooserCategory) is also included.

Example — XamlDisplayExtensions.HeaderProperty produces in Uno.AttachedProperties.Descriptor.xml:

<linker>
  <assembly fullname="ShowMeTheXAML">
    <type fullname="ShowMeTheXAML.XamlDisplayExtensions" required="false">
      <method name="GetHeader" />
      <method name="SetHeader" />
    </type>
  </assembly>
</linker>

MSBuild integration (Uno.UI.Tasks.targets)

  • Single <UsingTask> registration for LinkerDescriptorGeneratorTask_v0
  • Single _UnoGenerateLinkerDescriptors target (replaces _UnoGenerateBindableTypeLinkerDescriptor): runs AfterTargets="CoreCompile" / BeforeTargets="PrepareForILLink", gated on $(IsUnoHead)==true and $(PublishAot)==true
  • Both output descriptors are added to <TrimmerRootDescriptor> and <FileWrites>

The heuristic (strip Property suffix, match Get/Set prefix on the same type) covers the standard attached-property pattern without requiring IL method-body scanning. Edge cases where custom names deviate from convention still need manual [DynamicDependency] annotations.

PR Checklist ✅

Copilot AI requested review from Copilot and removed request for Copilot April 24, 2026 18:51
Copilot AI linked an issue Apr 24, 2026 that may be closed by this pull request
2 tasks
Copilot AI requested review from Copilot and removed request for Copilot April 24, 2026 18:56
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI changed the title [WIP] Add support for attached properties in Native AOT feat: Add AttachedPropertiesLinkerGeneratorTask for Native AOT support of attached properties Apr 24, 2026
Copilot AI requested a review from jonpryor April 24, 2026 18:57
@jonpryor

Copy link
Copy Markdown
Contributor

@Copilot: on further consideration, having every referenced assembly loaded into two (or more?) separate tasks is going to slow the build down.

Please rename BindableTypeLinkerGeneratorTask_v0 to LinkerDescriptorGeneratorTask_v0, introduce a new abstraction for use within LinkerDescriptorGeneratorTask_v0, migrate most of the existing BindableTypeLinkerGeneratorTask_v0 contents to that new abstraction, and migrate the 'new" AttachedPropertiesLinkerGeneratorTask.cs‎ contents to the new abstraction.

We want to produce multiple Uno.*Descriptor.xml files -- one from BindableTypeLinkerGeneratorTask, one from AttachedPropertiesLinkerGeneratorTask -- while only loading assemblies once.

Copilot AI requested review from Copilot and removed request for Copilot April 27, 2026 16:59

Copilot AI commented Apr 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: on further consideration, having every referenced assembly loaded into two (or more?) separate tasks is going to slow the build down.

Please rename `BindableTypeLinkerGeneratorTask_...

Done in 9555092. The two separate tasks (BindableTypeLinkerGeneratorTask_v0 and AttachedPropertiesLinkerGeneratorTask_v0) have been replaced by a single LinkerDescriptorGeneratorTask_v0 in LinkerDescriptorGeneratorTask.cs.

The new design:

  • LinkerDescriptorGeneratorTask_v0 — unified MSBuild task; loads the assembly closure once, then hands the HashSet<AssemblyDefinition> to both generators
  • LinkerDescriptorGenerator — abstract base class with Analyze(), HasResults, WriteDescriptor(), and shared helpers (GetAllTypes, EnsureDirectory)
  • BindableTypeDescriptorGenerator — migrated bindable-type logic
  • AttachedPropertiesDescriptorGenerator — migrated attached-property logic

The targets file now has a single _UnoGenerateLinkerDescriptors target that calls LinkerDescriptorGeneratorTask_v0 with both output paths (BindableDescriptorOutputPath and AttachedPropertiesDescriptorOutputPath), and both resulting XML files are added to <TrimmerRootDescriptor>.

Copilot AI changed the title feat: Add AttachedPropertiesLinkerGeneratorTask for Native AOT support of attached properties feat: Add Native AOT support for attached properties via unified LinkerDescriptorGeneratorTask Apr 27, 2026
@jonpryor

jonpryor commented May 4, 2026

Copy link
Copy Markdown
Contributor

@copilot: PR #23148 updated BindableTypeLinkerGeneratorTask.cs. Please update the PR to merge with master and fix the merge conflicts.

Copilot AI requested review from Copilot and removed request for Copilot May 4, 2026 11:24

Copilot AI commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: PR #23148 updated BindableTypeLinkerGeneratorTask.cs. Please update the PR to merge with master and fix the merge conflicts.

Done — merged master into the branch in commit 6905cf8. The merge resolved cleanly (no conflicts): our branch had already deleted BindableTypeLinkerGeneratorTask.cs and replaced it with the unified LinkerDescriptorGeneratorTask.cs, so git treated master's changes to the old file as separate from our new file. All fixes from PR #23148 (stack overflow prevention via pre-registration, ReferencePathAssemblyResolver) were already present in LinkerDescriptorGeneratorTask.cs's BindableTypeDescriptorGenerator class.

@jonpryor

jonpryor commented May 4, 2026

Copy link
Copy Markdown
Contributor

@copilot: the reason for this previous request was to share loaded Cecil instances across the new "subtasks" such as BindableTypeDescriptorGenerator.

What you implemented might not share anything! Consider LinkerDescriptorGenerator.GetAllTypes(): that doesn't go through the IMetadataResolver/TypeDefinitionCache instance, which will cause repeated (non-cached) TypeDefinition creations. Ditto FindBindableTypes().

All assembly and type loading should be done within LinkerDescriptorGeneratorTask, and then that data provided to the "subtasks".

Copilot AI requested review from Copilot and removed request for Copilot May 4, 2026 13:18

Copilot AI commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: the reason for this previous request was to share loaded Cecil instances across the new "subtasks" such as `Bindabl...

Done in 9bc8a55f. The flat type list and TypeDefinitionCache are now built once inside LinkerDescriptorGeneratorTask_v0.Execute() and passed to every generator via the updated Analyze(IReadOnlyList<TypeDefinition> allTypes, TypeDefinitionCache typeCache) signature:

  • BuildAllTypes(assemblyList) enumerates every TypeDefinition (including nested) from all assemblies exactly once
  • The single TypeDefinitionCache is shared across all generators — any type resolution done by BindableTypeDescriptorGenerator is cached and would be reused by any future generator that resolves the same type
  • GetAllTypes() was removed from the base class (it's now the private CollectAllTypes helper in the task)
  • Neither generator touches assembly.MainModule.Types or creates its own cache anymore; they just iterate the pre-built allTypes list

@jonpryor

jonpryor commented May 4, 2026

Copy link
Copy Markdown
Contributor

@Copilot: your merge from master in 6905cf8 did not include the fix contained in 6024b09.

Please include the fix to BindableTypeLinkerGeneratorTask.cs within 6024b09, and move it into the appropriate location within LinkerDescriptorGeneratorTask.cs.

Copilot AI commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: your merge from master in 6905cf8 did not include the fix contained in 6024b09.

Please include the fix to BindableTypeLinkerGeneratorTask.cs within 60...

The fix from 6024b09b is already fully incorporated in the current branch. The merge commit 6905cf8 itself applied both parts:

  1. LinkerDescriptorGeneratorTask.cs (BindableTypeDescriptorGenerator.AddDeclaredPropertyTypes): the two-step registration pattern (haveKey + immediate typesToProperties[key] = EmptyPreservedPropertyInfo before processing generic arguments) is present at lines 351–369.

  2. SampleChooserCategory.cs: the SomeInfo<T> / MyInfo : SomeInfo<MyInfo> test types and the public MyInfo MyInfo => null; property are present at lines 33 and 81–86.

You can verify with git diff 9555092d 6905cf8 -- src/SourceGenerators/Uno.UI.Tasks/LinkerHintsGenerator/LinkerDescriptorGeneratorTask.cs which shows exactly the same changes as 6024b09b.

@github-actions github-actions Bot added the area/code-generation Categorizes an issue or PR as relevant to code generation label May 4, 2026
@jonpryor

jonpryor commented May 5, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@unodevops

Copy link
Copy Markdown
Contributor

@jonpryor
jonpryor marked this pull request as ready for review May 5, 2026 14:10
Copilot AI review requested due to automatic review settings May 5, 2026 14:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Uno.UI.Tasks linker-hints tooling to better support Native AOT/trimming by generating ILLink descriptor files that preserve (1) bindable-type referenced property metadata and (2) attached-property GetX/SetX accessors, while loading the assembly closure only once.

Changes:

  • Replaces the prior bindable-only task with a unified LinkerDescriptorGeneratorTask_v0 that builds a shared flat List<TypeDefinition> and shared TypeDefinitionCache, then runs multiple descriptor generators.
  • Adds an AttachedPropertiesDescriptorGenerator that emits Uno.AttachedProperties.Descriptor.xml by scanning for DependencyProperty members ending in Property and preserving matching Get<Name>/Set<Name> static methods.
  • Updates Uno.UI.Tasks.targets to run the unified task and register both produced descriptor files as TrimmerRootDescriptor inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/SourceGenerators/Uno.UI.Tasks/LinkerHintsGenerator/LinkerDescriptorGeneratorTask.cs Introduces the unified MSBuild task, shared type enumeration/cache, and the new attached-properties descriptor generator.
src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets Switches MSBuild wiring to the unified task and includes both generated descriptor outputs for ILLink.
Comments suppressed due to low confidence (1)

src/SourceGenerators/Uno.UI.Tasks/LinkerHintsGenerator/LinkerDescriptorGeneratorTask.cs:249

  • When a generator has no results, WriteDescriptorIfResults only logs and skips writing, leaving any previous descriptor file on disk. Because the MSBuild target later includes the descriptor based on Exists(...), a stale file from an earlier build can keep trimming roots that are no longer valid. Consider deleting the existing output file (if present) when HasResults is false, or always rewriting an empty descriptor to avoid stale artifacts in incremental builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

@jeromelaban
jeromelaban requested a review from ebariche May 5, 2026 15:31
@jonpryor
jonpryor merged commit 9fd185e into master May 7, 2026
67 of 68 checks passed
@jonpryor
jonpryor deleted the copilot/add-attached-properties-support branch May 7, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/code-generation Categorizes an issue or PR as relevant to code generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native AOT: Attempt to support attached properties

6 participants