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

[Trimming] Use new feature switch definition attribute and enable analyzers in Controls.Core.csproj #21621

Conversation

simonrozsival
Copy link
Member

Description of Change

We can use [FeatureSwitchDefinition("...")] attributes instead of ILLink.Substitutions.xml since dotnet/runtime#99338 has been merged and has flown into MAUI.

/cc @vitek-karas

Issues Fixed

Contributes to #18658

@simonrozsival simonrozsival requested a review from a team as a code owner April 3, 2024 14:23
@vitek-karas
Copy link
Member

@sbomer for both review and as an FYI.

@simonrozsival
Copy link
Member Author

@sbomer I was trying to understand the [FeatureGuard] attribute and if it can be applied to MAUI (e.g., the IsXamlRuntimeParsingSupported feature switch is used to guard codepaths annotated with [RequiresUnreferencedCode]) but I wasn't able to figure out how to use it. Is it something that could/should be used in this context?

Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

LGTM! If you are using the trim analyzer and would like IsXamlRuntimeParsingSupported to guard RequiresUnreferencedCodce-annotated code, you can also add [FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))] to the property.

You'll need to suppress IL4000 (which is emitted when the analyzer can't see that the property will always be false when unreferenced code is removed) on the property, and ensure manually that the feature switch is set to false in a trimmed app.

@@ -20,6 +20,9 @@ internal static class RuntimeFeature
private const bool IsQueryPropertyAttributeSupportedByDefault = true;
private const bool IsImplicitCastOperatorsUsageViaReflectionSupportedByDefault = true;

#if !NETSTANDARD
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported")]
Copy link
Member

Choose a reason for hiding this comment

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

Just wanted to check that it's expected for the feature switch to no longer work in the netstandard build.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's expected. The netstandard build doesn't need trimming, only the platform specific builds do.

@simonrozsival
Copy link
Member Author

@sbomer thanks for the additional details. I tried what you say before and I was confused by the IL4000 and I thought I must be using it incorrectly. I'll give it another go.

@simonrozsival simonrozsival changed the title [Trimming] Use new feature switch definition attribute [Trimming] Use new feature switch definition attribute and enable analyzers in Controls.Core.csproj Apr 8, 2024
@@ -8,6 +9,7 @@
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/VisualElement.xml" path="Type[@FullName='Microsoft.Maui.Controls.VisualElement']/Docs/*" />
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm thinking about putting CSS styling behind a feature flag instead of preserving all public and non-public fields on all VisualElement classes. Would it be acceptable to say that CSS isn't supported with trimming?

On the other hand, when I compared the .ipa size of NativeAOT app build with this [DAM] and without it, the size difference was negligible. I worry that when somebody uses Telerik or some other control library, it might suddenly become more pronounced.

Comment on lines 12 to 13
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = TrimmerConstants.NativeBindingService)]
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmerConstants.NativeBindingService)]
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not very happy we have these UnconditionalSuppressMessage here. I think in this case it might be worth putting [RequiresUnreferencedCode] on the INativeBindingService.TrySetBinding(object, string, BindingBase) and all the classes that implement it.

@jonathanpeppers jonathanpeppers merged commit ac6cc71 into dotnet:net9.0 Apr 17, 2024
46 of 48 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants