Declare AOT compatibility and fix Native AOT crash in enum fallback#378
Merged
Conversation
Mark the library IsAotCompatible and resolve all trim/AOT analyzer warnings: - FieldInfo.MetadataToken is unavailable on Native AOT and crashed the enum reflection fallback with InvalidOperationException. Sort by the underlying constant value instead, which is deterministic on every runtime and matches Enum.GetValues semantics. - Suppress IL2090 in the enum fallback: the trimmer always preserves fields of enum types it keeps (verified with TrimMode=full). - Suppress IL2026/IL2077 for TypeDescriptor.GetConverter: intrinsic converters cover the types typically used with prompts; custom TypeConverter attributes on user types remain the app's responsibility. Verified with PublishTrimmed (TrimMode=full) and PublishAot on win-x64: zero IL warnings, and both the source-generated metadata path and the reflection fallback work at runtime. Fixes #248 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Sharprompt to explicitly declare Native AOT compatibility and addresses a Native AOT runtime crash in the enum reflection fallback path used by Prompt.Select<TEnum>/Prompt.MultiSelect<TEnum> when enum metadata isn’t pre-registered.
Changes:
- Declares the library as AOT-compatible via
<IsAotCompatible>true</IsAotCompatible>. - Adds targeted trimming suppressions around
TypeDescriptor.GetConverterusage inTypeHelper<T>. - Reworks enum fallback metadata creation to avoid
FieldInfo.MetadataToken(unsupported on Native AOT) by sorting based on enum values.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Sharprompt/Sharprompt.csproj | Marks the package as AOT-compatible to enable analyzers and communicate support to consumers. |
| src/Sharprompt/Internal/TypeHelper.cs | Suppresses trimming warnings for TypeDescriptor converter usage with documented justification. |
| src/Sharprompt/EnumMetadataRegistry.cs | Updates enum fallback ordering to avoid Native AOT MetadataToken usage and remain deterministic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sort by ordinal field name before deduplicating aliased values so the representative does not depend on GetFields ordering, and always map values through the representative's field name because Enum.ToString picks an implementation-defined alias. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
Makes Sharprompt trim- and Native AOT-compatible, and fixes a real Native AOT crash found during verification.
<IsAotCompatible>true</IsAotCompatible>on the library — enables the trim/AOT analyzers at build time and marks the NuGet package as AOT-compatible for consumers.FieldInfo.MetadataTokenthrowsInvalidOperationExceptionon Native AOT, crashing the enum reflection fallback (Prompt.Select<TEnum>for unregistered enums). The fallback now sorts by the underlying constant value instead — deterministic on every runtime and matchingEnum.GetValuessemantics.TrimMode=full).TypeDescriptor.GetConverterinTypeHelper<T>: intrinsic converters cover the types typically used with prompts (string, numerics, bool, Guid, DateTime, enums); customTypeConverterattributes on user-defined types remain the application's responsibility to preserve.Fixes #248
Verification
dotnet buildwith AOT analyzersPublishTrimmed+TrimMode=full(win-x64)TypeDescriptorconversions all work at runtimePublishAot(win-x64, ILC 8.0.28)InvalidOperationExceptiononMetadataToken)🤖 Generated with Claude Code