AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.06k stars 2.17k forks source link

IL2026 & 2104 Trim Warnings #13897

Closed styris-ame closed 2 months ago

styris-ame commented 8 months ago

Describe the bug

Trimming warnings on fresh Avalonia MVVM template.

List of warnings:

Code Description
IL2026 Avalonia.Data.Converters.MethodToCommandConverter.CanExecuteChanged: Using member 'Avalonia.Data.Converters.MethodToCommandConverter.CanExecuteChanged.remove' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. BindingExpression and ReflectionBinding heavily use reflection. Consider using CompiledBindings instead.
IL2026 Avalonia.Data.Converters.MethodToCommandConverter.CanExecuteChanged: Using member 'Avalonia.Data.Converters.MethodToCommandConverter.CanExecuteChanged.add' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. BindingExpression and ReflectionBinding heavily use reflection. Consider using CompiledBindings instead.
IL2026 Avalonia.Data.Core.Plugins.ObservableStreamPlugin.ObservableStreamPlugin(): Using member 'Avalonia.Data.Core.Plugins.ObservableStreamPlugin.GetBoxObservable()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. StreamPlugin might require unreferenced code.
IL2026 Avalonia.Data.Core.Plugins.ObservableStreamPlugin.ObservableStreamPlugin(): Using member 'Avalonia.Data.Core.Plugins.ObservableStreamPlugin.GetBoxObservable(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. StreamPlugin might require unreferenced code.
IL2026 Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeInternal(ComActivationContextInternal): Using member 'Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeImpl(ComActivationContextInternal, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Built-in COM support is not trim compatible. https://aka.ms/dotnet-illink/com
IL2104 Assembly 'Avalonia.Controls.DataGrid' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'Avalonia.Diagnostics' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'Avalonia.Win32' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'Microsoft.CodeAnalysis' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'Microsoft.CodeAnalysis.Scripting' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'ReactiveUI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
IL2104 Assembly 'System.Reactive' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

To Reproduce

Steps to reproduce the behavior:

  1. Install Avalonia templates: dotnet new install Avalonia.Templates
  2. Create a new solution with "Avalonia .NET MVVM App" template
  3. Add a Publish to folder deployment configuration with the following configuration:

rider64_8ocwp0wiV8 A modified csproj may also be necessary:

csproj

true true true win-x64 true

  1. Compile the app with that configuration

Expected behavior

No warnings

Environment

Additional context

Similar to issue 8925. Note: <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> is in the csproj file.

maxkatz6 commented 8 months ago

About severity of these problems:

MethodToCommandConverter, ObservableStreamPlugin are not a problem, since these classes are used only if a developer uses ReflectionBinding. With CompiledBindings these classes are not used.

Built-in COM support probably mentions this issue. In short - current accessibility code uses old COM interop which isn't supported with NativeAOT/trimming. That's pretty much the only actual problem with these warnings.

Anything Avalonia.Diagnostics related (including Microsoft.CodeAnalysis) is expected, as we don't expect Avalonia.Diagnostics project to be used in Release builds. They are not trimming/AOT friendly because of the console feature that requires dynamic code.

ReactiveUI and System.Reactive are expected - these third-party libraries are not trimming/aot friendly. But System.Reactive is improving with the latest versions.

timunie commented 8 months ago

@styris-ame why exactly do you think it is a bug? I think it is more of a feature request if there is anything you think we are missing. Can you specify what the "bug" for you is?

styris-ame commented 8 months ago

@timunie I was going off issue 8925, which is very similar and is listed as a bug. Also, even if they are just meaningless warnings, I would still consider something like this a slight bug. Feel free to change it to a feature request if you see fit, or even close it since it has been explained.

timunie commented 8 months ago

@styris-ame it's still good to track it šŸ‘

alexrp commented 7 months ago

Given that these do not seem to be particularly severe, is there a way to actually suppress them?

Tyrrrz commented 3 months ago

As it stands right now, trying to use Avalonia in an application with trimming enabled produces a bunch of errors. I got around it by manually suppressing each error code, but it doesn't feel right. I'm concerned I may be silencing actual errors that might appear in the future. What's the correct solution in this case?

I'm using Avalonia 11.0.10 with compiled bindings.

image