Skip to content

fix(trimming): Suppress IL2026 on bindable activators - #26630

Merged
Xiaoy312 merged 1 commit into
masterfrom
dev/xygu/20261120/fix-bindable-createinstance-il2026
Nov 24, 2026
Merged

Xiaoy312 merged 1 commit into
masterfrom
dev/xygu/20261120/fix-bindable-createinstance-il2026

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/add-private#62

PR Type:

🐞 Bugfix

What changed? 🚀

Before: the bindable metadata generator emits a CreateInstance() activator (new T()) for every createable bindable type in referenced assemblies, whether the app uses it or not. When the type or its parameterless constructor is marked [RequiresUnreferencedCode], the trim analyzer reports IL2026 in BindableMetadata.g.cs, and the app can't do anything about it. For example, referencing Uno.Microsoft.Xaml.Behaviors.Interactivity.WinUI 4.0.1 gives four of these warnings: CallMethodAction, ChangePropertyAction, DataTriggerBehavior and EventTriggerBehavior.

After: when the type or its activator constructor carries [RequiresUnreferencedCode], the activator gets [UnconditionalSuppressMessage("Trimming", "IL2026", ...)]. This matches what the generated code already does elsewhere: Build() in the same file and the XAML generator's InitializeComponent already blanket-suppress IL2026.

  • The attribute is matched by name and namespace, as the trim analyzer does, so polyfilled copies count too.
  • IsCreateable is replaced by GetActivatorConstructor, which both the activator emission and the attribute check use.

Validation

  • Generator test When_Constructor_Requires_Unreferenced_Code_Then_Activator_Suppresses_IL2026 covers three cases: [RequiresUnreferencedCode] on the type, on the constructor only, and a trim-safe type that must not get the suppression. It fails without the fix. When_RequiresUnreferencedCode_Is_Polyfilled_Then_Activator_Suppresses_IL2026 covers a polyfilled copy of the attribute (suppressed) and a same-named attribute in another namespace (not suppressed). The full Uno.UI.SourceGenerators.Tests suite passes (639 passed, 7 skipped, 0 failed).
  • Repro app (Uno.Sdk 7.0.22, Behaviors 4.0.1, net10.0-desktop, EnableTrimAnalyzer=true): 4 IL2026 with the published generator, 0 with this branch's generator. The suppression lands on exactly those four activators.
  • Using EventTriggerBehavior / CallMethodAction in XAML produces no IL2026 at the use site, before or after the fix, because InitializeComponent is already suppressed.
  • Trimmed publish (PublishTrimmed=true, win-x64, self-contained) of the same repro: ILLink reports the same 4 IL2026 in BindableMetadata.g.cs with the published generator, and none with this branch's generator. The other trim warnings are the same in both runs (2 IL2104, for Uno.UI.Runtime.MacOS and Uno.UI.Runtime.Win32). To get ILLink to finish, the WebView2Aot/DirectNAot references were kept out of ILLink. Otherwise it hangs on the ICoreWebView2_* interface chain, independently of this change (ILLink takes exponential time on deep interface hierarchies dotnet/runtime#136766, Trimmed Skia Desktop publish hangs #24618).

PR Checklist ✅

🤖 Generated with Claude Code

@github-actions github-actions Bot added the area/code-generation Categorizes an issue or PR as relevant to code generation label Nov 21, 2026
@mergify

mergify Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@Sergio0694

Copy link
Copy Markdown

If these annotations are suppressed, when will users see that IL2026 warning when using the behavior?

@Xiaoy312

Copy link
Copy Markdown
Contributor Author

If these annotations are suppressed, when will users see that IL2026 warning when using the behavior?

Good question. From XAML, they don't today, and they didn't before this PR either. The XAML generator emits [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Generated code")] on the methods it generates for a XAML file (InitializeComponent, the builder classes, etc.), so <i:EventTriggerBehavior/> never warned at the use site. From C#, new EventTriggerBehavior() still warns as usual.

The warnings removed here came from BindableMetadata.g.cs. The bindable generator emits an activator for every bindable type in referenced assemblies, used or not, so every app referencing the package got them, even if it used none of the behaviors, and couldn't act on them. They weren't a usage signal.

The real fix is for the XAML generator to report [RequiresUnreferencedCode] types and members where they're used in XAML. I opened #26661 for that, so this PR stays scoped to the noise.

@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261120/fix-bindable-createinstance-il2026 branch from a2ac6a9 to 1802642 Compare November 23, 2026 20:38
The bindable metadata generator emits a CreateInstance activator for
every createable bindable type in referenced assemblies, used or not.
When the type or its parameterless constructor carries
[RequiresUnreferencedCode] (e.g. CallMethodAction and EventTriggerBehavior
in Uno.Microsoft.Xaml.Behaviors.Interactivity), the trim analyzer reported
an IL2026 on BindableMetadata.g.cs that the app could not act on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261120/fix-bindable-createinstance-il2026 branch from 1802642 to b57955a Compare November 23, 2026 20:55
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@Xiaoy312
Xiaoy312 merged commit 6e34483 into master Nov 24, 2026
50 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20261120/fix-bindable-createinstance-il2026 branch November 24, 2026 16:22
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.

4 participants