Skip to content

NativeAOT: ILCompiler crash InvalidCastException in EnsureLoadableType when publishing NativeLib on .NET 9.0.305 #120178

@Program66

Description

@Program66

Description

Publishing the native bridge project in cxxx with NativeAOT on .NET 9.0.305 always crashes ILCompiler. Using the exact same project and command with the .NET 10.0.100‑rc.1 SDK succeeds, so the fix appears to be in a later toolchain, but the .NET 9 line still crashes.

Reproduction Steps

Clone the repo (native project: srcxxx.PythonBridge.Native).

Ensure global.json points to SDK 9.0.305.

From repo root run:

dotnet clean src\cxxx.PythonBridge.Native\cxxx.PythonBridge.Native.csproj -c Release
dotnet publish src\cxxx.PythonBridge.Native\cxxx.PythonBridge.Native.csproj -c Release -r win-x64
-p:PublishAot=true -p:NativeLib=Shared -p:SelfContained=true `
-v:diag -bl:NativeAot.binlog
(Optional confirmation) Repeat with SDK 10.0.100-rc.1.25451.107 – publish succeeds there.

Expected behavior

NativeAOT publish completes, producing a native DLL (or, at minimum, fails with a diagnostic pointing to the problematic code).

Actual behavior

On .NET 9.0.305 the publish reaches “Generating native code” and ILCompiler exits with:

System.InvalidCastException: Specified cast is not valid.
at ILCompiler.CompilerTypeSystemContext.EnsureLoadableTypeUncached(TypeDesc)
at ILCompiler.CompilerTypeSystemContext.EnsureLoadableType(TypeDesc)
at ILCompiler.LibraryRootProvider.CheckCanGenerateMethod(MethodDesc)
at ILCompiler.DependencyAnalysis.TypeMetadataNode.GetStaticDependencies(NodeFactory)
at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.GetStaticDependenciesImpl(DependencyNodeCore1)
at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.GetStaticDependencies(DependencyNodeCore1)
at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.ProcessMarkStack() at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.ComputeMarkedNodes()
at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan()
at ILCompiler.Program.g__RunScanner|4_0(...)
MSB3073: "...ilc @obj\Release\net9.0\win-x64\native\cxxx.PythonBridge.Native.ilc.rsp" exited with code 1.
The same command succeeds with SDK 10.0.100-rc.1.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions