Repository navigation
Add DPI awareness - #20443
Add DPI awareness#20443
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR addresses DPI scaling issues for processes launched via the .NET CLI by adding proper DPI awareness and improving related XAML method naming. Key changes include:
- Introducing a new method SetProcessDpiAwareness() to set per-monitor DPI awareness.
- Logging an error if setting DPI awareness fails.
- Updating NativeMethods.txt to include the new SetProcessDpiAwarenessContext API.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Uno.UI.Runtime.Skia.Win32/Win32Host.cs | Added SetProcessDpiAwareness() method and its invocation in the initialization routine. |
| src/Uno.UI.Runtime.Skia.Win32.Support/NativeMethods.txt | Documented the SetProcessDpiAwarenessContext API for native interop. |
Comments suppressed due to low confidence (1)
src/Uno.UI.Runtime.Skia.Win32.Support/NativeMethods.txt:196
- [nitpick] Ensure that the inclusion of SetProcessDpiAwarenessContext in this file is complemented by updates in the corresponding documentation, so its purpose and usage are clearly communicated.
SetProcessDpiAwarenessContext
|
@MartinZikmund @dr1rrb Please review this. It solve the problem when the process is started using This change MAY break uno island, so maybe there's a better approach to it. Since the |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-20443/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-20443/docs/index.html |
|
The build 165973 found UI Test snapshots differences: Details
|
|
Seems good, but will also let @ramezgerges chime in 👍 |
Introduced SetProcessDpiAwareness() to address DPI scaling issues when running from dotnet CLI.
Move DPI awareness setup to a dedicated `DpiBootstrap` class using a module initializer for improved clarity and reuse. This ensures the correct DPI context is applied before any window initialization, replacing redundant logic in `Win32Host`.
d728cc8 to
1003d8c
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-20443/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-20443/docs/index.html |
|
|
|
@Mergifyio backport release/stable/6.0 |
✅ Backports have been createdDetails
|
|
The build 166322 found UI Test snapshots differences: Details
|
…6.0/pr-20443 Add DPI awareness (backport #20443)
|
Seems like it broke Linux #20544 |
…rce-process-dpiawareness Add DPI awareness
("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
("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
("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.
("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.
GitHub Issue (If applicable): closes https://github.com/unoplatform/uno.hotdesign/issues/4139
Bugfix
What is the new behavior?
SetProcessDpiAwareness()to address DPI scaling issues when not defined in the process manifest (when started fromdotnet run)PR Checklist
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.