Skip to content

Commit

Permalink
Address Trim warnings and prevent future regressions (#3841)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell authored Dec 18, 2024
1 parent 1ecab82 commit a421af5
Show file tree
Hide file tree
Showing 74 changed files with 9,337 additions and 71 deletions.
14 changes: 14 additions & 0 deletions .generated.NoMobile.sln
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Analyzers.Tests", "t
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Console.HeapDump", "samples\Sentry.Samples.Console.HeapDump\Sentry.Samples.Console.HeapDump.csproj", "{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.TrimTest", "test\Sentry.TrimTest\Sentry.TrimTest.csproj", "{6030B748-0000-43B5-B8A8-399AA42F5229}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.MauiTrimTest", "test\Sentry.MauiTrimTest\Sentry.MauiTrimTest.csproj", "{DF92E098-822C-4B94-B96B-56BFB91FBB54}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -520,6 +524,14 @@ Global
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Release|Any CPU.Build.0 = Release|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Release|Any CPU.Build.0 = Release|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -605,5 +617,7 @@ Global
{E36C8DCA-464E-41CB-B189-F58553AAA8D4} = {230B9384-90FD-4551-A5DE-1A5C197F25B6}
{5A17FEF9-07BB-47B8-9883-9C2CC93F09E8} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821} = {21B42F60-5802-404E-90F0-AEBCC56760C0}
{6030B748-0000-43B5-B8A8-399AA42F5229} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{DF92E098-822C-4B94-B96B-56BFB91FBB54} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion .github/actions/environment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ runs:
- name: Pin the Xcode Version
if: runner.os == 'macOS'
shell: bash
run: sudo xcode-select --switch /Applications/Xcode_16.0.app
run: sudo xcode-select --switch /Applications/Xcode_16.2.app

# Needed for Android SDK setup step
- uses: actions/setup-java@v3
Expand Down
52 changes: 49 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
# Pin macos to get the version of XCode that we need: https://github.com/actions/runner-images/issues/10703
# Pin macos to get the version of Xcode that we need: https://github.com/actions/runner-images/issues/10703
os: [ubuntu-latest, windows-latest, macos-15]

steps:
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
strategy:
fail-fast: false
matrix:
# Pin macos to get the version of XCode that we need: https://github.com/actions/runner-images/issues/10703
# Pin macos to get the version of Xcode that we need: https://github.com/actions/runner-images/issues/10703
os: [ubuntu-latest, windows-latest, macos-15]

steps:
Expand Down Expand Up @@ -154,7 +154,7 @@ jobs:
strategy:
fail-fast: false
matrix:
# Pin macos to get the version of XCode that we need: https://github.com/actions/runner-images/issues/10703
# Pin macos to get the version of Xcode that we need: https://github.com/actions/runner-images/issues/10703
os: [ubuntu-latest, windows-latest, macos-15]

steps:
Expand Down Expand Up @@ -187,6 +187,52 @@ jobs:
with:
path: integration-test


trim-analysis:
name: Trim analysis
runs-on: macos-15

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 2 # default is 1 and codecov needs > 1

# We use macOS for the final publishing build so we get all the iOS/macCatalyst targets in the packages
- name: Set Environment Variables
run: echo "CI_PUBLISHING_BUILD=true" >> $GITHUB_ENV

- name: Download sentry-native (macOS)
uses: actions/cache/restore@v4
with:
path: src/Sentry/Platforms/Native/sentry-native
key: sentry-native-macOS-${{ hashFiles('scripts/build-sentry-native.ps1') }}-${{ hashFiles('.git/modules/modules/sentry-native/HEAD') }}
fail-on-cache-miss: true

- name: Setup Environment
uses: ./.github/actions/environment

- name: Build Native Dependencies
uses: ./.github/actions/buildnative

- name: Install Android SDKs
if: runner.os == 'macOS'
run: |
dotnet build src/Sentry/Sentry.csproj -t:InstallAndroidDependencies -f:net8.0-android34.0 -p:AcceptAndroidSDKLicenses=True -p:AndroidSdkPath="/usr/local/lib/android/sdk/"
- name: Publish Test app (macOS)
run: dotnet publish test/Sentry.TrimTest/Sentry.TrimTest.csproj -c Release -r osx-arm64

- name: Publish Test app (Android)
run: dotnet publish test/Sentry.MauiTrimTest/Sentry.MauiTrimTest.csproj -c Release -f net9.0-android35.0 -r android-arm64

# We can't publish iOS applications on CI yet. We'd need a valid bundle identifier and to install the relevant
# certificates/profiles on CI (presumably certs associated with the Sentry org).
# See https://youtrack.jetbrains.com/issue/RIDER-17115/Could-not-find-any-available-provisioning-profiles-for-iOS
# - name: Publish Test app (iOS)
# run: dotnet publish test/Sentry.MauiTrimTest/Sentry.MauiTrimTest.csproj -c Release -f net9.0-ios18.0 -r ios-arm64

test-solution-filters:
runs-on: ubuntu-latest
if: ${{ !startsWith(github.ref_name, 'release/') }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Fixed JNI Error when accessing Android device data from multiple threads ([#3802](https://github.com/getsentry/sentry-dotnet/pull/3802))
- Android - fix bug that prevents logcat.log from getting attached to unhandled events (SIGSEGV Segfault) ([#3694](https://github.com/getsentry/sentry-dotnet/pull/3694))
- Fix "System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'idData')" error propagating OpenTelemetry span ids ([#3850](https://github.com/getsentry/sentry-dotnet/pull/3850))
- Address Trim warnings so that MAUI applications can be compiled AOT ([#3841](https://github.com/getsentry/sentry-dotnet/pull/3841))

### Dependencies

Expand Down
1 change: 1 addition & 0 deletions Sentry-CI-Build-Linux.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"test\\Sentry.AspNet.Tests\\Sentry.AspNet.Tests.csproj",
"test\\Sentry.AspNetCore.Grpc.Tests\\Sentry.AspNetCore.Grpc.Tests.csproj",
"test\\Sentry.AspNetCore.Tests\\Sentry.AspNetCore.Tests.csproj",
"test\\Sentry.AspNetCore.TestUtils\\Sentry.AspNetCore.TestUtils.csproj",
"test\\Sentry.Azure.Functions.Worker.Tests\\Sentry.Azure.Functions.Worker.Tests.csproj",
"test\\Sentry.DiagnosticSource.IntegrationTests\\Sentry.DiagnosticSource.IntegrationTests.csproj",
"test\\Sentry.DiagnosticSource.Tests\\Sentry.DiagnosticSource.Tests.csproj",
Expand Down
14 changes: 14 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Analyzers.Tests", "t
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Console.HeapDump", "samples\Sentry.Samples.Console.HeapDump\Sentry.Samples.Console.HeapDump.csproj", "{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.TrimTest", "test\Sentry.TrimTest\Sentry.TrimTest.csproj", "{6030B748-0000-43B5-B8A8-399AA42F5229}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.MauiTrimTest", "test\Sentry.MauiTrimTest\Sentry.MauiTrimTest.csproj", "{DF92E098-822C-4B94-B96B-56BFB91FBB54}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -520,6 +524,14 @@ Global
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821}.Release|Any CPU.Build.0 = Release|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6030B748-0000-43B5-B8A8-399AA42F5229}.Release|Any CPU.Build.0 = Release|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF92E098-822C-4B94-B96B-56BFB91FBB54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -605,5 +617,7 @@ Global
{E36C8DCA-464E-41CB-B189-F58553AAA8D4} = {230B9384-90FD-4551-A5DE-1A5C197F25B6}
{5A17FEF9-07BB-47B8-9883-9C2CC93F09E8} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{D7DF0B26-AD43-4F8B-9BFE-C4471CCC9821} = {21B42F60-5802-404E-90F0-AEBCC56760C0}
{6030B748-0000-43B5-B8A8-399AA42F5229} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{DF92E098-822C-4B94-B96B-56BFB91FBB54} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
EndGlobalSection
EndGlobal
25 changes: 24 additions & 1 deletion scripts/generate-solution-filters-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ groupConfigs:
windowsOnly:
# .NET Framework projects
- "**/*.AspNet.csproj"
artefacts:
# Any projects in the obj or bin directories
- "**/obj/**/*.csproj"
- "**/bin/**/*.csproj"
trimTests:
- "**/Sentry.TrimTest.csproj"
- "**/Sentry.MauiTrimTest.csproj"

filterConfigs:

Expand All @@ -35,12 +42,13 @@ filterConfigs:
groups:
- "macOnly"
- "windowsOnly"
- "artefacts"
- "trimTests"
patterns:
- "**/*AndroidTestApp.csproj"
- "**/*DeviceTests*.csproj"
- "**/*Maui.Device.TestApp.csproj"
- "**/*SourceGen.csproj"
- "**/*TestUtils*.csproj"

- outputPath: Sentry-CI-Build-macOS.slnf
include:
Expand All @@ -50,6 +58,9 @@ filterConfigs:
- "modules/perfview/**/TraceEvent.csproj"
- "modules/perfview/**/FastSerialization.csproj"
exclude:
groups:
- "artefacts"
- "trimTests"
patterns:
- "**/*AndroidTestApp.csproj"
- "**/*DeviceTests*.csproj"
Expand All @@ -65,6 +76,8 @@ filterConfigs:
exclude:
groups:
- "macOnly"
- "artefacts"
- "trimTests"
patterns:
- "**/*AndroidTestApp.csproj"
- "**/*DeviceTests*.csproj"
Expand All @@ -78,6 +91,8 @@ filterConfigs:
groups:
- "allProjects"
exclude:
groups:
- "artefacts"
patterns:
- "**/Sentry.Bindings*.csproj"
- "benchmarks/**/*.csproj"
Expand Down Expand Up @@ -115,6 +130,9 @@ filterConfigs:
- "**/Sentry.Testing.CrashableApp.csproj"
- "**/Sentry.Tests.csproj"
- "**/Sentry.Analyzers.Tests.csproj"
exclude:
groups:
- "artefacts"

- outputPath: SentryCore.slnf
solution: .generated.NoMobile.sln
Expand Down Expand Up @@ -173,6 +191,9 @@ filterConfigs:
groups:
- "allProjects"
exclude:
groups:
- "artefacts"
- "trimTests"
patterns:
- "**/*Bindings*.csproj"
- "**/*Android*.csproj"
Expand All @@ -184,6 +205,8 @@ filterConfigs:
groups:
- "allProjects"
exclude:
groups:
- "trimTests"
patterns:
- "samples/**/*"
- "**/*Bindings*"
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.AspNetCore.Grpc/Sentry.AspNetCore.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.19.1" />
<PackageReference Include="Google.Protobuf" Version="3.22.5" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.40.0" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions src/Sentry.AspNetCore/Sentry.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Sentry.AspNetCore.Tests" PublicKey="$(SentryPublicKey)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
}
}

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = AotHelper.SuppressionJustification)]
[UnconditionalSuppressMessage("Trimming", "IL2026: RequiresUnreferencedCode", Justification = AotHelper.AvoidAtRuntime)]
[UnconditionalSuppressMessage("Trimming", "IL2075: DynamicallyAccessedMembers", Justification = AotHelper.AvoidAtRuntime)]
private async Task<TransactionContext> StartOrContinueTraceAsync(FunctionContext context)
{
var transactionName = context.FunctionDefinition.Name;
Expand All @@ -101,7 +102,7 @@ private async Task<TransactionContext> StartOrContinueTraceAsync(FunctionContext
// attribute. In that case the route name will always be /api/<FUNCTION_NAME>
// If this is ever a problem for customers, we can potentially see if there are alternate ways to get this info
// from route tables or something. We're not even sure if anyone will use this functionality for now though.
if (!AotHelper.IsNativeAot && !TransactionNameCache.TryGetValue(transactionNameKey, out transactionName))
if (!AotHelper.IsTrimmed && !TransactionNameCache.TryGetValue(transactionNameKey, out transactionName))
{
// Find the HTTP Trigger attribute via reflection
var assembly = Assembly.LoadFrom(context.FunctionDefinition.PathToAssembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Sentry.Internal.DiagnosticSource;
/// </remarks>
internal static class ReflectionHelper
{
[UnconditionalSuppressMessage("TrimAnalyzer", "IL2075", Justification = AotHelper.SuppressionJustification)]
[UnconditionalSuppressMessage("TrimAnalyzer", "IL2075: DynamicallyAccessedMembers", Justification = AotHelper.AvoidAtRuntime)]
public static object? GetProperty(this object obj, string name, IDiagnosticLogger? logger = null)
{
if (AotHelper.IsTrimmed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
<Description>Official Google Cloud Functions integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description>
</PropertyGroup>

<PropertyGroup Condition="'$(EnableAot)' == 'true'">
<IsAotCompatible>true</IsAotCompatible>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Sentry.AspNetCore\Sentry.AspNetCore.csproj" />
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion src/Sentry.Google.Cloud.Functions/SentryStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public override void ConfigureLogging(WebHostBuilderContext context, ILoggingBui

// TODO: refactor this with SentryWebHostBuilderExtensions
var section = context.Configuration.GetSection("Sentry");
logging.Services.Configure<SentryAspNetCoreOptions>(section);
logging.Services.AddSingleton<IConfigureOptions<SentryAspNetCoreOptions>>(
_ => new SentryAspNetCoreOptionsSetup(section)
);

logging.Services.Configure<SentryAspNetCoreOptions>(options =>
{
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.Hangfire/Sentry.Hangfire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<PropertyGroup Condition="'$(EnableAot)' == 'true'">
<!-- Although Sentry.Hangfire is trim compatible, Hangfire.Core produces trim warnings so ultimately neither
Hangfire nor Sentry.Hangfire should be used in applications built using Trimming or AOT compilation -->
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

Expand Down
28 changes: 24 additions & 4 deletions src/Sentry.Maui/Internal/PageNavigationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
using Sentry.Internal;

namespace Sentry.Maui.Internal;

internal static class PageNavigationExtensions
{
private static readonly PropertyInfo? DestinationPageProperty =
typeof(NavigatedFromEventArgs).GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly PropertyInfo? DestinationPageProperty;
private static readonly PropertyInfo? PreviousPageProperty;

private static readonly PropertyInfo? PreviousPageProperty =
typeof(NavigatedToEventArgs).GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.NonPublic);
[UnconditionalSuppressMessage("Trimming", "IL2075: DynamicallyAccessedMembers", Justification = AotHelper.AvoidAtRuntime)]
static PageNavigationExtensions()
{
if (AotHelper.IsTrimmed)
{
return;
}
DestinationPageProperty = typeof(NavigatedFromEventArgs)
.GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.NonPublic);
PreviousPageProperty = typeof(NavigatedToEventArgs)
.GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.NonPublic);
}

/// <summary>
/// Reads the (internal) NavigatedFromEventArgs.DestinationPage property via reflection.
/// Note that this will return null if trimming is enabled.
/// </summary>
public static Page? GetDestinationPage(this NavigatedFromEventArgs eventArgs) =>
DestinationPageProperty?.GetValue(eventArgs) as Page;

/// <summary>
/// Reads the (internal) NavigatedFromEventArgs.PreviousPage property via reflection.
/// Note that this will return null if trimming is enabled.
/// </summary>
public static Page? GetPreviousPage(this NavigatedToEventArgs eventArgs) =>
PreviousPageProperty?.GetValue(eventArgs) as Page;
}
3 changes: 2 additions & 1 deletion src/Sentry.OpenTelemetry/Sentry.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.5.0" />
<!-- Version 1.6.0 is the minimum version that does not have trim warnings -->
<PackageReference Include="OpenTelemetry" Version="1.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a421af5

Please sign in to comment.