|
| 1 | +# Fix AOT and Trim Warnings |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Package produces IL2104/IL3053 trim and AOT analysis warnings when used in applications published with `PublishAot=true`. The package needs to be made fully AOT-compatible. |
| 6 | + |
| 7 | +**GitHub Issue:** #7 |
| 8 | + |
| 9 | +## Todo List |
| 10 | + |
| 11 | +- [x] Identify reflection usage causing trim warnings |
| 12 | +- [x] Add `[DynamicallyAccessedMembers]` attributes where needed |
| 13 | +- [x] Consider source generators for reflection-based binding |
| 14 | +- [x] Add `<IsAotCompatible>true</IsAotCompatible>` to project |
| 15 | +- [x] Add `<IsTrimmable>true</IsTrimmable>` to project |
| 16 | +- [x] Test with AOT publish to verify zero warnings |
| 17 | +- [ ] Update package version |
| 18 | + |
| 19 | +## Notes |
| 20 | + |
| 21 | +**Current warnings:** |
| 22 | +``` |
| 23 | +TimeWarp.OptionsValidation.dll : warning IL2104: Assembly 'TimeWarp.OptionsValidation' produced trim warnings. |
| 24 | +TimeWarp.OptionsValidation.dll : warning IL3053: Assembly 'TimeWarp.OptionsValidation' produced AOT analysis warnings. |
| 25 | +``` |
| 26 | + |
| 27 | +**Reproduction steps:** |
| 28 | +1. Create a .NET 10 application using `TimeWarp.OptionsValidation` |
| 29 | +2. Use `AddFluentValidatedOptions<TOptions, TValidator>(config)` |
| 30 | +3. Publish with AOT: `dotnet publish -p:PublishAot=true` |
| 31 | + |
| 32 | +**Package Version:** 1.0.0-beta.3 |
| 33 | + |
| 34 | +**Related:** TimeWarp.Nuru task 018-make-configuration-validation-sample-aot-compatible |
| 35 | + |
| 36 | +## Results |
| 37 | + |
| 38 | +### Changes Made: |
| 39 | + |
| 40 | +**1. `service-collection-extensions.cs`** |
| 41 | +- Added `using System.Diagnostics.CodeAnalysis;` |
| 42 | +- Added `[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]` to `TOptions` on the configuration binding overload (required by `Bind<TOptions>()`) |
| 43 | +- Added `[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]` to `TValidator` on both overloads |
| 44 | + |
| 45 | +**2. `options-builder-extensions.cs`** |
| 46 | +- Added `using System.Diagnostics.CodeAnalysis;` |
| 47 | +- Added `[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]` to `TValidator` (required by `TryAddSingleton<TValidator>()`) |
| 48 | + |
| 49 | +**3. `timewarp-options-validation.csproj`** |
| 50 | +- Added `<IsAotCompatible>true</IsAotCompatible>` |
| 51 | +- Added `<IsTrimmable>true</IsTrimmable>` |
| 52 | + |
| 53 | +**4. New AOT test project** (`tests/aot-test/`) |
| 54 | +- Created test project that publishes with AOT to verify no trim/AOT warnings |
| 55 | + |
| 56 | +### Test Results: |
| 57 | + |
| 58 | +| Test | Result | |
| 59 | +|------|--------| |
| 60 | +| `dotnet build -warnaserror` | ✅ 0 warnings, 0 errors | |
| 61 | +| `dotnet test` | ✅ Passed: 4, Skipped: 1 | |
| 62 | +| `dotnet publish -c Release` (AOT) | ✅ 0 IL2104/IL3053 warnings | |
| 63 | +| Run AOT binary | ✅ Output: "Name: Test, Value: 42" | |
| 64 | + |
| 65 | +### Decision: |
| 66 | +Source generators were not needed - the `[DynamicallyAccessedMembers]` attributes were sufficient to eliminate all warnings while maintaining full AOT compatibility. |
0 commit comments