Skip to content

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

Closed
mergify[bot] wants to merge 6 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25862
Closed

mergify[bot] wants to merge 6 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25862

Conversation

@mergify

@mergify mergify Bot commented Nov 6, 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


This is an automatic backport of pull request #25862 done by [Mergify](https://mergify.com).

ramezgerges and others added 6 commits November 6, 2026 08:37
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]>
(cherry picked from commit fd9a94e)
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]>
(cherry picked from commit f9d365a)
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]>
(cherry picked from commit 88e5b09)
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]>
(cherry picked from commit 8189c70)
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]>
(cherry picked from commit 6c69b61)
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]>
(cherry picked from commit 029cc17)
@unodevops

Copy link
Copy Markdown
Contributor

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

@MartinZikmund
MartinZikmund deleted the mergify/bp/release/stable/7.0/pr-25862 branch November 12, 2026 10:04
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