Fix HybridWebView NativeAOT warning path#34958
Merged
Conversation
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34958Or
iex "& { $(irm https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34958" |
Contributor
There was a problem hiding this comment.
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
HybridWebViewHandlerregistration into an annotated, feature-gated local path to avoid warning reachability under NativeAOT/full-trim. - Add
EventTrigger.Create(...)factory methods and refactorEventTriggerinternals 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). |
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>
8744ffd to
42efef8
Compare
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
approved these changes
Apr 14, 2026
Member
|
/azp run maui-pr-uitests, maui-pr-devicetests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
This was referenced Apr 14, 2026
Open
3 tasks
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
HybridWebViewHandlerwarnings whenHybridWebViewis feature-switched off.Root cause
HybridWebViewwas already being disabled forPublishAot=true/TrimMode=fullviaMauiHybridWebViewSupported=false. The warning still leaked because the default controls handler registration path directly referencedHybridWebViewHandler, which carriesRequiresDynamicCode/RequiresUnreferencedCodeannotations.What changed
RuntimeFeature.IsHybridWebViewSupportedgateHybridWebViewregistration into a guarded local helperWhy 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.0HybridWebViewHandlerwarning; remaining local failures were unrelated environment/feed restore issues