Annotate core for trimming and Native AOT compatibility#1492
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1492 +/- ##
========================================
Coverage 78.07% 78.08%
========================================
Files 218 218
Lines 5944 5945 +1
Branches 1273 1273
========================================
+ Hits 4641 4642 +1
Misses 763 763
Partials 540 540 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Annotates Autofac core for trimming and Native AOT compatibility and sets
<IsAotCompatible>true</IsAotCompatible>on the modern target frameworks (net8.0/net10.0). The assembly now advertises AOT-compatibility, and the previously-opaqueIL2104/IL3053rollup warnings that downstream AOT consumers saw againstAutofac.dllare resolved.No public API is removed or changed; no runtime behavior changes for non-trimming/non-AOT consumers. All 869 existing tests pass.
What changed
Annotations (the bulk of the diff):
Resolve<T>/Resolve(Type)/TypedParameter, constructor injection, public-property injection) is made genuinely trim/AOT-clean by flowing[DynamicallyAccessedMembers]through the registration → activator chain. The agreed DAM contract for activated types isPublicConstructors | PublicProperties(centralized inUtil/ActivatorMemberTypes.cs).[RequiresDynamicCode]/[RequiresUnreferencedCode].IEnumerable<T>,Lazy<T>,Meta<T>,Owned<T>,KeyedServiceIndex<,>) use internal[UnconditionalSuppressMessage]with justifications, because marking them would taintContainerBuilder.Build()for every consumer. This keepsBuild()and the closed-type resolve path clean.Util/LinkerAttributes.csbackfills the linker attributes (DynamicallyAccessedMembers,RequiresUnreferencedCode,RequiresDynamicCode,UnconditionalSuppressMessage, and a[Flags]DynamicallyAccessedMemberTypes) for thenetstandardtargets that lack them.Build gate:
IsAotCompatible=true(net7.0+) implicitly enables the trim/AOT/single-file analyzers; combined with the existing ReleaseTreatWarningsAsErrorsthis is a permanent in-build gate against new unannotated reflection/dynamic-code sites.Verification (
VerifyAottarget +.github/workflows/aot.yml): two complementary layers, neither inAutofac.sln:test/Autofac.Test.Aot— a console app published withPublishAot=trueand then executed. The ILC publish proves static AOT-cleanliness; running the native binary proves the AOT-safe surface works at runtime (20 checks), and asserts that a value-type open-generic close correctly throws (the dynamic-code boundary is real).test/Autofac.Test.AotWarnings— a fixture that calls the[Requires*]APIs;VerifyAotWarningsasserts IL2026/IL3050 are still emitted, guarding against silently losing an annotation in a future refactor. Runs on a plain build, no native toolchain.Compatibility note for release notes
This is a minor release (9.2.0 → 9.3.0). The one thing to call out for consumers: a project that already builds with the trim/AOT analyzers enabled and treats warnings as errors may see a previously-clean build begin to fail, because APIs like
RegisterGeneric, assembly scanning, and generated factories now correctly emitIL2026/IL3050at their call sites. This is accurate new diagnostic information rather than a regression, and only affects consumers who have opted into trim/AOT analysis.What is NOT in this PR
Empirically verified AOT boundary (net10.0 / osx-arm64)
RegisterInstance, lambda registration, and reference-typed relationships (IEnumerable<T>,Lazy<T>,Func<T>,Meta<T>,Owned<T>), plus open generics closed over reference types. EvenIEnumerable<valueType>works.