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

NativeAOT Windows - System.InvalidCastException: Specified cast is not valid #89672

Closed
e012345678 opened this issue Jul 29, 2023 · 11 comments
Closed

Comments

@e012345678
Copy link

I have followed the guide here https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7 to try compile our app with Native AOT on .NET 8 Preview 6. However, I am getting this error:

EXEC : error : Specified cast is not valid. [C:\git\MyApp.csp
roj::TargetFramework=net8.0-windows10.0.19041.0]
  System.InvalidCastException: Specified cast is not valid.
     at System.Runtime.TypeCast.CheckCastClass(MethodTable*, Object) + 0x31
     at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0x132
     at ILCompiler.MetadataManager.IsReflectionBlocked(Instantiation) + 0x3d
     at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0xfc
     at ILCompiler.UsageBasedMetadataManager.HasConditionalDependenciesDueToEETypePresence(TypeDesc) + 0xec
     at ILCompiler.DependencyAnalysis.EETypeNode..ctor(NodeFactory, TypeDesc) + 0x10a
     at ILCompiler.DependencyAnalysis.NodeFactory.CreateNecessaryTypeNode(TypeDesc) + 0xfb
     at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) + 0x11
     at ILCompiler.DependencyAnalysis.NodeFactory.NecessaryTypeSymbol(TypeDesc) + 0xe7
     at ILCompiler.DependencyAnalysis.NonGCStaticsNode.GetConditionalStaticDependencies(NodeFactory) + 0x32
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1) + 0
  xf3
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1) + 0x2b
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() + 0xb5
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x50
     at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x14
     at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x192
     at ILCompiler.Program.Run() + 0x1f9b
     at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass215_0.<.ctor>b__0(InvocationContext context) + 0x2f8

How can I find out more information of what this means and how it can be fixed? The publish command I used is: dotnet publish -f:net8.0-windows10.0.19041.0 -r:win10-x64 -c:Release

@jkotas jkotas transferred this issue from dotnet/runtimelab Jul 29, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 29, 2023
@ghost
Copy link

ghost commented Jul 29, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

I have followed the guide here https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7 to try compile our app with Native AOT on .NET 8 Preview 6. However, I am getting this error:

EXEC : error : Specified cast is not valid. [C:\git\MyApp.csp
roj::TargetFramework=net8.0-windows10.0.19041.0]
  System.InvalidCastException: Specified cast is not valid.
     at System.Runtime.TypeCast.CheckCastClass(MethodTable*, Object) + 0x31
     at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0x132
     at ILCompiler.MetadataManager.IsReflectionBlocked(Instantiation) + 0x3d
     at ILCompiler.MetadataManager.IsReflectionBlocked(TypeDesc) + 0xfc
     at ILCompiler.UsageBasedMetadataManager.HasConditionalDependenciesDueToEETypePresence(TypeDesc) + 0xec
     at ILCompiler.DependencyAnalysis.EETypeNode..ctor(NodeFactory, TypeDesc) + 0x10a
     at ILCompiler.DependencyAnalysis.NodeFactory.CreateNecessaryTypeNode(TypeDesc) + 0xfb
     at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) + 0x11
     at ILCompiler.DependencyAnalysis.NodeFactory.NecessaryTypeSymbol(TypeDesc) + 0xe7
     at ILCompiler.DependencyAnalysis.NonGCStaticsNode.GetConditionalStaticDependencies(NodeFactory) + 0x32
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1) + 0
  xf3
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1) + 0x2b
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() + 0xb5
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x50
     at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x14
     at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x192
     at ILCompiler.Program.Run() + 0x1f9b
     at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass215_0.<.ctor>b__0(InvocationContext context) + 0x2f8

How can I find out more information of what this means and how it can be fixed? The publish command I used is: dotnet publish -f:net8.0-windows10.0.19041.0 -r:win10-x64 -c:Release

Author: e012345678
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jul 29, 2023

@e012345678 is that app public?

@e012345678
Copy link
Author

Hi @EgorBo thanks for the reply, unfortunately no.

@am11
Copy link
Member

am11 commented Jul 29, 2023

dotnet publish -f:net8.0-windows10.0.19041.0 -r:win10-x64 -c:Release

The framework (-f) value has TPI, which suggests that your app is using CsWinRT. This issue was recently fixed downstream: #68278 (comment)). Currently, CsWinRT does not fully support AOT, so even after its next release, chances are your app may not build or run. You can follow the progress in their issue tracker for AOT support: https://github.com/microsoft/CsWinRT/labels/AOT.

@e012345678
Copy link
Author

Thanks for the reply @am11 . Why is the NativeAOT marketed as it supports Windows (on the link in my first post) but it does not due to "CsWinRT does not fully support AOT". What fine print/requirements am I missing?

@am11
Copy link
Member

am11 commented Jul 30, 2023

What fine print/requirements am I missing?

If your project or one of its dependencies are relying on one of the limitations: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net8plus#limitations-of-native-aot-deployment, publishing as AOT app will not work.

@e012345678
Copy link
Author

Hi @am11, is there anyway I can find out which of my dependencies have this limitation? Basically "what part of my app do I need to remove?"

@MichalPetryka
Copy link
Contributor

is there anyway I can find out which of my dependencies have this limitation?

Putting those options in your csproj should enable all the analyzers that will log such info at build time:

        <PublishAot>true</PublishAot>
        <EnableAOTAnalyzer>true</EnableAOTAnalyzer>
        <EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
        <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
        <SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
        <TrimmerSingleWarn>false</TrimmerSingleWarn>

@MichalStrehovsky
Copy link
Member

Putting those options in your csproj should enable all the analyzers that will log such info at build time:

PublishAot is enough to enable all the analyzers. The last two change analysis behaviors and should be set if one want such behaviors.

I'm closing this Won't fix with the same justification as #68278 (comment)

@MichalStrehovsky MichalStrehovsky closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 31, 2023
@Sergio0694
Copy link
Contributor

Sergio0694 commented Aug 17, 2023

Getting the same build error with CsWinRT 2.0.4-prerelease.230731.4, which should have the fix from microsoft/CsWinRT#1340 (also mentioned in #68278). Can't build a blank WinUI 3 app with NativeAOT at all. FYI @manodasanW 🤔

Can also open a new issue in the CsWinRT repo to track this there.

@manodasanW
Copy link
Contributor

Getting the same build error with CsWinRT 2.0.4-prerelease.230731.4, which should have the fix from microsoft/CsWinRT#1340 (also mentioned in #68278). Can't build a blank WinUI 3 app with NativeAOT at all. FYI @manodasanW 🤔

Can also open a new issue in the CsWinRT repo to track this there.

You will need an updated Windows SDK projection with that CsWinRT version.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants