Skip to content

Fix HybridWebView NativeAOT warning path#34958

Merged
PureWeen merged 2 commits intonet11.0from
dev/simonrozsival/investigate-34867-hybridwebview-aot
Apr 15, 2026
Merged

Fix HybridWebView NativeAOT warning path#34958
PureWeen merged 2 commits intonet11.0from
dev/simonrozsival/investigate-34867-hybridwebview-aot

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

@simonrozsival simonrozsival commented Apr 14, 2026

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Fixes #34867

Summary

Prevent NativeAOT/full-trim analysis from surfacing HybridWebViewHandler warnings when HybridWebView is feature-switched off.

Root cause

HybridWebView was already being disabled for PublishAot=true / TrimMode=full via MauiHybridWebViewSupported=false. The warning still leaked because the default controls handler registration path directly referenced HybridWebViewHandler, which carries RequiresDynamicCode / RequiresUnreferencedCode annotations.

What changed

  • keep the existing RuntimeFeature.IsHybridWebViewSupported gate
  • move the HybridWebView registration into a guarded local helper
  • attach the AOT/trimming annotations to that guarded path instead of the default registration flow

Why this approach

This fixes the actual reachability/analysis shape instead of suppressing the warning or expanding the expected-warning baseline.

Validation

  • dotnet build src/Controls/src/Core/Controls.Core.csproj -f net11.0
  • local NativeAOT integration flow no longer emitted the previous unexpected HybridWebViewHandler warning; remaining local failures were unrelated environment/feed restore issues

Copilot AI review requested due to automatic review settings April 14, 2026 12:23
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 14, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34958

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34958"

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 aims to prevent NativeAOT/full-trim analysis from reporting HybridWebViewHandler dynamic-code warnings when HybridWebView is feature-switched off by isolating the handler registration behind the feature gate. In addition, it introduces new AOT-safe EventTrigger factory APIs and updates the XAML source generator to emit those factories when EventTrigger.Event is set, along with new unit tests.

Changes:

  • Isolate HybridWebViewHandler registration into an annotated, feature-gated local path to avoid warning reachability under NativeAOT/full-trim.
  • Add EventTrigger.Create(...) factory methods and refactor EventTrigger internals to use pluggable subscription strategies (reflection vs static).
  • Extend SourceGen to generate AOT-safe EventTrigger.Create(...) instantiation and add coverage across Core/XAML/SourceGen unit tests.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs Moves HybridWebView handler registration behind a local helper annotated for trimming/AOT analysis.
src/Controls/src/Core/Interactivity/EventTrigger.cs Adds AOT-safe EventTrigger.Create APIs and refactors subscription logic into strategies with trimming/AOT annotations.
src/Controls/src/SourceGen/Visitors/CreateValuesVisitor.cs Special-cases EventTrigger so it’s declared before child TriggerAction processing and can be created via SourceGen provider.
src/Controls/src/SourceGen/NodeSGExtensions.cs Registers EventTriggerValueProvider as a known value provider for SourceGen.
src/Controls/src/SourceGen/EventTriggerValueProvider.cs Implements AOT-safe SourceGen emission for EventTrigger by using EventTrigger.Create when possible.
src/Controls/src/Core/PublicAPI/*/PublicAPI.Unshipped.txt Adds new public API entries for the EventTrigger.Create overloads across TFMs.
src/Controls/tests/Xaml.UnitTests/Issues/Maui33591.xaml Adds a XAML repro page using EventTrigger for multi-inflator validation.
src/Controls/tests/Xaml.UnitTests/Issues/Maui33591.xaml.cs Adds XAML unit tests validating EventTrigger.Event population and element creation across inflators.
src/Controls/tests/SourceGen.UnitTests/Maui33591SourceGenTests.cs Adds a snapshot-style test asserting SourceGen emits EventTrigger.Create(...) with static lambdas.
src/Controls/tests/Core.UnitTests/Triggers/EventTriggerBaseTests.cs Adds Core unit tests validating EventTrigger.Create(...) behavior (attach/detach/invocation).

Comment thread src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs
Comment thread src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs
Comment thread src/Controls/src/Core/Interactivity/EventTrigger.cs Outdated
Comment thread src/Controls/src/Core/Interactivity/EventTrigger.cs Outdated
Comment thread src/Controls/tests/Core.UnitTests/Triggers/EventTriggerBaseTests.cs Outdated
Comment thread src/Controls/tests/Core.UnitTests/Triggers/EventTriggerBaseTests.cs Outdated
Comment thread src/Controls/src/Core/Interactivity/EventTrigger.cs Outdated
Comment thread src/Controls/src/SourceGen/EventTriggerValueProvider.cs Outdated
Comment thread src/Controls/src/SourceGen/EventTriggerValueProvider.cs Outdated
Keep the HybridWebView handler registration on its own guarded local path so NativeAOT/full-trim analysis does not pick up the dynamic-code handler from the default controls registration list.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/investigate-34867-hybridwebview-aot branch from 8744ffd to 42efef8 Compare April 14, 2026 12:45
Use a local const string for the AOT/trimming annotation message instead of referencing HybridWebViewHandler.DynamicFeatures from Controls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Apr 14, 2026
Add Section 23 to review-rules.md covering IL2026/IL3050 annotation
chain analysis, the extract-method pattern for feature-guarded types,
and why [UnconditionalSuppressMessage] is almost always wrong for
these warnings.

Sourced from the HybridWebViewHandler incident (Issue #34867,
PR #34958) where AI agents consistently recommended suppression
instead of the correct structural fix when they didn't read the
source code first.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen
Copy link
Copy Markdown
Member

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen PureWeen merged commit c223f8b into net11.0 Apr 15, 2026
45 of 220 checks passed
@PureWeen PureWeen deleted the dev/simonrozsival/investigate-34867-hybridwebview-aot branch April 15, 2026 00:30
PureWeen pushed a commit that referenced this pull request Apr 15, 2026
…ethod

The local function pattern from PR #34958 compiles to a compiler-
generated method (<AddControlsHandlers>g__AddHybridWebViewHandler|2_0)
which newer ILC versions (SDK 26203.107+) trace back to the caller,
defeating the [FeatureGuard] suppression. The IL3050 warning leaks
because ILC doesn't treat local functions as independent annotation
boundaries.

Moving to a proper private static method on the class creates a real
method boundary in IL metadata that the [FeatureGuard] on
RuntimeFeature.IsHybridWebViewSupported can properly suppress.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
PureWeen added a commit that referenced this pull request Apr 21, 2026
…ethod (#34986)

## Problem

PR #34958's fix for the HybridWebView IL3050 warning used a **local
function** inside `AddControlsHandlers`. This worked with SDK
`26166.111` but **fails with the newer SDK** `26203.107` (from #34852):

```
Unexpected warning: AppHostBuilderExtensions.<AddControlsHandlers>g__AddHybridWebViewHandler|2_0
  has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling.
```

The C# compiler emits local functions as `<EnclosingMethod>g__Name|N_M`
— a compiler-generated method that newer ILC versions trace back to the
caller, defeating the `[FeatureGuard]` suppression on
`RuntimeFeature.IsHybridWebViewSupported`.

## Fix

Move the local function to a **private static method** on the class.
This creates a real method boundary in IL metadata that the
`[FeatureGuard]` can properly suppress — the standard pattern documented
in our [trim/AOT review
rules](https://github.com/dotnet/maui/blob/main/.github/skills/code-review/references/review-rules.md#23-trim--nativeaot-safety).

cc @simonrozsival — this builds on your #34958 fix, just moving from
local function to class method for the newer ILC.

## Validation

`dotnet build Controls.Core.csproj -f net11.0
-p:TreatWarningsAsErrors=true` — 0 errors

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants