Skip to content

Remove DAM from design-time attributes - #98378

Merged
agocke merged 4 commits into
dotnet:mainfrom
agocke:remove-dam-from-design-time-attrs
Feb 22, 2024
Merged

agocke merged 4 commits into
dotnet:mainfrom
agocke:remove-dam-from-design-time-attrs

Conversation

@agocke

@agocke agocke commented Feb 13, 2024 •

Copy link
Copy Markdown
Member

I noticed these attributes were bringing in a lot of designer code in WinForms due to the DAM annotation. However, I don't think the DAM annotation is appropriate for attributes meant for design time. I would instead expect the code using these attributes to get RUC, or find a different way to preserve the code.

I noticed these attributes were bringing in a lot of designer code in WinForms due to the DAM
annotation. However, I don't think the DAM annotation is appropriate for attributes meant for
design time. I would instead inspect the code using these attributes to get RUC, or find a different
way to preserve the code.
@ghost

ghost commented Feb 13, 2024

Copy link
Copy Markdown

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost

ghost commented Feb 13, 2024

Copy link
Copy Markdown

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

Issue Details

I noticed these attributes were bringing in a lot of designer code in WinForms due to the DAM annotation. However, I don't think the DAM annotation is appropriate for attributes meant for design time. I would instead inspect the code using these attributes to get RUC, or find a different way to preserve the code.

Author: agocke
Assignees: agocke
Labels:

area-System.ComponentModel, new-api-needs-documentation

Milestone: -

@agocke

agocke commented Feb 13, 2024

Copy link
Copy Markdown
Member Author

@eerhardt @LakshanF @vitek-karas @sbomer I would appreciate your thoughts here

When looking at WinForms I noticed that most of the designer stuff was being brought in by these attributes, which are meant for design-time-only. The existing codebase tried to make the retrieval methods trim-compatible by annotating these attributes. But I'm not sure that's right. Aside from the fact that it didn't work, and those retrieval methods are still RUC for other reasons, I don't think making those methods safe is the right answer.

If the intention of design-time is to only run during build, not during publish, I would actually expect this stuff to produce warnings if it were ever being kept in a production app (i.e., an app where trimming is enabled). If the user wanted to preserve this stuff for production, I would expect they would use some other attribute or code to do so.

This could technically be considered a breaking change, but since all the main usage points are RUC, I don't think it's meaningful.

@agocke
agocke requested review from LakshanF and eerhardt February 13, 2024 19:00

@eerhardt eerhardt left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably update the RUC message on

[RequiresUnreferencedCode("The Type of component cannot be statically discovered.")]
public static IDesigner? CreateDesigner(IComponent component, Type designerBaseType)
{
Type? type = null;
IDesigner? result = null;
AttributeCollection attributes = GetAttributes(component);
for (int i = 0; i < attributes.Count; i++)
{
if (attributes[i] is DesignerAttribute designerAttribute)
{
Type? type2 = Type.GetType(designerAttribute.DesignerBaseTypeName);

Since this is now another reason (probably a main reason) why that method is incompatible.

The same goes for the usages of EditorAttribute:

[RequiresUnreferencedCode(TypeDescriptor.EditorRequiresUnreferencedCode + " " + PropertyDescriptorPropertyTypeMessage)]
public virtual object? GetEditor(Type editorBaseType)
{
object? editor = null;
// Always grab the attribute collection first here, because if the metadata version
// changes it will invalidate our editor cache.
AttributeCollection attrs = Attributes;
// Check the editors we've already created for this type.
if (_editorTypes != null)
{
for (int i = 0; i < _editorCount; i++)
{
if (_editorTypes[i] == editorBaseType)
{
return _editors![i];
}
}
}
// If one wasn't found, then we must go through the attributes.
if (editor == null)
{
for (int i = 0; i < attrs.Count; i++)
{
if (!(attrs[i] is EditorAttribute attr))
{
continue;
}
Type? editorType = GetTypeFromName(attr.EditorBaseTypeName);

/// <summary>
/// Gets an editor with the specified base type for the specified type.
/// </summary>
[RequiresUnreferencedCode(EditorRequiresUnreferencedCode)]
public static object? GetEditor(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type,
Type editorBaseType)
{
ArgumentNullException.ThrowIfNull(editorBaseType);
return GetDescriptor(type, nameof(type)).GetEditor(editorBaseType);

@eerhardt

Copy link
Copy Markdown
Member

We had a bit of discussion on this in #49467 (comment).

cc @RussKie @vitek-karas

@vitek-karas

Copy link
Copy Markdown
Member

Just a note on making the app smaller - another consideration might be to completly remove these attributes during trimming. That would in itself "fix" the problem with the DAM, but we could/should change that as well I think.

@agocke

agocke commented Feb 13, 2024

Copy link
Copy Markdown
Member Author

Just a note on making the app smaller - another consideration might be to completly remove these attributes during trimming

Yeah, that's another approach, but I thought this one was actually simpler. The current design implies that "EditorAttribute" and "DesignerAttribute" intend to preserve stuff in your app (due to the DAM annotation) -- I think that's backwards. I would expect most people would think the target code should be trimmed away

@agocke

agocke commented Feb 13, 2024

Copy link
Copy Markdown
Member Author

Also @JeremyKuhne

@RussKie

RussKie commented Feb 13, 2024 •

Copy link
Copy Markdown
Contributor

A dam question, what's DAM (pun intended)? :)

A lot of designer attributes may required for the PropertyGrid control to function at the runtime...

@agocke

agocke commented Feb 13, 2024 •

Copy link
Copy Markdown
Member Author

RussKie on Mar 16, 2021
These attributes can be and are used at runtime by PropertyGrid control, and can be used by custom designers (e.g. a report viewer)

This is unfortunate. However, I don't think it changes the calculus that much.

From what I can see, WinForms trimming is not viable if the designer stack is brought in. It's all fundamentally unsafe. That implies that DesignerAttribute and EditorAttribute alone can't be sufficient to root code.

So it still seems to follow that we should remove DAM from these attributes. The only question is if PropertyGrid can somehow be made safe in another way, or if we will just have to annotate PropertyGrid as RUC.

Edit: Looking at the traces... maybe we could keep EditorAttribute. DesignerAttribute is the real killer here. If we get rid of that, it might be good enough.

@RussKie

RussKie commented Feb 13, 2024 •

Copy link
Copy Markdown
Contributor

I moved out from the Windows Forms team, @KlausLoeffelmann may be able to provide deeper insights.

@JeremyKuhne

Copy link
Copy Markdown
Member

@agocke I believe that EditorAttribute is the only one we really need to make work (for PropertyGrid).

@KlausLoeffelmann

KlausLoeffelmann commented Feb 13, 2024 •

Copy link
Copy Markdown
Member

A lot of designer attributes may required for the PropertyGrid control to function at the runtime...

Yes, but I would consider this to be a niche case.
We have been thinking about a Project feature flag, which would indicate that an App opt out of Designer functionality (like using Editors or even Custom Type Descriptors in the PropertyGrid scenario).

The "only" thing to solve for your average WinForms LOB App is reading resource files and DataBinding. I am currently working on the side on a Prototype to make DataBinding type safe: As long as we have customers wire-up the binding definition for their Forms/UserControls at design-time, we know both the data source/property source and the data destination/property destination. And since we generate that Binding class code for InitializeComponent anyway (since 17.6 via Roslyn, btw!), we should as well be able to generate that type information in a way, that a type-safe Binding<Tsource, Tdestination> would principally work the same way as the existing, just not by pushing and pulling the data via reflection.

I haven't looked into the resource topic, yet, though, but there should be a similar approach possible.

@agocke

agocke commented Feb 13, 2024

Copy link
Copy Markdown
Member Author

We have been thinking about a Project feature flag, which would indicate that an App opt out of Designer functionality (like using Editors or even Custom Type Descriptors in the PropertyGrid scenario).

Yeah, that's another alternative to making PropertyGrid RUC -- use a feature flag to remove the Editor-specific functionality. As long as that's an acceptable user experience, I think that's a good path forward.

Broadly, I think the question in this PR is: do we expect every Editor or Designer target to be trim-compatible? If not, we run into a real problem where the attribute forces the type to be kept, and then the type produces unsolvable warnings because it wasn't designed to be trim-compatible.

By removing the annotation on the above types, we basically say, "this attribute is not supported when trimming." It doesn't necessarily mean that the types can't be preserved or used in a different way, it just means that the given attributes won't be enough to keep the targets around.

@KlausLoeffelmann

Copy link
Copy Markdown
Member

By removing the annotation on the above types, we basically say, "this attribute is not supported when trimming." It doesn't necessarily mean that the types can't be preserved or used in a different way, it just means that the given attributes won't be enough to keep the targets around.

Makes perfect sense to me!

@MichalStrehovsky

Copy link
Copy Markdown
Member

This could technically be considered a breaking change, but since all the main usage points are RUC, I don't think it's meaningful

It will likely break https://github.com/meziantou/Meziantou.Framework/blob/ba361873c94491e459c81862b4b9b6abee8189ac/src/Meziantou.AspNetCore.Components/GenericFormField.cs#L212 (the repo does have references to IsTrimmable as well but not sure if this is already trimmable).

The upswing from doing this is probably worth the break. We just have to make a breaking change notice and explain the upswing.

Alternatively we could strip the attribute under a feature switch. This way people can still opt into the old semantic. #92043 has the diff to apply because I already experimented with it in the past. But I hate suggesting this because I detest feature switches and I hate attribute stripping as well because they require careful emulation during F5 debug time that breaks down for unit testing... and attribute stripping cannot even be emulated.

@agocke

agocke commented Feb 14, 2024

Copy link
Copy Markdown
Member Author

Thanks for the pointer, I agree it will break that use case. And I do agree that a breaking change doc is merited.

However, overall, I think it's worth the change. The semantics "design time attributes are only meant for design time" seems like the right intent.

The argument that would persuade me the other way is that EditorAttribute is not meant to be a design-time attribute. That may be true. All the docs seem to suggest that it's meant to be for design-time, but I don't see the situation as clear cut as it is for DesignerAttribute

@KlausLoeffelmann

Copy link
Copy Markdown
Member

The argument that would persuade me the other way is that EditorAttribute is not meant to be a design-time attribute.

Especially for WinForms App, the definition of something being design-time or run-time is a bit fuzzy. After all, the WinForms Designer itself is actually running at the user's design time.

What I mean is: Folks are using the WinForms Property Browser Control for an easy way to collect for example a large amount of settings data. I don't think that that is the most frequent usage, but it certainly happens. Those cases would be qualifying as using Design-time functionality at runtime, especially, when they are authoring custom type editors which are called for a certain type through the property browser at (then) runtime.

I think, there are workarounds around this, which could be trimmable, but the worst case would be, that those kinds of apps would not be able to use a feature flag (at least in the first iteration) and would be excluded from trimming.

@agocke

agocke commented Feb 15, 2024

Copy link
Copy Markdown
Member Author

Yup, to be clear I expect there will be many people who are taking advantage of designer functionality at runtime for a variety of different reasons. However, the scope of making everything built for design-time be compatible with trimming is... challenging, if it's even possible. I think those apps are fine as is and should continue working -- they just won't be compatible with trimming without modification.

@sbomer

sbomer commented Feb 16, 2024 •

Copy link
Copy Markdown
Member

and attribute stripping cannot even be emulated.

ILLink will at least warn on attempts to use the attribute type. Would you consider it good enough if we had ILC/analyzer support for this? @MichalStrehovsky

I am in favor of the approach in this PR given the current limitations of attribute stripping. I think both approaches will require a feature switch to remove the code that reflects over the attributes.

@MichalStrehovsky

Copy link
Copy Markdown
Member

ILLink will at least warn on attempts to use the attribute type. Would you consider it good enough if we had ILC/analyzer support for this? @MichalStrehovsky

If the analyzer could warn about this, it would alleviate some of my concerns. I don't know if it's possible with the current attribute stripping design. And we'd still have a hole in reflection APIs that don't take the attribute type as a parameter (they just list all attributes). We'd need to mark those as trim-unsafe and that might be a hard to swallow pill.

Maybe better to open a separate issue for those conversations. I don't know if there's a way to make these experiences good. It might be that this is a mechanism we need to use when retrofitting trimming into things designed before trimming because nothing else is possible. But we'd advise against using these mechanism in any new code and not spend time designing experiences around it.

@agocke

agocke commented Feb 21, 2024

Copy link
Copy Markdown
Member Author

@eerhardt Anything else?

@eerhardt eerhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@agocke
agocke merged commit 2d90448 into dotnet:main Feb 22, 2024
@agocke
agocke deleted the remove-dam-from-design-time-attrs branch February 22, 2024 06:32
@MichalStrehovsky MichalStrehovsky added the breaking-change Issue or PR that represents a breaking API or functional change over a previous release. label Feb 22, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.ComponentModel breaking-change Issue or PR that represents a breaking API or functional change over a previous release. new-api-needs-documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants