Move HybridWebView trimmer attributes from class-level to method-level#35363
Move HybridWebView trimmer attributes from class-level to method-level#35363jonathanpeppers wants to merge 2 commits intonet11.0from
Conversation
Remove [RequiresUnreferencedCode] and [RequiresDynamicCode] from: - HybridWebViewHandler class (move to InvokeDotNetAsync, CreateInvokeResult, InvokeDotNetMethodAsync — the only methods with actual trim-unsafe code) - WebViewScriptMessageHandler and SchemeHandler nested classes (iOS) - HybridWebView2Proxy nested class (Windows) - MauiHybridWebViewClient (Android) - MauiHybridWebView platform views (Android, Windows) These types are preserved by platform managed registrars (NSObject/WebViewClient subclasses), so class-level RUCA caused IL2026 errors from <Module>..cctor() that could never be resolved via feature switches. Guard InvokeDotNetAsync calls with RuntimeFeature.IsHybridWebViewSupported (a FeatureGuard for RequiresUnreferencedCode) on iOS, Android, and Windows. When the switch is false (TrimMode=full or PublishAot=true), the trimmer removes the unsafe code path entirely. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 35363Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 35363" |
|
The warning that is appearing on the one test failure seems unrelated/different: |
There was a problem hiding this comment.
Pull request overview
This PR refines trimming/AOT annotations for HybridWebView by moving [RequiresUnreferencedCode] / [RequiresDynamicCode] from broad class-level usage to the specific reflection/dynamic-serialization methods that require it, and by gating the InvokeDotNet endpoint behind RuntimeFeature.IsHybridWebViewSupported to avoid trim-unsafe code paths when disabled.
Changes:
- Removed class-level
Requires*attributes fromHybridWebViewHandlerand several platform helper types/nested classes that are otherwise trim-safe. - Added method-level
Requires*attributes to the three trim-unsafe methods inHybridWebViewHandler. - Added
RuntimeFeature.IsHybridWebViewSupportedguards to the InvokeDotNet request handling paths on Android/iOS/Windows.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Core/src/Platform/Windows/MauiHybridWebView.cs | Removes class-level trimming attributes from the Windows platform WebView wrapper. |
| src/Core/src/Platform/Android/MauiHybridWebViewClient.cs | Removes class-level trimming attributes; gates InvokeDotNet endpoint behind RuntimeFeature.IsHybridWebViewSupported. |
| src/Core/src/Platform/Android/MauiHybridWebView.cs | Removes class-level trimming attributes from the Android platform WebView wrapper. |
| src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs | Gates InvokeDotNet handling and removes trimming attributes from the WebView2 proxy type. |
| src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.iOS.cs | Removes trimming attributes from nested registrar-referenced types; gates InvokeDotNet handling. |
| src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs | Moves trimming attributes from class-level to the specific trim-unsafe methods. |
Comments suppressed due to low confidence (1)
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.iOS.cs:129
- After removing the Requires* attributes from the nested handler types,
using System.Diagnostics.CodeAnalysis;is unused in this file and will trigger CS8019 (and fail the build under TreatWarningsAsErrors). Please remove the unused using.
private sealed class WebViewScriptMessageHandler : NSObject, IWKScriptMessageHandler
{
private readonly WeakReference<HybridWebViewHandler?> _webViewHandler;
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
|
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!
Description
Move
[RequiresUnreferencedCode]and[RequiresDynamicCode]from class-level to method-level onHybridWebViewHandlerand remove them entirely from platform types that are trim-safe.Problem
The iOS/macCatalyst Managed Registrar generates code in
<Module>..cctor()that references allNSObjectsubclasses, includingSchemeHandlerandWebViewScriptMessageHandler. These nested classes had class-level[RequiresUnreferencedCode], which caused IL2026 errors from the registrar's type registration code — errors that could never be resolved via feature switches.Similarly, Android's
MauiHybridWebViewClient(extendingWebViewClient) had the same problem.This caused failures in:
RunOniOS_MauiReleaseTrimFullintegration test (TrimMode=full)PublishNativeAOTRootAllMauiAssembliesintegration test (rooting all assemblies)Fix
Only three methods on
HybridWebViewHandlerare actually trim-unsafe (they use reflection and runtime JSON serialization):InvokeDotNetAsyncInvokeDotNetMethodAsyncCreateInvokeResultAll other code (message receiving, URL scheme handling for static files, platform view wrappers) is trim-safe — it only uses source-generated JSON contexts.
Changes:
HybridWebViewHandler,MauiHybridWebViewClient,MauiHybridWebView(Android/Windows), and all nested platform classesInvokeDotNetAsynccalls on all platforms withRuntimeFeature.IsHybridWebViewSupported— a[FeatureGuard]forRequiresUnreferencedCode. When the switch is false (TrimMode=fullorPublishAot=true), the trimmer removes the unsafe code path entirely.