Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUI Blazor Windows App does not work with Native AOT #17713

Closed
strich opened this issue Sep 28, 2023 · 8 comments
Closed

MAUI Blazor Windows App does not work with Native AOT #17713

strich opened this issue Sep 28, 2023 · 8 comments
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) platform/windows 🪟 t/bug Something isn't working

Comments

@strich
Copy link

strich commented Sep 28, 2023

Description

I am currently developing an app with Avalonia on .NET 7 and I was curious to see if I could move to MAUI Blazor as it'd improve my productivity. I do not plan to release the app on anything but desktop platforms, and even then I may keep it to Windows.

It is important to me to have Native AOT. I got this working with Avalonia by adding the following to my csproj:

<SelfContained>false</SelfContained>
<PublishSingleFile>false</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
<TrimMode>partial</TrimMode>
<PublishAot>true</PublishAot>
<OptimizationPreference>Speed</OptimizationPreference>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

I installed the latest MAUI and created the default MAUI Blazor project in VS. I've attempted to apply the same or similar csproj settings, starting with just <PublishAot> and each step of the way the commandline publish has spit out errors. I cannot currently get it to work.

How can I get the default start up project to publish to native AOT?

Steps to Reproduce

  1. Create a new .NET Blazor MAUI App in VS.
  2. Remove all platforms from the csproj except Windows to simply things.
  3. Try to get it to compile to AOT.

Link to public reproduction project repository

No response

Version with bug

7.0.92

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@strich strich added the t/bug Something isn't working label Sep 28, 2023
@drasticactions
Copy link
Contributor

drasticactions commented Sep 28, 2023

Could you post the logs of what you're getting?

Based on my memory of this, I think WinUI doesn't support some of those features (Ex. IL Trimming). Trying to run those commands in a standard WinUI 3 app I get this

1>EXEC : error : One or more errors occurred. (Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()') (Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>ABI.Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()')
1>System.AggregateException: One or more errors occurred. (Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()') (Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>ABI.Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()')
1> ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()'
1> ---> System.NotImplementedException: CORINFO_HELP_INITCLASS
1>   at Internal.JitInterface.CorInfoImpl.GetHelperFtnUncached(CorInfoHelpFunc) + 0x922
1>   at Internal.JitInterface.CorInfoImpl.getHelperFtn(CorInfoHelpFunc, Void*&) + 0x34
1>   at Internal.JitInterface.CorInfoImpl._getHelperFtn(IntPtr, IntPtr*, CorInfoHelpFunc, Void**) + 0x40
1>   --- End of inner exception stack trace ---
1>   at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) + 0x1de
1>   at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodCodeNode, MethodIL) + 0x5c
1>   at ILCompiler.RyuJitCompilation.CompileSingleMethod(CorInfoImpl, MethodCodeNode) + 0x93
1>   at ILCompiler.RyuJitCompilation.CompileSingleMethod(MethodCodeNode methodCodeNodeNeedingCode) + 0x90
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x289
1>--- End of stack trace from previous location ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception) + 0x13
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x44a
1>   at System.Threading.Tasks.TaskReplicator.Replica.Execute() + 0x66
1>   --- End of inner exception stack trace ---
1>   at System.Threading.Tasks.TaskReplicator.Run[TState](TaskReplicator.ReplicatableUserAction`1, ParallelOptions, Boolean) + 0x150
1>   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x394
1>--- End of stack trace from previous location ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
1>   at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) + 0x30
1>   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x450
1>   at ILCompiler.RyuJitCompilation.CompileMultiThreaded(List`1) + 0x1f0
1>   at ILCompiler.RyuJitCompilation.ComputeDependencyNodeDependencies(List`1 obj) + 0x166
1>   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x95
1>   at ILCompiler.RyuJitCompilation.CompileInternal(String, ObjectDumper) + 0x1f
1>   at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String, ObjectDumper) + 0x2d
1>   at ILCompiler.Program.Run() + 0x2778
1>   at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass221_0.<.ctor>b__0(ParseResult result) + 0x315
1> ---> (Inner Exception #1) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.Windows.SDK.NET]<GuidPatcherImplementationDetails>+<SignatureCache>ABI.Windows.Foundation.TypedEventHandler`2<TSender,TResult><System.__Canon,System.__Canon>..cctor()'
1> ---> System.NotImplementedException: CORINFO_HELP_INITCLASS
1>   at Internal.JitInterface.CorInfoImpl.GetHelperFtnUncached(CorInfoHelpFunc) + 0x922
1>   at Internal.JitInterface.CorInfoImpl.getHelperFtn(CorInfoHelpFunc, Void*&) + 0x34
1>   at Internal.JitInterface.CorInfoImpl._getHelperFtn(IntPtr, IntPtr*, CorInfoHelpFunc, Void**) + 0x40
1>   --- End of inner exception stack trace ---
1>   at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) + 0x1de
1>   at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodCodeNode, MethodIL) + 0x5c
1>   at ILCompiler.RyuJitCompilation.CompileSingleMethod(CorInfoImpl, MethodCodeNode) + 0x93
1>   at ILCompiler.RyuJitCompilation.CompileSingleMethod(MethodCodeNode methodCodeNodeNeedingCode) + 0x90
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x289
1>--- End of stack trace from previous location ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception) + 0x13
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x44a
1>   at System.Threading.Tasks.TaskReplicator.Replica.Execute() + 0x66<---

Trying it with a MAUI Blazor app, I got the same errors. This is, most likely, an issue with WinUI that would need to be addressed.

edit: Since I wanted to try it, WPF and WinForms don't support trimming so you couldn't use a BlazorWebView with those either. Also, not thinking about it, I was running this in Debug, when it should really be run in Release mode. Still fails there, though

1>EXEC : error : Specified cast is not valid.
1>System.InvalidCastException: Specified cast is not valid.
1>   at System.Runtime.TypeCast.ThrowInvalidCastException(MethodTable*) + 0x16
1>   at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0x132
1>   at ILCompiler.MetadataManager.IsReflectionBlocked(Instantiation) + 0x3d
1>   at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0xfc
1>   at ILCompiler.UsageBasedMetadataManager.HasConditionalDependenciesDueToEETypePresence(TypeDesc) + 0xee
1>   at ILCompiler.DependencyAnalysis.EETypeNode..ctor(NodeFactory, TypeDesc) + 0xff
1>   at ILCompiler.DependencyAnalysis.NodeFactory.CreateNecessaryTypeNode(TypeDesc) + 0xfa
1>   at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) + 0x11
1>   at ILCompiler.DependencyAnalysis.NodeFactory.NecessaryTypeSymbol(TypeDesc) + 0xe7
1>   at ILCompiler.DependencyAnalysis.NonGCStaticsNode.GetConditionalStaticDependencies(NodeFactory) + 0x32
1>   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1) + 0xf3
1>   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1) + 0x2b
1>   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() + 0xb5
1>   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x50
1>   at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x14
1>   at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x191
1>   at ILCompiler.Program.Run() + 0x2444
1>   at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass221_0.<.ctor>b__0(ParseResult result) + 0x315

This is with a WinUI3 project, fails the same way in MAUI (which, make sense, given that it uses it for its platform head). So yeah, unless I'm missing something, I think this is a WinUI issue.

@strich
Copy link
Author

strich commented Sep 28, 2023

I figured I'd leave logs out in the initial issue coz I felt I was hitting a few different problems as I tried to add/remove the various AOT configurations, get rid of the other platforms, etc. Annoyingly I can't run it again so easily as I just tried to move to .NET 8 preview and try it there, but it doesn't work either (#17715).

Am I mistaken that MAUI was meant to be officially supporting Native AOT?

A question not exactly relevant to the issue - Is it possible to AOT compile just a C# library project within another C# project without having to treat it like a classic CPP library that requires bindings etc?

@drasticactions drasticactions changed the title .NET 7 MAUI Blazor Windows App does not work with Native AOT MAUI Blazor Windows App does not work with Native AOT Sep 28, 2023
@drasticactions
Copy link
Contributor

The same error happens with net7.0 as it does with net8.0, I believe it has to do with the WinUI dependencies, less so the runtime (Although I could be wrong here)

@jonathanpeppers @mattleibow Do ya'll have thoughts on this? Off the top of my head I don't know what's currently supported or not for the SDKs and the MAUI UI project itself. In terms of this specific issue, it does look like a WinUI one to me.

@jonathanpeppers
Copy link
Member

If the error is related to Windows.Foundation.TypedEventHandler, I think they are basically asking:

"When will WindowsAppSdk support NativeAOT?"

I think we will have to defer to the Windows team on this.

@mattleibow
Copy link
Member

If this is an issue with WASDK not supporting NativeAOT, then is it possible to open a new issue https://github.com/microsoft/WindowsAppSDK/issues

@strich
Copy link
Author

strich commented Sep 29, 2023

If this is an issue with WASDK not supporting NativeAOT, then is it possible to open a new issue https://github.com/microsoft/WindowsAppSDK/issues

Done.

@jsuarezruiz jsuarezruiz added area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/windows 🪟 labels Sep 29, 2023
@mkArtakMSFT mkArtakMSFT added area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) and removed area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Oct 10, 2023
@mattleibow
Copy link
Member

Duplicate of microsoft/WindowsAppSDK#3887

@mattleibow mattleibow marked this as a duplicate of microsoft/WindowsAppSDK#3887 Oct 24, 2023
@mattleibow
Copy link
Member

Thanks! I know the WASDK is working on better linking and AOT, so hopefully things get better soon. Right now, a WinUI app is huge.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) platform/windows 🪟 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants