Skip to content

Conversation

@simonrozsival
Copy link
Member

@simonrozsival simonrozsival commented Mar 6, 2024

Description of Change

ILLink Trim analyzer is reporting the following warnings:

/.../MyMauiApp/obj/Release/net9.0-maccatalyst/maccatalyst-arm64/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator/Resources_Styles_Colors.xaml.sg.cs(30,4): warning IL2026: Using member 'Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml<TXaml>(TXaml, Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Loading XAML at runtime might require types and members that cannot be statically analyzed. Make sure all of the required types and members are preserved. [/.../MyMauiApp/MyMauiApp.csproj::TargetFramework=net9.0-maccatalyst]

It is safe to suppress the warnings in the InitializeComponent method because its method body will be replaced with XamlC in Release builds.

Issues Fixed

Contributes to #19397

/cc @jonathanpeppers @vitek-karas @StephaneDelcroix

@simonrozsival simonrozsival requested a review from a team as a code owner March 6, 2024 14:08
@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Mar 6, 2024
@simonrozsival
Copy link
Member Author

This is probably not the right fix. XamlC runs before ILLink so by the time ILLink processes the assembly, the InitializeComponent body should already be replaced with the compiled IL. I'm closing the PR until I figure out what exactly is going on.

@simonrozsival
Copy link
Member Author

I assumed that the warnings are produced by ILLink, but they are in fact produced by Csc (or by an analyzer during C# compilation). This PR is needed after all.

@simonrozsival simonrozsival reopened this Mar 6, 2024
jonathanpeppers
jonathanpeppers previously approved these changes Mar 6, 2024

sb.AppendLine("\t\tprivate void InitializeComponent()");
sb.AppendLine("\t\t{");
sb.AppendLine("#pragma warning disable IL2026 // The body of InitializeComponent will be replaced by XamlC so LoadFromXaml will never be called in production builds");
Copy link
Member

Choose a reason for hiding this comment

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

I think source suppression would probably work:

  • dotnet new maui-lib
  • Have one view or page with XAML
  • dotnet build -c Release -> would run XamlC
  • Any consuming app would have no warnings?

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, XamlC will replace the body of InitializeComponent in that case.

@jonathanpeppers jonathanpeppers merged commit 28eebcb into dotnet:net9.0 Mar 11, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors fixed-in-9.0.0-preview.3.10457

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants