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

Reduce the trim requirements on MetadataUpdateHandlerAttribute #66069

Closed
vitek-karas opened this issue Mar 2, 2022 · 6 comments · Fixed by #84369
Closed

Reduce the trim requirements on MetadataUpdateHandlerAttribute #66069

vitek-karas opened this issue Mar 2, 2022 · 6 comments · Fixed by #84369
Labels
area-System.Reflection help wanted [up-for-grabs] Good issue for external contributors linkable-framework Issues associated with delivering a linker friendly framework
Milestone

Comments

@vitek-karas
Copy link
Member

Currently the type passed to the .ctor of MetadataUpdateHandlerAttribute will be annotated with DynamicallyAccessedMemberTypes.All. For apps this doesn't matter because the attribute is marked for removal in trimmed apps by default, so the annotation is not applied.

But this does matter for analysis of libraries for trimming. The All annotation is particularly problematic because it will force include all nested types and may cause additional warnings produced by the analysis as it will treat all members on the type as potentially accessed via reflection (so any member annotated for trimming in any way will produce a warning).

My reading of the usage of MetadataUpdateHandlerAttribute shows that we only need methods on the target type (in fact we only need two specific static methods, but there's no way to annotate it that way right now).

Can we improve the annotation on the MetadataUpdateHandlerAttribute to only ask for methods: DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods?

@ghost
Copy link

ghost commented Mar 2, 2022

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

Issue Details

Currently the type passed to the .ctor of MetadataUpdateHandlerAttribute will be annotated with DynamicallyAccessedMemberTypes.All. For apps this doesn't matter because the attribute is marked for removal in trimmed apps by default, so the annotation is not applied.

But this does matter for analysis of libraries for trimming. The All annotation is particularly problematic because it will force include all nested types and may cause additional warnings produced by the analysis as it will treat all members on the type as potentially accessed via reflection (so any member annotated for trimming in any way will produce a warning).

My reading of the usage of MetadataUpdateHandlerAttribute shows that we only need methods on the target type (in fact we only need two specific static methods, but there's no way to annotate it that way right now).

Can we improve the annotation on the MetadataUpdateHandlerAttribute to only ask for methods: DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods?

Author: vitek-karas
Assignees: -
Labels:

area-System.Reflection

Milestone: -

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 2, 2022
@stephentoub
Copy link
Member

For my edification, can you share an example from one of our handlers where this is causing an issue?

@vitek-karas
Copy link
Member Author

I filed the issue because of this: dotnet/aspnetcore#40430 (comment)

The reduction of the annotation would not fix all of the problems (it would still require all methods and some of those warnings are about methods), but it would help.

We're solving the problem in ASP.NET by creating a subclass which will act solely as the update handler, the attribute points to it and the static methods then do what is necessary (either directly or by forwarding the call to the parent class). Actually, this feels like the best approach right now for the update handlers in general.

I'm wondering if we could also use static abstract methods on interfaces for this purpose - that would allow precise behavior. Personally I don't like these magical behaviors of "and something will call a method with name Update" - the caller has to do reflection and has to be relatively careful (get the right overload if there's more than one, and so on). Declaring the dependency via an interface just feels much cleaner.

@vitek-karas
Copy link
Member Author

The All annotation being applied to a "random" class is also problematic because of compiler generated code. Technically the annotation allows one to use reflection to access anything on the class, including closure classes. Thus linker has to "mark" the closures and everything on them as well. If there's anything annotated anywhere on the closure, it will produce a warning which will be really difficult to explain since it may not directly map to user code. And even if linker could map the closure back to the method it originated from, it's still not clear if it can ignore the potential reflection access (in all other cases we're trying to treat the closures as "not there" and only consider them for the method they come from, but private reflection sees everything).

@steveharter steveharter added design-discussion Ongoing discussion about design without consensus and removed untriaged New issue has not been triaged by the area owner labels Mar 9, 2022
@buyaa-n buyaa-n added this to the Future milestone Jul 21, 2022
@steveharter steveharter added linkable-framework Issues associated with delivering a linker friendly framework and removed design-discussion Ongoing discussion about design without consensus labels Nov 4, 2022
@ghost
Copy link

ghost commented Nov 4, 2022

Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @sbomer, @joperezr
See info in area-owners.md if you want to be subscribed.

Issue Details

Currently the type passed to the .ctor of MetadataUpdateHandlerAttribute will be annotated with DynamicallyAccessedMemberTypes.All. For apps this doesn't matter because the attribute is marked for removal in trimmed apps by default, so the annotation is not applied.

But this does matter for analysis of libraries for trimming. The All annotation is particularly problematic because it will force include all nested types and may cause additional warnings produced by the analysis as it will treat all members on the type as potentially accessed via reflection (so any member annotated for trimming in any way will produce a warning).

My reading of the usage of MetadataUpdateHandlerAttribute shows that we only need methods on the target type (in fact we only need two specific static methods, but there's no way to annotate it that way right now).

Can we improve the annotation on the MetadataUpdateHandlerAttribute to only ask for methods: DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods?

Author: vitek-karas
Assignees: -
Labels:

area-System.Reflection, linkable-framework

Milestone: Future

@steveharter steveharter added the help wanted [up-for-grabs] Good issue for external contributors label Nov 4, 2022
@steveharter
Copy link
Member

Moving to 8.0; we want to be more linker-friendly in 8.0.

@steveharter steveharter modified the milestones: Future, 8.0.0 Nov 7, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 5, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 19, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection help wanted [up-for-grabs] Good issue for external contributors linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants