Skip to content
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

Avoid generating internal static classes with constant names in Options Source gen #89148

Merged
merged 1 commit into from
Jul 19, 2023

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Jul 18, 2023

The Option Source Generator generates static classes with constant names such as __OptionValidationStaticInstances.__Attributes and __OptionValidationStaticInstances.__Validators. These classes have an internal 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 the InternalsVisibleToAttribute 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.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 18, 2023
@ghost ghost assigned tarekgh Jul 18, 2023
@tarekgh tarekgh added area-Extensions-Options source-generator Indicates an issue with a source generator feature and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 18, 2023
@ghost
Copy link

ghost commented Jul 18, 2023

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

Issue Details

The Option Source Generator generates static classes with constant names such as __Attributes and __Validators. These classes have an internal 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 the InternalsVisibleToAttribute 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.

Author: tarekgh
Assignees: tarekgh
Labels:

area-Extensions-Options, source-generator

Milestone: -

@tarekgh tarekgh added this to the 8.0.0 milestone Jul 18, 2023
@tarekgh
Copy link
Member Author

tarekgh commented Jul 18, 2023

@tarekgh tarekgh merged commit d051a84 into dotnet:main Jul 19, 2023
@tarekgh tarekgh deleted the FixOptionsSourceGenStaticClassesScope branch July 19, 2023 15:33
@ghost ghost locked as resolved and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Options source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants