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

[tests] verify trimmer warnings where appropriate #9076

Merged
merged 1 commit into from
Jul 4, 2024

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Jul 2, 2024

One change we need in the Android workload is to make sure that
trimmer warnings are displayed if a project sets $(IsAotCompatible).
Customers would likely want this set for all platforms if they are
using NativeAOT on iOS or MacCatalyst.

I also wrote a test with somewhat complicated parameters to verify
we get warnings.

First, I can create a warning for both IL2055 and IL3050:

// Member field
Type type = typeof (List<>);
// ...
// Later in OnCreate
Console.WriteLine (type.MakeGenericType (typeof (object)));

The combinations of tests are:

Configuration Property Warning(s)
Debug (defaults) None
Release (defaults) None
Debug TrimMode=full None
Release TrimMode=full IL2055(2)
Release SuppressTrimAnalysisWarnings=false IL2055(2)
Debug IsAotCompatible=true IL2055, IL3050
Release IsAotCompatible=false IL2055(2), IL3050

Some of the cases receive duplicate warnings, but this is expected as
the same behavior occurs in the simplest case:

  • dotnet new console
  • Add the above code to Program.cs
  • dotnet publish -c Release -r win-x64 -p:PublishAot=true
  • Receive warnings from both the Roslyn analyzer and ILC (NativeAOT compiler)

In a future PR, I might try to "fix" the duplicate warnings.

@jonathanpeppers

This comment was marked as outdated.

@jonathanpeppers

This comment was marked as outdated.

@ivanpovazan
Copy link
Member

@jonathanpeppers I repeated your test setup with a dotnet new android app by adjusting the source code so it produces warnings in the same way.

Here is the overview of the expected behaviour and property values (verified by inspecting binlogs and order of evaluation):

Case: TrimMode=full, debug

  • AndroidLinkMode = None
  • PublishTrimmed = ''
  • TrimMode = full
  • SuppressTrimAnalysisWarnings = false (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • EnableAotAnalyzer = ''
  • EnableTrimAnalyzer = ''
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = ''
  • ILLink invoked = No (reason: PublishTrimmed == '')
  • Warnings (Roslyn analyzers) = X
  • Warnings (ILLink) = X
  • Total build warnings = 0
Comment: We don't see any warnings from analyzers because we are not setting (PublishTrimmed, or IsAotComaptible, or IsTrimmable - and with it we don't enable analyzers), additionally in `Debug` builds we don't run ILLink, so we don't get any warnings

Case: TrimMode=full, release

  • AndroidLinkMode = SdkOnly (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: PublishTrimmed=true which is set when Configuration=Release)
  • PublishTrimmed = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: Configuration=Release)
  • TrimMode = full
  • SuppressTrimAnalysisWarnings = false (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • EnableAotAnalyzer = ''
  • EnableTrimAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets)
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = ''
  • ILLink invoked = Yes
  • Warnings (Roslyn analyzers) = IL2055
  • Warnings (ILLink) = IL2055
  • Total build warnings = 2
Comment: We see all the trim warnings, but we don't see AOT warnings because AOT analyzer is not enabled

SuppressTrimAnalysisWarnings=false, debug

  • AndroidLinkMode = None
  • PublishTrimmed = ''
  • TrimMode = partial (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • SuppressTrimAnalysisWarnings = false
  • EnableAotAnalyzer = ''
  • EnableTrimAnalyzer = ''
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = ''
  • ILLink invoked = No (reason: PublishTrimmed == '')
  • Warnings (Roslyn analyzers) = X
  • Warnings (ILLink) = X
  • Total build warnings = 0
Comment: We don't see any warnings from analyzers because we are not setting (PublishTrimmed, or IsAotComaptible, or IsTrimmable - and with it we don't enable analyzers), additionally in `Debug` builds we don't run ILLink, so we don't get any warnings

SuppressTrimAnalysisWarnings=false, release

  • AndroidLinkMode = SdkOnly (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: PublishTrimmed=true which is set when Configuration=Release)
  • PublishTrimmed = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: Configuration=Release)
  • TrimMode = partial (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • SuppressTrimAnalysisWarnings = false
  • EnableAotAnalyzer = ''
  • EnableTrimAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets)
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = ''
  • ILLink invoked = Yes
  • Warnings (Roslyn analyzers) = IL2055
  • Warnings (ILLink) = IL2055
  • Total build warnings = 2
Comment: We see all the trim warnings, but we don't see AOT warnings because AOT analyzer is not enabled

IsAotCompatible=true, debug

  • AndroidLinkMode = None
  • PublishTrimmed = ''
  • TrimMode = partial (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • SuppressTrimAnalysisWarnings = false (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • EnableAotAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets)
  • EnableTrimAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets, reason: IsTrimmable=true which is set if IsAotCompatible=true)
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = true
  • ILLink invoked = No (reason: PublishTrimmed == '')
  • Warnings (Roslyn analyzers) = IL2055, IL3050
  • Warnings (ILLink) = X
  • Total build warnings = 2
Comment: We see all the warnings from analyzers because we set IsAotCompatible=true. We don't see any warnings from ILLink because it doesn't run in `Debug` configuration.

IsAotCompatible=true, release

  • AndroidLinkMode = SdkOnly (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: PublishTrimmed=true which is set when Configuration=Release)
  • PublishTrimmed = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets, reason: Configuration=Release)
  • TrimMode = partial (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • SuppressTrimAnalysisWarnings = false (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • EnableAotAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets)
  • EnableTrimAnalyzer = true (set by: Microsoft.NET.Sdk.Analyzers.targets, reason: IsTrimmable=true which is set if IsAotCompatible=true)
  • EnableSingleFileAnalyzer = true (set by: Microsoft.Android.Sdk.DefaultProperties.targets)
  • IsAotCompatible = true
  • ILLink invoked = Yes
  • Warnings (Roslyn analyzers) = IL2055, IL3050
  • Trim warnings = IL2055
  • Total build warnings = 3
Comment: We see all the warnings from analyzers because we set IsAotCompatible=true. We see only trim warnings from ILLink as ILLink does not report AOT warnings.

Conclusion

The added test in this PR does not seem to have correct expected values, please take a look at my notes above, and let me know if you have any questions.

More importantly, the good news is, that the change in Microsoft.Android.Sdk.DefaultProperties.targets produces the desired behavior for our NativeAOT snippet, so we are on the good track.

@jonathanpeppers

This comment was marked as outdated.

One change we need in the Android workload is to make sure that
trimmer warnings are displayed if a project sets `$(IsAotCompatible)`.
Customers would likely want this set for all platforms if they are
using NativeAOT on iOS or MacCatalyst.

I also wrote a test with somewhat complicated parameters to verify
we get warnings.

First, I can create a warning for both `IL2055` and `IL3050`:

    // Member field
    Type type = typeof (List<>);
    // ...
    // Later in OnCreate
    Console.WriteLine (type.MakeGenericType (typeof (object)));

The combinations of tests are:

| Configuration | Property                           | Warning(s)        |
| ------------- | ---------------------------------- | ----------------- |
| Debug         | (defaults)                         | None              |
| Release       | (defaults)                         | None              |
| Debug         | TrimMode=full                      | None              |
| Release       | TrimMode=full                      | IL2055(2)         |
| Release       | SuppressTrimAnalysisWarnings=false | IL2055(2)         |
| Debug         | IsAotCompatible=true               | IL2055, IL3050    |
| Release       | IsAotCompatible=false              | IL2055(2), IL3050 |

Some of the cases receive duplicate warnings, but this is expected as
the same behavior occurs in the simplest case:

* `dotnet new console`
* Add the above code to `Program.cs`
* `dotnet publish -c Release -r win-x64 -p:PublishAot=true`
* Receive warnings from both the Roslyn analyzer and ILC (NativeAOT compiler)

In a future PR, I might try to "fix" the duplicate warnings.
Copy link
Member

@ivanpovazan ivanpovazan left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for looking into this!

@jonathanpeppers jonathanpeppers merged commit 6b665dc into dotnet:main Jul 4, 2024
57 checks passed
@jonathanpeppers jonathanpeppers deleted the TrimmerWarnings branch July 4, 2024 12:27
grendello added a commit that referenced this pull request Jul 5, 2024
* main:
  [tests] verify trimmer warnings where appropriate (#9076)
  Bump to jbevain/cecil@8c123e1 (#9078)
  [trimming] remove `$(NullabilityInfoContextSupport)` (#9069)
  [build] Bump `$(XABuildToolsVersion)`=35 (#9071)
  [ci] Move PR build to shared pool (#8854)
  Use AsyncTask from xamarin-android-tools (#9017)
  Bump to dotnet/sdk@02c06d398a 9.0.100-preview.7.24351.1 (#9067)
  [trimming] use public `$(MetricsSupport)` property (#9068)
  [ci] Update resourceManagement.yml (#9070)
  Bump to dotnet/android-api-docs@c14203771a (#8992)
  [trimming] remove `$(_AggressiveAttributeTrimming)` by default (#9062)
@github-actions github-actions bot locked and limited conversation to collaborators Aug 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants