Avoid generating internal static classes with constant names in Options Source gen #89148
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.
The Option Source Generator generates static classes with constant names such as
__OptionValidationStaticInstances.__Attributes
and__OptionValidationStaticInstances.__Validators
. These classes have aninternal
access modifier, which works well when compiling independent projects. However, issues arise when compiling two libraries that both use the Option Source Generator, and one of the libraries is marked with theInternalsVisibleToAttribute
to grant access to the internals of the first library.In this scenario, a compilation error occurs because the generated static classes exist in two places and are accessible to one of these libraries. This leads to ambiguous references and compiler errors.
To resolve this issue, a fix has been implemented. When compiling with Language version 11 or higher, the
file
access modifier is used, limiting access to only the file where this class is defined. For earlier language versions, a random suffix is generated and added to the class names.