Skip to content

AOT Compatibility: Fix Trim Analysis and Dynamic Code Warnings (56 warnings) #7901

@Aaronontheweb

Description

@Aaronontheweb

Overview

This issue tracks the resolution of AOT compilation warnings discovered during Native AOT compatibility testing. Currently, the AOT test app produces 56 warnings and fails at runtime.

Related Epic: #7246

Current Status

  • Warning Count: 56 (Target: 0)
  • Runtime Status: ❌ Fails with ConfigurationException for actor provider type resolution

Error Categories

1. Trim Analysis Warnings - Type.GetType() (IL2057)

These occur when using Type.GetType(string) with dynamic type names from configuration, making it impossible for the trimmer to guarantee type availability.

Affected Files:

  • ActorSystemImpl.cs:300 - LoadExtensions()
  • ActorSystemImpl.cs:285 - ConfigureScheduler()
  • ActorSystemImpl.cs:447 - ConfigureProvider()
  • Mailboxes.cs:66 - Constructor
  • Serialization.cs:183 - Constructor (serializers)
  • Serialization.cs:210 - Constructor (serialization bindings)
  • Settings.cs:104 - Constructor (LoggerType)
  • Settings.cs:142 - Constructor (StdoutLogger)
  • Settings.cs:174 - Constructor (LoggerStartTimeout)
  • Dispatchers.cs:607 - ConfiguratorFrom()
  • Serializer.cs:200 - GetSerializerIdentifierFromConfig()
  • AbstractDispatcher.cs:343 - MessageDispatcherConfigurator.ConfigureExecutor()

2. Trim Analysis Warnings - Activator.CreateInstance (IL2067, IL2087)

These occur when creating instances without proper DynamicallyAccessedMembers annotations.

Affected Files:

  • Props.cs:587 - CreateProducer() - Missing PublicConstructors annotation
  • Extensions.cs:136 - WithExtension<T,TI>() - Missing PublicParameterlessConstructor annotation
  • Extensions.cs:116 - WithExtension() - Missing PublicParameterlessConstructor annotation

3. AOT Analysis Warnings - System.Linq.Expressions (IL3050)

These are from BCL code using Type.MakeGenericType() which requires dynamic code generation. These may be acceptable if expression compilation isn't used in AOT scenarios.

Files: DelegateHelpers.Generated.cs (40 warnings in BCL code)

Action Items

High Priority

  • Add DynamicDependency and UnconditionalSuppressMessage attributes for known-safe type resolutions
  • Implement AOT-compatible type registration system (potentially using source generators)
  • Add DynamicallyAccessedMembers annotations to Props.CreateProducer() parameter
  • Add DynamicallyAccessedMembers annotations to extension methods in Extensions.cs
  • Fix runtime error: ConfigurationException for 'Akka.Actor.LocalActorRefProvider'

Medium Priority

  • Investigate expression compilation usage - determine if we can avoid it in AOT scenarios
  • Create AOT-specific configuration validator to catch type resolution issues at build time
  • Document which Akka.NET features are/aren't AOT compatible

Low Priority

  • Evaluate if System.Linq.Expressions warnings can be suppressed (if not used in AOT paths)
  • Add AOT compatibility tests to CI pipeline

Runtime Error Details

Unhandled Exception: Akka.Configuration.ConfigurationException: 
'akka.actor.provider' is not a valid type name : 'Akka.Actor.LocalActorRefProvider'
   at Akka.Actor.Settings..ctor(ActorSystem, Config, ActorSystemSetup)

This suggests the type resolution mechanism fails completely in AOT scenarios. The provider type cannot be loaded dynamically.

Technical Approach

Option 1: Source Generator for Type Registration

  • Create a source generator that scans configuration and generates static type mappings
  • Eliminates runtime Type.GetType() calls

Option 2: Explicit Type Registration API

  • Add ActorSystemSetup builders that accept concrete types instead of type names
  • Provides AOT-friendly configuration path

Option 3: Hybrid Approach

  • Keep existing API for non-AOT scenarios
  • Add AOT-specific setup path with compile-time type registration
  • Use #if directives or feature detection

Success Criteria

  • All 56 warnings resolved or properly suppressed with justification
  • AOT test app runs successfully without runtime exceptions
  • Core actor system functionality works in AOT scenarios
  • Documentation updated with AOT compatibility guidelines

Metadata

Metadata

Assignees

No one assigned

    Labels

    AOTAhead-of-Time (AOT) Compilationenhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions