Skip to content

chore: Support macOS Desktop NativeAOT - #1709

Closed
jonpryor wants to merge 1 commit into
mainfrom
dev/jonpryor/jonp-macos-nativeaot
Closed

jonpryor wants to merge 1 commit into
mainfrom
dev/jonpryor/jonp-macos-nativeaot

Conversation

@jonpryor

@jonpryor jonpryor commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator

Context: unoplatform/uno@1003d8c

What do we want? NativeAOT support!

Where do we want it! Desktop macOS?

(It works for Android, how hard could desktop macOS be?)

Allow building with .NET 10 RC2:

cat >patch <<EOF
diff --git a/Chefs/Chefs.csproj b/Chefs/Chefs.csproj
index b1fc80fc..4e181980 100644
--- a/Chefs/Chefs.csproj
+++ b/Chefs/Chefs.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Uno.Sdk">
+<Project Sdk="Uno.Sdk.Private">
 	<PropertyGroup>
 		<!-- Building with dotnet build -f net9.0-platform will still cause restore to happen for all TargetFrameworks -->
 		<!-- which will force us to install all workloads even if not needed -->
diff --git a/global.json b/global.json
index 7f65a2cc..36721321 100644
--- a/global.json
+++ b/global.json
@@ -1,8 +1,8 @@
 {
   "msbuild-sdks": {
-    "Uno.Sdk": "6.3.28"
+    "Uno.Sdk.Private": "6.4.0-dev.244"
   },
   "sdk": {
-    "allowPrerelease": false
+    "allowPrerelease": true
   }
 }
EOF
git apply < patch

Build it with .NET 10 RC2:

dotnet publish -c Release -r osx-x64 \
  -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop \
  -p:SelfContained=true -p:PublishAot=true -p:UseSkiaRendering=true \
  Chefs/Chefs.csproj -bl

Run it:

Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

Watch it blow up (?!):

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|247_0(DPI_AWARENESS_CONTEXT) + 0x22
   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
   at DpiBootstrap.Init() + 0x10
   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
zsh: abort      Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

What's happening is:

  1. unoplatform/uno@1003d8cf introduced the use of module initializers into Uno.UI.Runtime.Skia.Win32.Support.dll.

  2. Because Program.cs has a .UseWin32() method invocation, there is (eventually) an assembly reference to Uno.UI.Runtime.Skia.Win32.Support.dll.

  3. Under NativeAOT, module initializers are run during startup. All of them.

Which means that when building a NativeAOT app for macOS, the module initializers from Uno.UI.Runtime.Skia.Win32.Support.dll are executed.

The "fix" (workaround) is to remove the call to .UseWin32().

(A better fix would be to remove the module initializer, maybe?)

Update the AdjustCalabash target to update $(DefineConstants) based on the $(RuntimeIdentifier) value, when set, so that it contains a "#define-safe" RUNTIME_IDENTIFIER_-prefixed value based on $(RuntimeIdentifier). For example, in dotnet build -r osx-x64, both RUNTIME_IDENTIFIER_OSX and RUNTIME_IDENTIFIER_OSX_X64 will be defined during compilation.

See also the .NET RID Catalog, from which we see that the $(RuntimeIdentifier) values for Windows begin with win-.

Surround the .UseWin32() invocation with RUNTIME_IDENTIFIER_WIN, so that it isn't called when running on e.g. macOS.

That fixes the DllNotFoundException.

The next problem is that, while it no longer crashes, it doesn't show anything either.

Update Program.cs to call App.InitializeLogging(), so that we can see log messages printed to stdout.

Now we can see the next problem:

