-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Unexpected trimming on a type on which DynamicallyAccessedMembersAttribute is applied (.NET 6 Preview 6) #57140
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
@TheCakeMonster - check out the description in dotnet/linker#1929, which describes how the
Annotating a type with Closing, as this is the intended behavior. |
I want to add some more explanation of what is the expected behavior of trimming overall. There are basically only two cases:
Caveat: Suppressing any of the trimmer warnings can easily leave the app in a state where it doesn't report warnings, but it's still broken. Please be VERY careful when suppressing trimmer warnings. The repro app you provided should report several warnings, so it falls into the first bucket. As for what is removed, if the app falls into the second bucket (no warnings) - the trimmer does not make any guarantee that it will remove or keep anything in the app - all it guarantees is that the behavior of the app is not going to be changed. Obviously it tries to remove as much as possible (to get the size down), but the exact behavior of the trimming is subject to change at any point. In the case of the |
OK, thanks everyone. Is there another attribute for disabling trimming on a per-type basis that I am missing? If I understand the responses correctly, there are quite a few scenarios making any support for trimming unviable at the moment. If the names of types are not known until runtime - for example they come from a config file, a database or a serialization stream - then enabling trimming on that assembly is not possible? I'm wondering how pluggable architectures are covered in respect of trimming. There are a number of techniques that spring to mind that seem not to be covered by the existing capabilities. |
Correct. There's no conceivable way to determine what is used by the application, so there's no way to trim what is unused. |
@agocke thanks, that's what I thought. I wonder if it's time for a new attribute, perhaps called PleaseDontTrimMyTypeMrTrimmerIKnowWhatIAmDoingAndItsInappropriateAttribute? Hehehe |
That sounds like the |
Short Description
I expected a type on which the DynamicallyAccessedMembersAttribute is applied (with the enum value DynamicallyAccessedMemberTypes.All) to be excluded from trimming, and have all of its members preserved. However, this is not the case; instead private members are being removed. Repro created for inspection.
https://github.com/TheCakeMonster/Examples/tree/master/Issues/UnexpectedTrimming
Description
I have created two test libraries - one is a .NET Standard library, the other a .NET 6 library.
Each library contains a type that has the DynamicallyAccessedMembersAttribute applied using the DynamicallyAccessedMemberTypes.All enum value, indicating that all members are subject to dynamic access. However, the private instance method on each type is removed when the application is published. This is unexpected.
I have created a repro at https://github.com/TheCakeMonster/Examples/tree/master/Issues/UnexpectedTrimming
When the console app in this solution is run in debug mode, all of the console outputs are true, indicating that the private methods and the types they access are present. This is exactly what I expect (as trimming is not run) but is important in that it proves that the reflection code works.
When the console application is published and the published application is run, the flags all return false, instead of the second set - the ones associated with the class to which the attribute has been applied - continuing to return true.
Configuration
.NET 6 Preview 6
Dotnet --version output is as follows:
.NET SDK (reflecting any global.json):
Version: 6.0.100-preview.6.21355.2
Commit: 7f8e0d76c0
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\
Host (useful for support):
Version: 6.0.0-preview.6.21352.12
Commit: 770d630
Publishing is being performed using Visual Studio 2019 16.11 Preview 2, if that is relevant.
Regression?
This code makes use of a change to DynamicallyAccessedMembersAttribute that is only available in .NET 6, so it would not compile on .NET 5. This is new functionality in .NET 6, although it makes use of the trimming feature made more widely available in .NET 5.
The code makes use of the change made under issue #49465, which allows the DynamicallyAccessedMembersAttribute attribute to be applied to a class for the first time.
The text was updated successfully, but these errors were encountered: