-
Notifications
You must be signed in to change notification settings - Fork 773
Fixes #5069. ConfigurationManager is trim-safe without TrimmerRootAssembly #5071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tig
merged 6 commits into
gui-cs:develop
from
harder:fix/5069-config-manager-aot-trim-safe
Apr 26, 2026
+241
−15
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b67d8e6
Fixes #5069. ConfigurationManager is trim-safe without TrimmerRootAss…
harder 6bd58e3
Merge branch 'develop' into fix/5069-config-manager-aot-trim-safe
tig ff0b38c
Addressed PR feedback in XML comment and test
harder c7a6d40
Merge branch 'develop' into fix/5069-config-manager-aot-trim-safe
harder af3a0f4
Addressed PR review feedback and fixed tests to match project convent…
harder 353aa30
Merge branch 'develop' into fix/5069-config-manager-aot-trim-safe
tig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Terminal.Gui.App; | ||
| using Terminal.Gui.Drawing; | ||
| using Terminal.Gui.Drivers; | ||
| using Terminal.Gui.Input; | ||
| using Terminal.Gui.Text; | ||
| using Terminal.Gui.Tracing; | ||
| using Terminal.Gui.ViewBase; | ||
| using Terminal.Gui.Views; | ||
|
|
||
| namespace Terminal.Gui.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// INTERNAL: The statically-known set of types that own | ||
| /// <see cref="ConfigurationPropertyAttribute"/>-decorated properties. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <see cref="ConfigurationManager"/> used to discover these types by reflecting over every loaded | ||
| /// assembly at module-init time. That scan was not trim-safe: with <c>PublishTrimmed=true</c>, types | ||
| /// not otherwise referenced by the consuming application were stripped and | ||
| /// <c>Scope<T>.GetUninitializedProperty</c> would throw at startup. | ||
| /// See <see href="https://github.com/gui-cs/Terminal.Gui/issues/5069"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// The per-type <see cref="DynamicDependencyAttribute"/> entries on <see cref="GetTypes"/> root | ||
| /// each host and preserve its <see cref="DynamicallyAccessedMemberTypes.PublicProperties"/> so the | ||
| /// trimmer does not remove the properties the scan depends on. | ||
| /// </para> | ||
| /// <para> | ||
| /// A unit test verifies this list stays in sync with the types that actually carry | ||
| /// <see cref="ConfigurationPropertyAttribute"/> so drift is caught at build time, not in AOT | ||
| /// consumer apps. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal static class ConfigPropertyHostTypes | ||
| { | ||
| private const DynamicallyAccessedMemberTypes PreservedMembers = DynamicallyAccessedMemberTypes.PublicProperties; | ||
|
|
||
| private static readonly Type [] _types = | ||
| [ | ||
| typeof (Application), | ||
| typeof (ConfigurationManager), | ||
| typeof (SchemeManager), | ||
| typeof (ThemeManager), | ||
| typeof (Color), | ||
| typeof (Glyphs), | ||
| typeof (Driver), | ||
| typeof (Key), | ||
| typeof (NerdFonts), | ||
| typeof (Trace), | ||
| typeof (View), | ||
| typeof (Button), | ||
| typeof (CharMap), | ||
| typeof (CheckBox), | ||
| typeof (Dialog), | ||
| typeof (FileDialog), | ||
| typeof (FileDialogStyle), | ||
| typeof (FrameView), | ||
| typeof (HexView), | ||
| typeof (LinearRange), | ||
| typeof (Menu), | ||
| typeof (MenuBar), | ||
| typeof (MessageBox), | ||
| typeof (PopoverMenu), | ||
| typeof (SelectorBase), | ||
| typeof (StatusBar), | ||
| typeof (TextField), | ||
| typeof (TextView), | ||
| typeof (Window) | ||
| ]; | ||
|
|
||
| [DynamicDependency (PreservedMembers, typeof (Application))] | ||
| [DynamicDependency (PreservedMembers, typeof (ConfigurationManager))] | ||
| [DynamicDependency (PreservedMembers, typeof (SchemeManager))] | ||
| [DynamicDependency (PreservedMembers, typeof (ThemeManager))] | ||
| [DynamicDependency (PreservedMembers, typeof (Color))] | ||
| [DynamicDependency (PreservedMembers, typeof (Glyphs))] | ||
| [DynamicDependency (PreservedMembers, typeof (Driver))] | ||
| [DynamicDependency (PreservedMembers, typeof (Key))] | ||
| [DynamicDependency (PreservedMembers, typeof (NerdFonts))] | ||
| [DynamicDependency (PreservedMembers, typeof (Trace))] | ||
| [DynamicDependency (PreservedMembers, typeof (View))] | ||
| [DynamicDependency (PreservedMembers, typeof (Button))] | ||
| [DynamicDependency (PreservedMembers, typeof (CharMap))] | ||
| [DynamicDependency (PreservedMembers, typeof (CheckBox))] | ||
| [DynamicDependency (PreservedMembers, typeof (Dialog))] | ||
| [DynamicDependency (PreservedMembers, typeof (FileDialog))] | ||
| [DynamicDependency (PreservedMembers, typeof (FileDialogStyle))] | ||
| [DynamicDependency (PreservedMembers, typeof (FrameView))] | ||
| [DynamicDependency (PreservedMembers, typeof (HexView))] | ||
| [DynamicDependency (PreservedMembers, typeof (LinearRange))] | ||
| [DynamicDependency (PreservedMembers, typeof (Menu))] | ||
| [DynamicDependency (PreservedMembers, typeof (MenuBar))] | ||
| [DynamicDependency (PreservedMembers, typeof (MessageBox))] | ||
| [DynamicDependency (PreservedMembers, typeof (PopoverMenu))] | ||
| [DynamicDependency (PreservedMembers, typeof (SelectorBase))] | ||
| [DynamicDependency (PreservedMembers, typeof (StatusBar))] | ||
| [DynamicDependency (PreservedMembers, typeof (TextField))] | ||
| [DynamicDependency (PreservedMembers, typeof (TextView))] | ||
| [DynamicDependency (PreservedMembers, typeof (Window))] | ||
| internal static Type [] GetTypes () => _types; | ||
| } |
87 changes: 87 additions & 0 deletions
87
Tests/UnitTestsParallelizable/Configuration/ConfigPropertyHostTypesTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Claude - Opus 4.7 | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using Terminal.Gui.Configuration; | ||
|
|
||
| namespace ConfigurationTests; | ||
|
|
||
| public class ConfigPropertyHostTypesTests | ||
| { | ||
| /// <summary> | ||
| /// Guard-rail test: the hard-coded list in <see cref="ConfigPropertyHostTypes"/> must exhaustively cover every type in | ||
| /// the Terminal.Gui assembly that declares a <see cref="ConfigurationPropertyAttribute"/> property. Drift between | ||
| /// the list and the attribute usage would silently reintroduce the trim/AOT failure fixed by | ||
| /// <see href="https://github.com/gui-cs/Terminal.Gui/issues/5069"/>. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConfigPropertyHostTypes_GetTypes_Matches_Reflected_Hosts_In_TerminalGui_Assembly () | ||
| { | ||
| // Arrange | ||
| Assembly terminalGuiAssembly = typeof (ConfigurationManager).Assembly; | ||
|
|
||
| HashSet<Type> reflected = terminalGuiAssembly | ||
| .GetTypes () | ||
| .Where (HasConfigurationProperty) | ||
| .ToHashSet (); | ||
|
|
||
| // Act | ||
| HashSet<Type> registered = ConfigPropertyHostTypes.GetTypes ().ToHashSet (); | ||
|
|
||
| // Assert | ||
| Type [] missing = reflected.Except (registered).ToArray (); | ||
| Type [] extra = registered.Except (reflected).ToArray (); | ||
|
|
||
| Assert.True ( | ||
| missing.Length == 0 && extra.Length == 0, | ||
| $"ConfigPropertyHostTypes drift detected.\nMissing (add to list): {string.Join (", ", missing.Select (t => t.FullName))}\nExtra (remove from list): {string.Join (", ", extra.Select (t => t.FullName))}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Guard-rail test: the <see cref="DynamicDependencyAttribute"/> set on <c>GetTypes</c> must exactly match the | ||
| /// <c>_types</c> array it returns. The attributes are what the trimmer actually reads to preserve members; if | ||
| /// someone adds a type to the array but forgets the matching attribute (or vice-versa), AOT builds would silently | ||
| /// lose rooting again without the reflected-hosts test above catching it. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConfigPropertyHostTypes_DynamicDependencies_Match_Returned_Types () | ||
| { | ||
| // Arrange | ||
| MethodInfo getTypes = typeof (ConfigPropertyHostTypes).GetMethod ( | ||
| nameof (ConfigPropertyHostTypes.GetTypes), | ||
| BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)!; | ||
|
|
||
| HashSet<Type> rooted = getTypes | ||
| .GetCustomAttributes<DynamicDependencyAttribute> () | ||
| .Select (a => a.Type!) | ||
| .ToHashSet (); | ||
|
|
||
| HashSet<Type> returned = ConfigPropertyHostTypes.GetTypes ().ToHashSet (); | ||
|
|
||
| // Assert | ||
| Type [] missingRoots = returned.Except (rooted).ToArray (); | ||
| Type [] extraRoots = rooted.Except (returned).ToArray (); | ||
|
|
||
| Assert.True ( | ||
| missingRoots.Length == 0 && extraRoots.Length == 0, | ||
| $"ConfigPropertyHostTypes [DynamicDependency] drift detected.\nTypes in array but not rooted by attribute: {string.Join (", ", missingRoots.Select (t => t.FullName))}\nTypes rooted by attribute but not in array: {string.Join (", ", extraRoots.Select (t => t.FullName))}"); | ||
| } | ||
|
|
||
| private static bool HasConfigurationProperty (Type type) | ||
| { | ||
| // Mirror the production scan in ConfigProperty.Initialize (), which calls type.GetProperties () — i.e., | ||
| // public properties only (includes inherited public members; excludes non-public and, by default, statics | ||
| // not declared on `type` itself). Widening the binding flags here would flag types the production scan | ||
| // could never discover and the trimmer wouldn't preserve via PublicProperties. | ||
| PropertyInfo [] properties = type.GetProperties (); | ||
|
|
||
| foreach (PropertyInfo property in properties) | ||
| { | ||
| if (property.GetCustomAttribute<ConfigurationPropertyAttribute> () is { }) | ||
|
harder marked this conversation as resolved.
|
||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.