Skip to content

fix(sdk): Leave out WebView2Aot assets when WebView is unused - #25862

Merged
MartinZikmund merged 6 commits into
unoplatform:masterfrom
ramezgerges:dev/rara/sdk-drop-unused-webview2
Nov 6, 2026
Merged

MartinZikmund merged 6 commits into
unoplatform:masterfrom
ramezgerges:dev/rara/sdk-drop-unused-webview2

Conversation

@ramezgerges

@ramezgerges ramezgerges commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: related to #24618

PR Type:

🐞 Bugfix

What changed? 🚀

A trimmed publish of a blank desktop app (-p:PublishTrimmed=true) gets stuck at "Optimizing assemblies for size", even when the app never uses a WebView.

Cause. Uno.UI.Runtime.Win32 references WebView2Aot (and through it DirectNAot) unconditionally, so every desktop head ships them. WebView2Aot's [GeneratedComInterface] chain is 29 interfaces deep (ICoreWebView2_29 : ICoreWebView2_28, …), and ILLink walks such chains exponentially. A dotnet/runtime issue and fix will be linked from #24618.

Fix. A WebView needs WebView2Loader.dll from Microsoft.Web.WebView2, which the WebView Uno Feature (required by the docs for WebView on -desktop), a direct reference or a transitive one deploys. Desktop heads that don't deploy it now leave out the WebView2Aot assets after package resolution. DirectNAot and DirectNAot.Extensions are general-purpose interop, so they are left out too only when the dependency graph (the SDK's ResolvePackageDependencies) shows WebView2Aot as their sole owner; a head or library that uses DirectN itself keeps it, and compile references are never removed. UnoIncludeWebView2Aot=true keeps everything.

The host registers its WebView implementation through ApiExtensibility factories, so those types load only when a WebView is created. An app without WebView starts normally without these assemblies.

Keying on the deployed loader covers heads that reference Microsoft.Web.WebView2 directly or transitively instead of using the feature. SamplesApp references it directly, and an earlier version keyed on the feature alone crashed both Windows lanes in Given_CoreWebView2Profile.

Creating a WebView in an app where it was left out used to fail with a bare FileNotFoundException for WebView2. It now throws an InvalidOperationException saying WebView2 support was left out and to enable the WebView Uno Feature or reference Microsoft.Web.WebView2.

That crash also exposed a finalizer bug. When Win32NativeAotWebView's constructor fails (here because WebView2 failed to load), the base constructor never ran. Its finalizer then dereferenced a null Presenter and ended the process. The finalizer now skips an unconstructed instance.

Apps with the webview feature are unchanged. A trimmed publish of such an app still needs the ILLink fix.

Validation (blank net10.0-desktop app on Uno.Sdk 7.0.0-dev.113, dotnet publish -r win-x64 --self-contained -p:PublishTrimmed=true, with this target imported into the app):

Before After
No webview feature ILLink still running after 15 min (4.2 GB), killed 18 s; no DirectN/WebView2 shipped; app starts and shows its window
webview feature (build output) DirectN, DirectN.Extensions, WebView2 present unchanged (all 3 present)

SamplesApp desktop, Given_CoreWebView2Profile + Given_CoreWebView2Environment: before, the app segfaults (Win32NativeWebViewBase.Finalize NRE) and writes no results; after, 17/17 pass, with WebView2.dll/DirectN*.dll in the output.

The trim warnings with the assemblies removed are the existing UI Automation (IL2050) and macOS WebView (IL2026) ones; none come from the missing assemblies.

Target logic, with a standalone head importing it (RID-less and -r win-x64):

Case WebView2Aot DirectN
Nothing deploys the loader left out left out
webview feature, direct reference, or a class library referencing Microsoft.Web.WebView2 kept kept
Head references DirectNAot and uses its types left out kept, builds (the previous version failed with CS0246)
A class library depends on DirectNAot left out kept

Error message: a SamplesApp desktop build with WebView2/DirectN removed from its output reports the new InvalidOperationException when WebView2_Fixed_Height creates its WebView.

PR Checklist ✅

🤖 Generated with Claude Code

@unodevops

Copy link
Copy Markdown
Contributor

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

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 242606 has failed on Uno.UI - CI.

@unodevops

Copy link
Copy Markdown
Contributor

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

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 242857 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AppBarButtonTest.png-dark
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSizing.png
    • AppWindowPositionAndSize.png-dark
    • ArcSegment.png
    • AuthenticationBroker_Cancel.png-dark
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_Landmark.png
    • AutomationProperties_Name.png-dark
    • AutoSuggestBox_Description.png-dark
    • AutoSuggestBox_Description.png
    • BackGesture.png-dark
    • BackGesture.png
    • Basics Pivot Test.png-dark
    • Basics Pivot Test.png
    • Battery.png-dark
    • Battery.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppBarButtonWithIconTest.png
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSizing.png-dark
    • AppWindowClosing.png-dark
    • AppWindowPositionAndSize.png
    • ArcSegment.png
    • Arrange_Performance01.png-dark
    • AutoBorderStretchwithbottommargin.png-dark
    • AutoBorderStretchwithbottommargin.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BadgeNotificationTests.png-dark
    • BadgeNotificationTests.png
    • Basics_Automated.png-dark
    • Basics_Automated.png

@ramezgerges
ramezgerges force-pushed the dev/rara/sdk-drop-unused-webview2 branch from c5bdd14 to 94d5320 Compare November 3, 2026 16:55
@unodevops

Copy link
Copy Markdown
Contributor

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

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243341 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedIconPage.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AppBarButtonTest.png
    • AnimatedVisualsTestbedPage.png
    • AppBar_KeyBoard.png
    • ApplicationViewMode.png-dark
    • ApplicationViewSpanningRectsPage.png
    • Application_DispatcherShutdownMode.png-dark
    • AppWindowPositionAndSize.png-dark
    • AppWindowPresenters.png-dark
    • AppXamlDefinedResources.png
    • ArcSegment.png-dark
    • Attributed_text_Simple.png-dark
    • Attributed_text_Simple.png
    • AuthenticationBroker_Demo.png-dark
    • AuthenticationBroker_Demo.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutoBorderStretchwithtopmargin.png-dark
    • AutoBorderStretchwithrightmargin.png
    • AutoBorderStretchwithtopmargin.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AppWindowFeatures.png
    • AppXamlDefinedResources.png
    • Attributed_text_Supserscript.png
    • AutoBorderStretchwithleftmargin.png
    • AutoBorderStretchwithtopmargin.png
    • AutomationProperties_Name.png
    • AutoSizedPathCentered.png-dark
    • AutoSizedTopLeft.png
    • AutoSuggestBox_BitmapIcon.png-dark
    • AutoSuggestBox_BitmapIcon.png
    • AutoSuggestBox_SoftKeboard.png
    • BackGesture.png-dark
    • Basics Pivot Test.png-dark
    • Basics Pivot Test.png
    • Battery.png-dark
    • Battery.png
    • BezierSegment.png-dark
    • BezierSegment.png
    • BitmapIcon_Foreground.png-dark
    • BitmapIcon_Foreground.png

@Xiaoy312

Xiaoy312 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Heads-up: this PR and #25996 (approved, fix for #25216) both edit src/Uno.Sdk/targets/Uno.SingleProject.Desktop.targets, and a test merge of the two conflicts. Whichever one merges second needs a rebase.

The conflict is textual only. Your new _UnoRemoveUnusedWebView2Aot target is inserted right above the comment of _UnoWebView2RemoveWpf, and #25996 rewrites that comment's first line. To resolve it:

  • keep _UnoRemoveUnusedWebView2Aot exactly as it is in this PR;
  • for the _UnoWebView2RemoveWpf comment, take fix(sdk)!: Ship only the WebView2 native loader #25996's line:
    <!--
    	An explicit `Microsoft.Web.WebView2` reference pulls in the WPF-specific
    	Microsoft.Web.WebView2.Wpf reference even on a plain (non-WPF) desktop head; strip it
    	by default. Set `UnoDisableWebView2Workarounds` to keep it.
    -->
    After fix(sdk)!: Ship only the WebView2 native loader #25996, the webview feature references Microsoft.Web.WebView2 with IncludeAssets="native", so it only ships the native WebView2Loader.dll. The WPF reference now comes only from an explicit reference in the app.

There's no semantic conflict that I can see:

A side effect, not verified: removing WebView2Aot from ResolvedCompileFileDefinitions should also avoid #26007 (CS0118: 'WebView2' is a namespace on new WebView2()) for heads without the webview feature. Heads with the feature, or with an explicit Microsoft.Web.WebView2 reference, still need the fix tracked there.

@MartinZikmund MartinZikmund added the 7.0 PRs only. For issues, use the Release field. label Nov 4, 2026
@MartinZikmund

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/7.0

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/7.0

✅ Backports have been created

Details

@ramezgerges
ramezgerges force-pushed the dev/rara/sdk-drop-unused-webview2 branch from 94d5320 to ca90519 Compare November 4, 2026 18:22
@unodevops

Copy link
Copy Markdown
Contributor

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

@Xiaoy312 Xiaoy312 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.

Approving: dropping WebView2Aot/DirectN from heads that can't use a WebView is the right call, and the finalizer guard is correct. A few non-blocking follow-ups inline, the first two being the ones I'd most like addressed (here or in a follow-up):

  1. The keep-condition only sees the head's own feature/direct reference, so a transitively flowing Microsoft.Web.WebView2 (a library with the WebView feature, or a NuGet package depending on it) would now lose the WebView at runtime.
  2. DirectNAot is stripped (including compile refs) even when the app references it directly.
  3. Without the assemblies, a WebView now fails with an assembly-load FileNotFoundException instead of the clearer DllNotFoundException for WebView2Loader.dll; a targeted message pointing at the WebView UnoFeature / UnoIncludeWebView2Aot would help.
  4. UnoIncludeWebView2Aot isn't documented yet; skia-libraries-trimming.md (next to UnoIncludeAllSkiaNativeLibraries) or the desktop section of WebView.md seem like the right homes.

CI: the Android shard 0/4 failures are the known Given_TextBox.When_Touch_*_Does_Not_Lock_ScrollViewer_Android pair, and the Android+NativeAOT failure is the NU1903 ILCompiler advisory at restore; neither is from this PR.

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243708 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedIconPage.png-dark
    • AnimatedVisualsTestbedPage.png
    • ApplicationViewMode.png-dark
    • ApplicationViewSpanningRectsPage.png
    • AppWindowFeatures.png
    • AppWindowPresenters.png-dark
    • ArcSegment.png-dark
    • Arrange_Performance01.png-dark
    • Attributed_text_Supserscript.png-dark
    • Attributed_text_Supserscript.png
    • AutoBorderStretchwithleftmargin.png-dark
    • AutoBorderStretchwithleftmargin.png
    • AutoSizedPathCentered.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_Description.png-dark
    • AutoSuggestBox_Description.png
    • BadgeNotificationTests.png-dark
    • BadgeNotificationTests.png
    • Basics.png-dark
    • Basics.png
  • skia-windows-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AppBarButtonTest.png-dark
    • AppBarButtonWithIconTest.png
    • AppBar_KeyBoard.png
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowClosing.png-dark
    • AppWindowPositionAndSize.png
    • AppXamlDefinedResources.png-dark
    • Arrange_Performance01.png-dark
    • Arrange_Performance01.png
    • AuthenticationBroker_Cancel.png
    • AuthenticationBroker_Demo.png-dark
    • AutomationProperties_AutomationId.png
    • AutomationProperties_Landmark.png-dark
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • AutoSuggestBox_Reason.png-dark
    • AutoSuggestBox_Reason.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png

@ramezgerges
ramezgerges force-pushed the dev/rara/sdk-drop-unused-webview2 branch from 78cc91f to 029cc17 Compare November 5, 2026 17:54
ramezgerges and others added 6 commits November 5, 2026 12:54
The Win32 host references WebView2Aot, which pulls in DirectNAot. With
trimming on, ILLink spends minutes or hangs walking their deep COM
interface chains, so a trimmed desktop publish never finishes, even
for apps that never create a WebView.

Desktop heads without the documented `webview` feature now drop their
assets after package resolution. `UnoIncludeWebView2Aot` keeps them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A head can use WebView through a direct Microsoft.Web.WebView2 reference instead of the webview feature, as SamplesApp does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A derived constructor that throws (WebView2Aot missing) leaves the base unconstructed, and its finalizer's NRE ends the process.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Keys the strip on WebView2Loader.dll being deployed, so a transitive
Microsoft.Web.WebView2 reference keeps the WebView working too.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
DirectN is general-purpose interop: it is now left out only when the
dependency graph shows WebView2Aot as its sole owner, and compile
references are no longer removed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Creating a WebView after the SDK left WebView2Aot out failed with a bare
FileNotFoundException for an assembly the app never referenced.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

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

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 244090 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedIconPage.png-dark
    • AppBarButtonWithIconTest.png
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowFeatures.png-dark
    • AppWindowTitleBar Properties.png
    • Attributed_text_FontSize_Changing.png-dark
    • AutoBorderStretchwithbottommargin.png-dark
    • AutoBorderStretchwithtopmargin.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutomationProperties_Relations.png-dark
    • AutomationProperties_Relations.png
    • AutoSuggestBox_Reason.png-dark
    • AutoSuggestBox_Reason.png
    • BasicEnability.png-dark
    • BasicEnability.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png
  • skia-windows-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AnimatedIconPage.png-dark
    • AppBarButtonTest.png-dark
    • AppBarToggleButtonTest.png-dark
    • ApplicationViewMode.png-dark
    • Application_DispatcherShutdownMode.png
    • AppXamlDefinedResources.png-dark
    • Arrange_Performance01.png-dark
    • AuthenticationBroker_Cancel.png-dark
    • AuthenticationBroker_Cancel.png
    • AutomationProperties_AutomationId.png
    • AutomationProperties_Landmark.png-dark
    • AutoSuggestBoxChosenSuggestion.png
    • AutoSuggestBox_BitmapIcon.png-dark
    • BadgeNotificationTests.png-dark
    • BadgeNotificationTests.png
    • BasicAutoSuggestBox.png-dark
    • BasicAutoSuggestBox.png
    • BasicThemeResources.png-dark
    • BasicThemeResources.png

@mergify

mergify Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@MartinZikmund
MartinZikmund merged commit e56d4db into unoplatform:master Nov 6, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.0 PRs only. For issues, use the Release field.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants