Skip to content

Default SentryOptions.Debug = true for Debug build configurations #4330

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Features

- Added StartSpan and GetTransaction methods to the SentrySdk ([#4303](https://github.com/getsentry/sentry-dotnet/pull/4303))
- `SentryOptions.Debug` now defaults to `true` for Debug build configurations ([#4330](https://github.com/getsentry/sentry-dotnet/pull/4330))

### Fixes

Expand Down
6 changes: 6 additions & 0 deletions samples/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

<!-- Workaround for hang on compile issue. See https://github.com/xamarin/xamarin-macios/issues/17825#issuecomment-1478568270. -->
<PropertyGroup Condition="'$(Configuration)' == 'Release' And $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
<MtouchUseLlvm>false</MtouchUseLlvm>
Expand Down
1 change: 1 addition & 0 deletions samples/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry.Bindings.Cocoa\buildTransitive\Sentry.Bindings.Cocoa.targets"
Condition="'$(OutputType)' == 'Exe' And ('$(TargetPlatformIdentifier)' == 'ios' Or '$(TargetPlatformIdentifier)' == 'maccatalyst')" />
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry\Platforms\Native\buildTransitive\Sentry.Native.targets" />
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry\buildTransitive\Sentry.SourceGenerators.targets" />
Copy link
Member

Choose a reason for hiding this comment

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

note: We deleted Sentry.SourceGenerators.targets in #4333.

It was redundant in the NuGet package, as only Sentry.props and Sentry.targets are automatically picked up in consuming projects (via PackageReference), and Sentry.SourceGenerators.targets was never Imported in any of these MSBuild files either.


<Target Name="GenerateSharedDsnConstant"
BeforeTargets="BeforeCompile"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
<Using Include="Android.App.Activity" Alias="Activity" />

Expand Down Expand Up @@ -42,7 +39,4 @@
<PublishTrimmed>true</PublishTrimmed>
<AndroidLinkMode>full</AndroidLinkMode>
</PropertyGroup>

<!--In order to test our analyzers with this project, we manually reference our targets file. User do not need to do this-->
<Import Project="..\..\src\Sentry\buildTransitive\Sentry.SourceGenerators.targets" />
</Project>
6 changes: 0 additions & 6 deletions samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,5 @@
<ItemGroup>
<!-- <PackageReference Include="Sentry" Version="..." /> -->
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

<!--In order to test our analyzers with this project, we manually reference our targets file. User do not need to do this-->
<Import Project="..\..\src\Sentry\buildTransitive\Sentry.SourceGenerators.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,5 @@
<ItemGroup>
<!-- <PackageReference Include="Sentry" Version="..." /> -->
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

<!--In order to test our analyzers with this project, we manually reference our targets file. User do not need to do this-->
<Import Project="..\..\src\Sentry\buildTransitive\Sentry.SourceGenerators.targets" />
</Project>
3 changes: 0 additions & 3 deletions samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,4 @@
<!-- Check SDK users will be able to resolve the latest version of CommunityToolkit.Mvvm (our Integration targets 8.3.2 -->
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0"/>
</ItemGroup>

<!--In order to test our analyzers with this project, we manually reference our targets file. User do not need to do this-->
<Import Project="..\..\src\Sentry\buildTransitive\Sentry.SourceGenerators.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Sentry.SourceGenerators;


/// <summary>
/// Generates the necessary msbuild variables
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Sentry/CompilerServices/BuildProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,27 @@ public static void Initialize(Dictionary<string, string> properties)
{
Values ??= properties.AsReadOnly();
}

/// <summary>
/// Tries to retrieve a boolean value from the build properties.
/// </summary>
/// <param name="key">The item key</param>
/// <param name="value">The value (if any)</param>
/// <returns>True if the key was found, false otherwise</returns>
public static bool TryGetBoolean(string key, out bool value)
Copy link
Member

@Flash0ver Flash0ver Jul 9, 2025

Choose a reason for hiding this comment

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

suggestion: make internal

I don't think this needs to be accessible from the user's assembly where the Generator is emitting source to.


On the same note ... I think that the only member here, that needs to be accessible from the user's assembly, is the method Initialize(Dictionary<string, string>).

So I'm wondering if we should also make the Values property internal, since it should only be Sentry code reading it. And it's only the Initialize method that needs to be accessible for the source-generated [ModuleInitializer] to work.

Technically a breaking change, but since our source generator wasn't emitting any source code up until now, this IReadOnlyDictionary<string, string>? was always null anyways, so perhaps now may just be the time to make the Values property internal too.

Perhaps
see also https://github.com/getsentry/sentry-dotnet/pull/4330/files#r2194786902
we could make the Values property all private, so that Sentry also only access it via internal methods.

{
value = false;
if (!(Values?.TryGetValue(key, out var foundValue) ?? false))
{
return false;
}

if (!bool.TryParse(foundValue, out var result))
{
return false;
}

value = result;
return true;
}
}
20 changes: 2 additions & 18 deletions src/Sentry/Internal/AotHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ static AotHelper()
IsTrimmed = CheckIsTrimmed();
}


[UnconditionalSuppressMessage("Trimming", "IL2026: RequiresUnreferencedCode", Justification = AvoidAtRuntime)]
private static bool CheckIsTrimmed()
{
if (TryGetBoolean("publishtrimmed", out var trimmed))
if (BuildProperties.TryGetBoolean("PublishTrimmed", out var trimmed))
{
return trimmed;
}

if (TryGetBoolean("publishaot", out var aot))
if (BuildProperties.TryGetBoolean("PublishAot", out var aot))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is a major bug that was introduced by the initial PR including the source generator...

Copy link
Member

@Flash0ver Flash0ver Jul 9, 2025

Choose a reason for hiding this comment

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

Great catch!
MSBuild is case-insensitive, and so should be the source that the [Generator] emits.
Will fix in #4331.

Copy link
Member

@Flash0ver Flash0ver Jul 10, 2025

Choose a reason for hiding this comment

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

Fixed in #4333:

Changed the generated code to use case-insensitive lookup:

-BuildProperties.Initialize(new Dictionary<string, string>()
+BuildProperties.Initialize(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)

{
return aot;
}
Expand All @@ -31,19 +30,4 @@ private static bool CheckIsTrimmed()
var stackTrace = new StackTrace(false);
return stackTrace.GetFrame(0)?.GetMethod() is null;
}

private static bool TryGetBoolean(string key, out bool value)
{
value = false;
if (BuildProperties.Values?.TryGetValue(key, out var aotValue) ?? false)
{
if (bool.TryParse(aotValue, out var result))
{
value = result;
return true;
}
}

return false;
}
}
13 changes: 13 additions & 0 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sentry.CompilerServices;
using Sentry.Extensibility;
using Sentry.Http;
using Sentry.Infrastructure;
Expand Down Expand Up @@ -1324,6 +1325,18 @@ public SentryOptions()
);

NetworkStatusListener = new PollingNetworkStatusListener(this);

#if NET8_0_OR_GREATER
Copy link
Member

@Flash0ver Flash0ver Jul 9, 2025

Choose a reason for hiding this comment

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

suggestion: Remove the preprocessor directive.

User code could build a .NET Framework app or a netcoreapp or older net app, while using a .NET SDK - or another Toolchain invoking the Compiler - that is compatible with Roslyn 4.3, such as the .NET SDK 6.0.400 or above.

In this case, I don't see a reason for "Auto Debug Mode" not to work.

The only thing, that we need to make very sure of, is that the source code that our Generator emits is compatible with <LangVersion>7.3</LangVersion>, which is the default C# language version used for .NET Standard 2.0 and .NET Framework TFMs.
Or alternatively let the Generator only emit code if the consuming project supports the C# language version that our generated source code requires via CSharpParseOptions.LanguageVersion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looks like from #4333 we need to leave the #if clause in there but we can change it to #if NET5_0_OR_GREATER

Copy link
Member

@Flash0ver Flash0ver Jul 10, 2025

Choose a reason for hiding this comment

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

Indeed ... I was not able to remove it entirely from the generated source.
The highest dependency is ModuleInitializerAttribute, available since net5.0.

// If source generators are available, we can detect the user's build configuration and use this
// to infer whether to enable debug mode by default.
// See: https://develop.sentry.dev/sdk/expected-features/#auto-debug-mode
string? buildConfig = null;
BuildProperties.Values?.TryGetValue("Configuration", out buildConfig);
if (buildConfig == "Debug")
Copy link
Member

Choose a reason for hiding this comment

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

question: Case-sensitivity?

I wonder if this string equality check should be case insensitive 🤔

buildConfig.Equals("Debug", StringComparison.OrdinalIgnoreCase)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it should, but we should configure the underlying dictionary to be case insensitive... that way we don't have to worry about it in method calls like this right?

Copy link
Member

Choose a reason for hiding this comment

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

Via #4333, the generated lookup type is now case-insensitive:

-BuildProperties.Initialize(new Dictionary<string, string>()
+BuildProperties.Initialize(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)

So we can lookup the "Configuration" Property case-insensitively.

But comparing the Value of the "Configuration"-Property here should also be done in a case-insensitive fashion. Or am I mixing something up here 🤔?

Comment on lines +1333 to +1335
Copy link
Member

Choose a reason for hiding this comment

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

nitpick(style): Could we make this a one-liner?

I find this code a bit verbose.
Perhaps we could make this a one-liner:

if (BuildProperties.TryGetString("Configuration", out var buildConfig) && buildConfig == "Debug")

by adding a new internal method to BuildProperties:

// public static class BuildProperties

internal static bool TryGetString(string key, [NotNullWhen(true)] out string? value)
{
  if (Values?.TryGetValue(key, out var foundValue) == true)
  {
    value = foundValue;
    return true;
  }

  value = null;
  return false;
}

and perhaps we could make the BuildProperties.Values property all private, so that we only have nicely consumable methods that work around the nullability of the IReadOnlyDictionary<string, string>.

{
Debug = true;
}
#endif
}

/// <summary>
Expand Down
Loading
Loading