Skip to content

Commit

Permalink
[trimming] TrimMode=full in debug mode, should enable analyzers (#9320
Browse files Browse the repository at this point in the history
)

For NativeAOT scenarios, projects would set:

	<PublishAot>true</PublishAot>

For both `Debug` and `Release`, this allows you to get the same set
of analyzers in both configurations.  You'd want to see the same
warnings for both.

For Android, the project template is currently doing:

	<PropertyGroup Condition="'$(Configuration)' == 'Release'">
	  <TrimMode>Full</TrimMode>
	</PropertyGroup>

but this would result in a *different* set of warnings between
`Debug` and `Release` configuration builds!

Instead, we can do:

	<PropertyGroup>
	  <TrimMode>Full</TrimMode>
	</PropertyGroup>

And then add a new default, such as:

	<EnableTrimAnalyzer
	    Condition=" '$(EnableTrimAnalyzer)' == '' and '$(TrimMode)' == 'full' "
	>true</EnableTrimAnalyzer>

`$(TrimMode)=Full` does *not* enable the trimmer, so other defaults
in the `Debug` configuration build should remain unchanged.

So, the new behavior is:

  * For `$(Configuration)=Debug` projects,
  * `$(PublishTrimmed)=false` (default, no trimmer)
  * `$(TrimMode)=Full` (project template)
  * `$(EnableTrimAnalyzer)=true` (new default)
  * You get the same set of warnings in `Debug` and `Release`
    configuration builds.

I also reworded the comment in the project template slightly, to
mention it enables analyzers.
  • Loading branch information
jonathanpeppers authored Sep 26, 2024
1 parent 5113600 commit 118e894
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/Microsoft.Android.Templates/android-wear/AndroidApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);XA4218</MSBuildWarningsAsMessages>
</PropertyGroup>
<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<!--
Enables trim analyzers and full trimming during Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<TrimMode>full</TrimMode>
</PropertyGroup>
<ItemGroup>
Expand Down
10 changes: 4 additions & 6 deletions src/Microsoft.Android.Templates/android/AndroidApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
<ApplicationId>com.companyname.AndroidApp1</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
</PropertyGroup>
<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<!--
Enables trim analyzers and full trimming during Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<TrimMode>full</TrimMode>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<VerifyDependencyInjectionOpenGenericServiceTrimmability Condition="'$(VerifyDependencyInjectionOpenGenericServiceTrimmability)' == '' and '$(PublishTrimmed)' == 'true'">false</VerifyDependencyInjectionOpenGenericServiceTrimmability>
<VerifyDependencyInjectionOpenGenericServiceTrimmability Condition="'$(VerifyDependencyInjectionOpenGenericServiceTrimmability)' == ''">true</VerifyDependencyInjectionOpenGenericServiceTrimmability>
<EnableSingleFileAnalyzer Condition="'$(EnableSingleFileAnalyzer)' == ''">true</EnableSingleFileAnalyzer>
<EnableTrimAnalyzer Condition="'$(EnableTrimAnalyzer)' == '' and '$(TrimMode)' == 'full'">true</EnableTrimAnalyzer>

<!-- XA feature switches defaults -->
<VSAndroidDesigner Condition="'$(VSAndroidDesigner)' == ''">false</VSAndroidDesigner>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void BuildHasNoWarnings (bool isRelease, bool xamarinForms, bool multidex
[TestCase ("", new string [0], false)]
[TestCase ("", new string [0], true)]
[TestCase ("SuppressTrimAnalysisWarnings=false", new string [] { "IL2055" }, true, 2)]
[TestCase ("TrimMode=full", new string [0], false)]
[TestCase ("TrimMode=full", new string [] { "IL2055" }, false, 1)]
[TestCase ("TrimMode=full", new string [] { "IL2055" }, true, 2)]
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, false)]
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, true, 3)]
Expand Down

0 comments on commit 118e894

Please sign in to comment.