System.InvalidOperationException: A suitable constructor for type 'Uno.Extensions.Navigation.Navigators.ContentControlNavigator' could not be located.
  Ensure the type is concrete and services are registered for all parameters of a public constructor.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, ServiceIdentifier, Type, CallSiteChain) + 0x3e9
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateExact(ServiceDescriptor, ServiceIdentifier, CallSiteChain, Int32) + 0x181
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceIdentifier, CallSiteChain) + 0x14f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain) + 0xc9
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceIdentifier, CallSiteChain) + 0x92
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier) + 0x68
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xd2
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier, ServiceProviderEngineScope) + 0x39
   at Uno.Extensions.Navigation.NavigatorFactory.CreateService(IRegion) + 0xe2
   at Uno.Extensions.Navigation.Regions.NavigationRegion.InitializeRootRegion(IServiceProvider) + 0x61
   at Uno.Extensions.Navigation.Regions.NavigationRegion..ctor(ILogger, FrameworkElement, IServiceProvider) + 0x2ee
   at Uno.Extensions.Navigation.FrameworkElementExtensions.HostAsync(FrameworkElement, IServiceProvider, String, Type, Type, Func`3) + 0xbc
   at Uno.Extensions.ServiceProviderExtensions.<BuildAndInitializeHostAsync>d__5.MoveNext() + 0x18f

Which is where everything goes off the rails: the Chefs app is quite dynamic, whereas NativeAOT isn't; the trimmer is removing lots of "necessary" code.

Square that circle by (more or less) preserving "everything": add a @(TrimmerRootAssembly) item group which lists (almost) everything that the trimmer needs to preserve. (And then some. This is not a minimal group; this is the "biggest small" group I could get building and running properly in a short timeframe. It likely can be shrunk.)

Notably absent: everything that references
Uno.UI.Runtime.Skia.Win32.Support.dll (need to allow the app to start!), all System assemblies, lots of Microsoft.* assemblies…

Now the app is able to launch and run correctly.

…but at what cost?

Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs is a 121MB binary.

The Chefs/bin/Release/net10.0-desktop/osx-x64/publish directory is 810MB in size, though that also contains artifacts that don't appear relevant for macOS, e.g. libvlc/win-x64; the libvlc directory is 197MB, and doesn't immediately appear to be needed (i.e. the app launches and is usable if libvlc doesn't exist).

Compare to a -p:SelfContained=true build which doesn't use NativeAOT, and we get:

Configuration Chefs size publish size
-p:SelfContained=true 92856
91K
444M
-p:SelfContained=true -p:PublishAot=true 126703200
121M
810M

The non-NativeAOT build also contains a libvlc directory…

The next obvious question is "what does startup look like?" Unfortunately that information doesn't appear to be collected by the operating system, so I either use a stopwatch (lol) or by "feels". NativeAOT "feels" faster, but it's not dramatic. It feels hundredths of a second faster to start and react to button clicks.

GitHub Issue (If applicable): #

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template. (for bug fixes / features)
  • Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Wasm UI Tests are not showing unexpected any differences. Validate PR Screenshots Compare Test Run results.
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Internal Issue (If applicable):

Context: unoplatform/uno@1003d8c

What do we want?  NativeAOT support!

Where do we want it!  Desktop macOS?

(It works for Android, how hard could desktop macOS be?)

*Allow* building with .NET 10 RC2:

	cat >patch <<EOF
	diff --git a/Chefs/Chefs.csproj b/Chefs/Chefs.csproj
	index b1fc80f..4e181980 100644
	--- a/Chefs/Chefs.csproj
	+++ b/Chefs/Chefs.csproj
	@@ -1,4 +1,4 @@
	-<Project Sdk="Uno.Sdk">
	+<Project Sdk="Uno.Sdk.Private">
	 	<PropertyGroup>
	 		<!-- Building with dotnet build -f net9.0-platform will still cause restore to happen for all TargetFrameworks -->
	 		<!-- which will force us to install all workloads even if not needed -->
	diff --git a/global.json b/global.json
	index 7f65a2c..36721321 100644
	--- a/global.json
	+++ b/global.json
	@@ -1,8 +1,8 @@
	 {
	   "msbuild-sdks": {
	-    "Uno.Sdk": "6.3.28"
	+    "Uno.Sdk.Private": "6.4.0-dev.244"
	   },
	   "sdk": {
	-    "allowPrerelease": false
	+    "allowPrerelease": true
	   }
	 }
	EOF
	git apply < patch

Build it with .NET 10 RC2:

	dotnet publish -c Release -r osx-x64 \
	  -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop \
	  -p:SelfContained=true -p:PublishAot=true -p:UseSkiaRendering=true \
	  Chefs/Chefs.csproj -bl

Run it:

	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

Watch it blow up (?!):

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '…/uno.chefs/Chefs/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|247_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	zsh: abort      Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

What's happening is:

 1. unoplatform/uno@1003d8cf introduced the use of module initializers
    into `Uno.UI.Runtime.Skia.Win32.Support.dll`.

 2. *Because* `Program.cs` has a `.UseWin32()` method invocation, there
    is (eventually) an assembly reference to
    `Uno.UI.Runtime.Skia.Win32.Support.dll`.

 3. Under NativeAOT, [module initializers are run during startup][0].
    ***All*** of them.

Which means that when building a NativeAOT app for macOS, the module
initializers from `Uno.UI.Runtime.Skia.Win32.Support.dll` are executed.

The "fix" (workaround) is to *remove* the call to `.UseWin32()`.

(A *better* fix would be to remove the module initializer, maybe?)

Update the `AdjustCalabash` target to update `$(DefineConstants)` based
on the `$(RuntimeIdentifier)` value, when set, so that it contains a
"#define-safe" `RUNTIME_IDENTIFIER_`-prefixed value based on
`$(RuntimeIdentifier)`.  For example, in `dotnet build -r osx-x64`,
both `RUNTIME_IDENTIFIER_OSX` and `RUNTIME_IDENTIFIER_OSX_X64` will be
defined during compilation.

See also the [.NET RID Catalog][1], from which we see that the
`$(RuntimeIdentifier)` values for Windows begin with `win-`.

Surround the `.UseWin32()` invocation with `RUNTIME_IDENTIFIER_WIN`,
so that it isn't called when running on e.g. macOS.

That fixes the `DllNotFoundException`.

The next problem is that, while it no longer crashes, it doesn't
*show* anything either.

Update `Program.cs` to call `App.InitializeLogging()`, so that we can
see log messages printed to `stdout`.

*Now* we can see the next problem:

	System.InvalidOperationException: A suitable constructor for type 'Uno.Extensions.Navigation.Navigators.ContentControlNavigator' could not be located.
	  Ensure the type is concrete and services are registered for all parameters of a public constructor.
	   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, ServiceIdentifier, Type, CallSiteChain) + 0x3e9
	   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateExact(ServiceDescriptor, ServiceIdentifier, CallSiteChain, Int32) + 0x181
	   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceIdentifier, CallSiteChain) + 0x14f
	   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain) + 0xc9
	   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceIdentifier, CallSiteChain) + 0x92
	   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier) + 0x68
	   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xd2
	   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier, ServiceProviderEngineScope) + 0x39
	   at Uno.Extensions.Navigation.NavigatorFactory.CreateService(IRegion) + 0xe2
	   at Uno.Extensions.Navigation.Regions.NavigationRegion.InitializeRootRegion(IServiceProvider) + 0x61
	   at Uno.Extensions.Navigation.Regions.NavigationRegion..ctor(ILogger, FrameworkElement, IServiceProvider) + 0x2ee
	   at Uno.Extensions.Navigation.FrameworkElementExtensions.HostAsync(FrameworkElement, IServiceProvider, String, Type, Type, Func`3) + 0xbc
	   at Uno.Extensions.ServiceProviderExtensions.<BuildAndInitializeHostAsync>d__5.MoveNext() + 0x18f

Which is where everything goes off the rails: the Chefs app is
quite dynamic, whereas NativeAOT *isn't*; the trimmer is removing
lots of "necessary" code.

Square that circle by (more or less) preserving "everything": add a
`@(TrimmerRootAssembly)` item group which lists (almost) everything
that the trimmer needs to preserve.  (And then some.  This is *not*
a minimal group; this is the "biggest small" group I could get building
and running properly in a short timeframe.  It likely can be shrunk.)

Notably absent: everything that references
`Uno.UI.Runtime.Skia.Win32.Support.dll` (need to allow the app to
start!), all `System` assemblies, lots of `Microsoft.*` assemblies…

*Now* the app is able to launch and run correctly.

…but at what cost?

`Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs` is a 121MB
binary.

The `Chefs/bin/Release/net10.0-desktop/osx-x64/publish` directory is
810MB in size, though that also contains artifacts that don't appear
relevant for macOS, e.g. `libvlc/win-x64`; the `libvlc` directory
is 197MB, and doesn't immediately appear to be needed (i.e. the app
launches and is usable if `libvlc` doesn't exist).

Compare to a `-p:SelfContained=true` build which *doesn't* use
NativeAOT, and we get:

| Configuration                               |          `Chefs` size |  `publish` size |
| ------------------------------------------- | --------------------: | --------------: |
| `-p:SelfContained=true`                     |        92856 <br> 91K |            444M |
| `-p:SelfContained=true -p:PublishAot=true`  |   126703200 <br> 121M |            810M |

The non-NativeAOT build *also* contains a `libvlc` directory…

The next obvious question is "what does startup look like?"
Unfortunately that information doesn't appear to be collected by the
operating system, so I either use a stopwatch (lol) or by "feels".
NativeAOT "feels" faster, but it's not dramatic.  It feels hundredths
of a second faster to start and react to button clicks.

[0]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs#L163-L169]
[1]: https://learn.microsoft.com/en-us/dotnet/core/rid-catalog
@unodevops

Copy link
Copy Markdown

⚠️⚠️ The build 181136 has failed on Uno Chefs - CI.

jonpryor added a commit to unoplatform/uno that referenced this pull request Nov 7, 2025
("Module initializers considered possibly harmful?")

This reverts commit 1003d8c.

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When built for NativeAOT, the
`.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544.

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

"Partially revert" part of #20443, returning to an earlier approach
wherein the DPI code is done within `Win32Host`, avoiding the use of
module initializers.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
jonpryor added a commit to unoplatform/uno that referenced this pull request Nov 7, 2025
("Module initializers considered possibly harmful?")

This reverts commit 1003d8c.

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When built for NativeAOT, the
`.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544.

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

"Partially revert" part of #20443, returning to an earlier approach
wherein the DPI code is done within `Win32Host`, avoiding the use of
module initializers.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
jonpryor added a commit to unoplatform/uno that referenced this pull request Nov 7, 2025
("Module initializers considered problematic?")

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When run under NativeAOT:

	dotnet publish src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/*.csproj \
	  -c Release -r osx-x64 -p:UnoTargetFrameworkOverride=net10.0-desktop -f net10.0-desktop \
	  -p:SelfContained=true -p:PublishAot=true -bl

	src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent

the `.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544:

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	…

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

Update `DpiBootstrap` to wrap the `SetProcessDpiAwarenessContext()`
call in a `if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))`
block[^0].  This ensures the code is only run on Windows, and should
avoid the crash on non-Windows platforms.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
[3]: https://github.com/unoplatform/uno.licensing/tree/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings
[4]: https://github.com/unoplatform/uno.licensing/blob/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings/Uno.Settings/Platforms/Desktop/Program.cs#L17-L23

[^0]: An alternate approach was to remove the module initializer
      completely, and instead rely on `Win32Host` to init DPI.
      The problem with this approach is the [Uno.Settings app][3]:
      there is a requirement that it be launchable as
      `dotnet Uno.Settings.dll`, i.e. outside of an "app manifest"
      environment.  Additionally, DPI must be set *before* the Windows
      message loop is created.  Given that a message loop is possibly
      created [before the Uno Platform Host is created][4], the only
      reliable mechanism to ensure DPI is set properly that doesn't
      require a code change is via a module initializer.
jonpryor added a commit to unoplatform/uno that referenced this pull request Nov 7, 2025
("Module initializers considered problematic?")

Context: #20443
Context: #20544
Context: #20557
Context: unoplatform/uno.chefs#1709

[Module Initializers][0] are code that:

> …is executed at, or sometime before, first access to any static field
> or first invocation of any method defined in the module.

In a CoreCLR or MonoVM context, a module initializer is typically
executed shortly after the assembly is loaded.

In a NativeAOT context, module initializers are executed
[*at startup*][1].  All of them.  All at once (sequentially).

This is fine…until you have a module initializer which assumes the
operating system that it's executing on.

Enter PR #20443, which introduced a module initializer which assumes
Windows.

This promptly broke Linux (#20544), the fix for which (#20557) was to
"only init DpiBootstrap on Windows".

Which works…so long as you have `DpiBootstrap` in a separate assembly
which is loaded on-demand.

Enter `src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent`,
which has a `.UseWin32()` invocation.  When run under NativeAOT:

	dotnet publish src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/*.csproj \
	  -c Release -r osx-x64 -p:UnoTargetFrameworkOverride=net10.0-desktop -f net10.0-desktop \
	  -p:SelfContained=true -p:PublishAot=true -bl

	src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent

the `.UseWin32()` invocation (eventually) causes
`Uno.UI.Runtime.Skia.Win32.Support.dll` to be included in the app and
the `DpiBootstrap` module initializer executed during startup, which
promptly fails in a manner similar to #20544:

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	…

A workaround for this is to use a `#if` to exclude the `.UseWin32()`
invocation when building for non-Windows platforms.  This allows the
trimmer to remove the reference to
`Uno.UI.Runtime.Skia.Win32.Support.dll`, avoiding its module
initializer; this is the approach taken in early drafts of
unoplatform/uno.chefs#1709.

Unfortunately, this doesn't "scale": we don't want every app to wrap
`.UseWin32()` invocations with a `#if`.

Update `DpiBootstrap` to wrap the `SetProcessDpiAwarenessContext()`
call in a `if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))`
block[^0].  This ensures the code is only run on Windows, and should
avoid the crash on non-Windows platforms.

Test this scenario by updating `run-netcore-mobile-template-tests.ps1`
to allow running a binary after the build and before the clean.

Update `src/SolutionTemplate/5.6/uno56netcurrent` to support a new
`--exit` command-line argument; when provided, `App.OnLaunched()` will
call `Application.Exit()`, causing the app to exit.  This allows us to
start the app and have it exit on CI.  If it crashes, then the exit
code will be a non-zero value, e.g.

	Executing: 5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/uno56netcurrent --exit

	Unhandled exception. System.DllNotFoundException: Unable to load shared library 'USER32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable:
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll.dylib' (no such file)
	dlopen(USER32.dll.dylib, 0x0001): tried: 'USER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll.dylib' (no such file), '/usr/lib/USER32.dll.dylib' (no such file, not in dyld cache), 'USER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll.dylib' (no such file)
	dlopen(libUSER32.dll.dylib, 0x0001): tried: 'libUSER32.dll.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll.dylib' (no such file), '/usr/lib/libUSER32.dll.dylib' (no such file, not in dyld cache), 'libUSER32.dll.dylib' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/USER32.dll' (no such file)
	dlopen(USER32.dll, 0x0001): tried: 'USER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSUSER32.dll' (no such file), '/usr/lib/USER32.dll' (no such file, not in dyld cache), 'USER32.dll' (no such file)
	dlopen(/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll, 0x0001): tried: '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file), '/Users/runner/work/1/s/src/SolutionTemplate/5.6/uno56netcurrent/uno56netcurrent/bin/Release/net10.0-desktop/osx-x64/publish/libUSER32.dll' (no such file)
	dlopen(libUSER32.dll, 0x0001): tried: 'libUSER32.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibUSER32.dll' (no such file), '/usr/lib/libUSER32.dll' (no such file, not in dyld cache), 'libUSER32.dll' (no such file)

	   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x12c
	   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
	   at Windows.Win32.PInvoke.<SetProcessDpiAwarenessContext>g__LocalExternFunction|267_0(DPI_AWARENESS_CONTEXT) + 0x22
	   at Windows.Win32.PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT) + 0x1b
	   at DpiBootstrap.Init() + 0x10
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x45
	   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x37
	…
	Exception: /Users/runner/work/1/s/build/test-scripts/run-netcore-mobile-template-tests.ps1:13
	Line |
	  13 |          throw "Exit code must be zero."
	     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	     | Exit code must be zero.

[0]: https://github.com/dotnet/runtime/blob/2e85cbda5839dc36705686fc4a3d6b50f2cef5a9/docs/design/specs/Ecma-335-Augments.md#module-initializer
[1]: https://github.com/dotnet/runtime/blob/b1cb4c502f1eec66227a31e705934fb67fc091f0/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/tartupCodeHelpers.cs#L163-L169%5D
[2]: https://github.com/unoplatform/uno.chefs/blob/af0a0c928337688c5ed3e87c3389a1cfdad46933/Chefs/Platforms/Desktop/Program.cs#L15
[3]: https://github.com/unoplatform/uno.licensing/tree/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings
[4]: https://github.com/unoplatform/uno.licensing/blob/90428261b08e4115e9ce1f08d2799e2451340491/src/Uno.Settings/Uno.Settings/Platforms/Desktop/Program.cs#L17-L23

[^0]: An alternate approach was to remove the module initializer
      completely, and instead rely on `Win32Host` to init DPI.
      The problem with this approach is the [Uno.Settings app][3]:
      there is a requirement that it be launchable as
      `dotnet Uno.Settings.dll`, i.e. outside of an "app manifest"
      environment.  Additionally, DPI must be set *before* the Windows
      message loop is created.  Given that a message loop is possibly
      created [before the Uno Platform Host is created][4], the only
      reliable mechanism to ensure DPI is set properly that doesn't
      require a code change is via a module initializer.
jonpryor added a commit to unoplatform/uno.extensions that referenced this pull request Nov 25, 2025
Context: unoplatform/uno.chefs#1709

While running the uno.chefs app on macOS under NativeAOT:

	dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
	  Chefs/Chefs.csproj \
	  -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
	  -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
	  -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

(along with a change from unoplatform/uno.chefs#1709 to `Program.cs`
to call `App.InitializeLogging()`…)

Console output would contain the following warnings:

	fail: Uno.UI.Dispatching.NativeDispatcher[0]
	      NativeDispatcher unhandled exception
	      System.InvalidOperationException: A suitable constructor for type 'Chefs.Presentation.ShellViewModel' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

This is actually fixable within uno.extensions!  The `ShellViewModel`
type is referenced by *both* `[Model]` and `IModel<ShelViewModel>`
in generated code:

	// Chefs.Presentation.ShellModel.g.cs
	namespace Chefs.Presentation;
	[Model(typeof(ShellViewModel))]
	partial record ShellModel : IModel<ShellViewModel> {
	}

`ModelAttribute` and `IModel<TViewModel>` thus give us places to tell
NativeAOT to preserve the constructors on `ShellViewModel`.

After having done so, the new failure message appears:

      	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [Pages] property getter does not exist on type [Chefs.Presentation.WelcomeViewModel]

This can *also* be fixed by telling NativeAOT to preserve
*properties* as well, as `WelcomeViewModel` goes through similar
infrastructure as `ShellViewModel`.

Update `ModelAttribute` so that the `Type bindable` constructor
parameter preserves both constructors and properties.

Update `IModel<TViewModel>` *for consistency* with `[Model]` to also
preserve constructor and properties on type parameter `TViewModel`.

TODO: this fixes `ShellViewModel` and similar constructs, but next
we have:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

There are no `[Model]` or `IModel<TViewModel>` uses which mention
`BindableIntIteratorViewModel`, so the current fix doesn't help.
Investigate what needs to be done to fix `BindableIntIteratorViewModel`.
jonpryor added a commit to unoplatform/uno.extensions that referenced this pull request Nov 26, 2025
Context: unoplatform/uno.chefs#1709

While running the uno.chefs app on macOS under NativeAOT:

	dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
	  Chefs/Chefs.csproj \
	  -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
	  -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
	  -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

(along with a change from unoplatform/uno.chefs#1709 to `Program.cs`
to call `App.InitializeLogging()`…)

Console output would contain the following warnings:

	fail: Uno.UI.Dispatching.NativeDispatcher[0]
	      NativeDispatcher unhandled exception
	      System.InvalidOperationException: A suitable constructor for type 'Chefs.Presentation.ShellViewModel' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

This is actually fixable within uno.extensions!  The `ShellViewModel`
type is referenced by *both* `[Model]` and `IModel<ShelViewModel>`
in generated code:

	// Chefs.Presentation.ShellModel.g.cs
	namespace Chefs.Presentation;
	[Model(typeof(ShellViewModel))]
	partial record ShellModel : IModel<ShellViewModel> {
	}

`ModelAttribute` and `IModel<TViewModel>` thus give us places to tell
NativeAOT to preserve the constructors on `ShellViewModel`.

After having done so, the new failure message appears:

      	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [Pages] property getter does not exist on type [Chefs.Presentation.WelcomeViewModel]

This can *also* be fixed by telling NativeAOT to preserve
*properties* as well, as `WelcomeViewModel` goes through similar
infrastructure as `ShellViewModel`.

Update `ModelAttribute` so that the `Type bindable` constructor
parameter preserves both constructors and properties.

Update `IModel<TViewModel>` *for consistency* with `[Model]` to also
preserve constructor and properties on type parameter `TViewModel`.

TODO: this fixes `ShellViewModel` and similar constructs, but next
we have:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

There are no `[Model]` or `IModel<TViewModel>` uses which mention
`BindableIntIteratorViewModel`, so the current fix doesn't help.
Investigate what needs to be done to fix `BindableIntIteratorViewModel`.
jonpryor added a commit to unoplatform/uno.extensions that referenced this pull request Dec 1, 2025
Context: unoplatform/uno.chefs#1709

While running the uno.chefs app on macOS under NativeAOT:

	dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
	  Chefs/Chefs.csproj \
	  -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
	  -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
	  -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

(along with a change from unoplatform/uno.chefs#1709 to `Program.cs`
to call `App.InitializeLogging()`…)

Console output would contain the following warnings:

	fail: Uno.UI.Dispatching.NativeDispatcher[0]
	      NativeDispatcher unhandled exception
	      System.InvalidOperationException: A suitable constructor for type 'Chefs.Presentation.ShellViewModel' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

This is actually fixable within uno.extensions!  The `ShellViewModel`
type is referenced by *both* `[Model]` and `IModel<ShelViewModel>`
in generated code:

	// Chefs.Presentation.ShellModel.g.cs
	namespace Chefs.Presentation;
	[Model(typeof(ShellViewModel))]
	partial record ShellModel : IModel<ShellViewModel> {
	}

`ModelAttribute` and `IModel<TViewModel>` thus give us places to tell
NativeAOT to preserve the constructors on `ShellViewModel`.

After having done so, the new failure message appears:

      	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [Pages] property getter does not exist on type [Chefs.Presentation.WelcomeViewModel]

This can *also* be fixed by telling NativeAOT to preserve
*properties* as well, as `WelcomeViewModel` goes through similar
infrastructure as `ShellViewModel`.

Update `ModelAttribute` so that the `Type bindable` constructor
parameter preserves both constructors and properties.

Update `IModel<TViewModel>` *for consistency* with `[Model]` to also
preserve constructor and properties on type parameter `TViewModel`.

TODO: this fixes `ShellViewModel` and similar constructs, but next
we have:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

There are no `[Model]` or `IModel<TViewModel>` uses which mention
`BindableIntIteratorViewModel`, so the current fix doesn't help.
Investigate what needs to be done to fix `BindableIntIteratorViewModel`.
jonpryor added a commit to unoplatform/uno.extensions that referenced this pull request Dec 1, 2025
Context: unoplatform/uno.chefs#1709

While running the uno.chefs app on macOS under NativeAOT:

	dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
	  Chefs/Chefs.csproj \
	  -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
	  -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
	  -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

(along with a change from unoplatform/uno.chefs#1709 to `Program.cs`
to call `App.InitializeLogging()`…)

Console output would contain the following warnings:

	fail: Uno.UI.Dispatching.NativeDispatcher[0]
	      NativeDispatcher unhandled exception
	      System.InvalidOperationException: A suitable constructor for type 'Chefs.Presentation.ShellViewModel' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

This is actually fixable within uno.extensions!  The `ShellViewModel`
type is referenced by *both* `[Model]` and `IModel<ShelViewModel>`
in generated code:

	// Chefs.Presentation.ShellModel.g.cs
	namespace Chefs.Presentation;
	[Model(typeof(ShellViewModel))]
	partial record ShellModel : IModel<ShellViewModel> {
	}

`ModelAttribute` and `IModel<TViewModel>` thus give us places to tell
NativeAOT to preserve the constructors on `ShellViewModel`.

After having done so, the new failure message appears:

      	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [Pages] property getter does not exist on type [Chefs.Presentation.WelcomeViewModel]

This can *also* be fixed by telling NativeAOT to preserve
*properties* as well, as `WelcomeViewModel` goes through similar
infrastructure as `ShellViewModel`.

Update `ModelAttribute` so that the `Type bindable` constructor
parameter preserves both constructors and properties.

Update `IModel<TViewModel>` *for consistency* with `[Model]` to also
preserve constructor and properties on type parameter `TViewModel`.

TODO: this fixes `ShellViewModel` and similar constructs, but next
we have:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

There are no `[Model]` or `IModel<TViewModel>` uses which mention
`BindableIntIteratorViewModel`, so the current fix doesn't help.
Investigate what needs to be done to fix `BindableIntIteratorViewModel`.
jonpryor added a commit to unoplatform/uno.extensions that referenced this pull request Dec 2, 2025
Context: #2966
Context: unoplatform/uno.chefs#1709

While running the uno.chefs app on macOS under NativeAOT:

	dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
	  Chefs/Chefs.csproj \
	  -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
	  -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
	  -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
	Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs

(along with a change from unoplatform/uno.chefs#1709 to `Program.cs`
to call `App.InitializeLogging()`…)

Console output would contain the following error:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]
	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The property setter for [CurrentIndex] does not exist on [Chefs.Business.Models.BindableIntIteratorViewModel]
	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [Value] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

PR #2966 had a similar set of messages, and the general fix is to use
`[DynamicallyAccessedMembers]` so that NativeAOT preserves property
metadata.

The problem *here* is that there is no good place to put
`[DynamicallyAccessedMembers]` that will preserve the ViewModel types
generated by Uno.Extensions.Reactive.Generator:

	namespace Chefs.Business.Models {
	  [GeneratedCodeAttribute("ViewModelGenerator_2", "2")]
	  [Bindable(typeof(IntIterator))]
	  public partial class BindableIntIteratorViewModel : global::Uno.Extensions.Reactive.Bindings.Bindable<Chefs.Business.Models.IntIterator> {
	    public IntIterator Value {get => …; set => …;}
	    public int CurrentIndex {get => …; set => …;}
	  }
	}
	namespace Chefs.Presentation {
	  [GeneratedCodeAttribute("ViewModelGenTool_3", "3")]
	  [Bindable(typeof(global::Chefs.Presentation.WelcomeModel))]
	  public partial class WelcomeViewModel {
	    protected WelcomeViewModel(global::Chefs.Presentation.WelcomeModel model) {
	      // …
	      Pages ??= new Chefs.Business.Models.BindableIntIteratorViewModel(…);
	      // …
	    }
	    public Chefs.Business.Models.BindableIntIteratorViewModel Pages { get; private set; }
	  }
	}

Nothing tells NativeAOT to preserve the `BindableIntIteratorViewModel`
properties, so they're "removed" from Reflection metadata.

Update `BindableFromFeedProperty.cs` to use `[DynamicDependency]`
on the `WelcomeViewModel.Pages` property accessor:

	namespace Chefs.Presentation {
	  public partial class WelcomeViewModel {
	    public Chefs.Business.Models.BindableIntIteratorViewModel Pages
	    {
	      [DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(Chefs.Business.Models.BindableIntIteratorViewModel))]
	      get;
	      private set;
	    }
	  }
	}

This change informs NativeAOT that if the `WelcomeViewModel.Pages`
property getter is preserved, then the public properties on
`BindableIntIteratorViewModel` should also be made accessible via
Reflection.  This fixes the original message:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CurrentIndex] property getter does not exist on type [Chefs.Business.Models.BindableIntIteratorViewModel]

…only to replace it with this *different* set of failure messages:

	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CanMovePrevious] property getter does not exist on type [Chefs.Business.Models.IntIterator]
	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CanMovePrevious] property getter does not exist on type [Chefs.Business.Models.IntIterator]
	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CanMoveNext] property getter does not exist on type [Chefs.Business.Models.IntIterator]
	fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
	      The [CanMoveNext] property getter does not exist on type [Chefs.Business.Models.IntIterator]

Address this new set of messages by updating `Bindable<T>` to

	partial class Bindable<
	  [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties)]
	  T
	> : IBindable
	{
	}

As `BindableIntIteratorViewModel` inherits from `Bindable<IntIterator>`
this causes the public properties on `IntIterator` to be preserved,
fixing the previous set of failure messages.
jonpryor added a commit that referenced this pull request Dec 2, 2025
Context: #1709

It is extremely helpful to have log messages written to stdout
when running on -desktop platforms such as macOS.

Update `Desktop/Program.cs` to call `App.InitializeLogging()` so that
messages are written to stdout.
@jonpryor

Copy link
Copy Markdown
Collaborator Author

This PR is no longer necessary; superseded by #1712, various changes to dependencies.

@jonpryor jonpryor closed this Jan 13, 2026
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.

2 participants