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

Change FileName to be relative to project root #1739

Merged
merged 18 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Send project root path with events ([#1739](https://github.com/getsentry/sentry-dotnet/pull/1739))

### Fixes

- Fix error with `ConcurrentHashMap` on Android <= 9 ([#1761](https://github.com/getsentry/sentry-dotnet/pull/1761))
Expand Down
17 changes: 17 additions & 0 deletions src/Sentry/AttributeReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

namespace Sentry
{
internal static class AttributeReader
{
public static bool TryGetProjectDirectory(Assembly assembly, out string? projectDirectory)
{
projectDirectory = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
Copy link
Member

Choose a reason for hiding this comment

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

Is this safe, and/or does this work with AOT? Like IL2CPP and net6.0-ios for example?
Just curious if it'll at least return null or it'll throw

.SingleOrDefault(x => x.Key == "Sentry.ProjectDirectory")
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
?.Value;
return projectDirectory != null;
}
}
}
13 changes: 11 additions & 2 deletions src/Sentry/Extensibility/SentryStackTraceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal IEnumerable<SentryStackFrame> CreateFrames(StackTrace stackTrace, bool
protected SentryStackFrame InternalCreateFrame(StackFrame stackFrame, bool demangle)
{
const string unknownRequiredField = "(unknown)";

string? projectPath = null;
var frame = new SentryStackFrame();
if (GetMethod(stackFrame) is { } method)
{
Expand Down Expand Up @@ -162,11 +162,20 @@ protected SentryStackFrame InternalCreateFrame(StackFrame stackFrame, bool deman
{
frame.InApp = false;
}

AttributeReader.TryGetProjectDirectory(method.Module.Assembly, out projectPath);
}

frame.ConfigureAppFrame(_options);

frame.FileName = stackFrame.GetFileName();
var frameFileName = stackFrame.GetFileName();
if (projectPath != null &&
frameFileName != null)
{
frameFileName = frameFileName.Replace(projectPath, "");
}

frame.FileName = frameFileName;

// stackFrame.HasILOffset() throws NotImplemented on Mono 5.12
var ilOffset = stackFrame.GetILOffset();
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry/Sentry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
<Content Include="buildTransitive\Sentry.props" PackagePath="buildTransitive\Sentry.props" />
<Content Include="buildTransitive\Sentry.props" PackagePath="build\Sentry.props" />
SimonCropp marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions src/Sentry/buildTransitive/Sentry.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SentryAttributesFile>Sentry.Attributes$(MSBuildProjectExtension.Replace('proj', ''))</SentryAttributesFile>
</PropertyGroup>
<Target Name="WriteSentryAttributes"
Condition="$(Language) == 'VB' or $(Language) == 'C#' or $(Language) == 'F#'"
BeforeTargets="BeforeCompile;CoreCompile"
Inputs="$(MSBuildAllProjects)"
Outputs="$(IntermediateOutputPath)$(SentryAttributesFile)">
<PropertyGroup>
<SentryAttributesFilePath>$(IntermediateOutputPath)$(SentryAttributesFile)</SentryAttributesFilePath>
</PropertyGroup>
<ItemGroup>
<SentryAttributes Include="System.Reflection.AssemblyMetadata">
<_Parameter1>Sentry.ProjectDirectory</_Parameter1>
<_Parameter2>$(ProjectDir)</_Parameter2>
</SentryAttributes>
<SentryAttributes Include="System.Reflection.AssemblyMetadata"
Condition="'$(SolutionDir)' != '' And '$(SolutionDir)' != '*Undefined*'">
<_Parameter1>Sentry.SolutionDirectory</_Parameter1>
<_Parameter2>$(SolutionDir)</_Parameter2>
</SentryAttributes>
<!-- Ensure not part of Compile, as a workaround for https://github.com/dotnet/sdk/issues/114 -->
<Compile Remove="$(SentryAttributesFilePath)" />
</ItemGroup>
<WriteCodeFragment AssemblyAttributes="@(SentryAttributes)"
Language="$(Language)"
OutputFile="$(SentryAttributesFilePath)">
<Output TaskParameter="OutputFile" ItemName="Compile" Condition="$(Language) != 'F#'" />
Copy link
Member

Choose a reason for hiding this comment

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

Condition="$(Language) != 'F#'" does this mean we want this for VB or a possible IronRuby or something if that's a thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for some reason the item name for F# is CompileBefore but for VB and c# it is Compile.

for other languages (like IronRuby) i have not tested. do you want me to include any other languages? Currently they will be a no-op

<Output TaskParameter="OutputFile" ItemName="CompileBefore" Condition="$(Language) == 'F#'" />
<Output TaskParameter="OutputFile" ItemName="FileWrites" />
</WriteCodeFragment>
</Target>
</Project>
12 changes: 12 additions & 0 deletions test/Sentry.Tests/AttributeReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using AttributeReader = Sentry.AttributeReader;

public class AttributeReaderTests
{
[Fact]
Copy link
Member

Choose a reason for hiding this comment

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

Cool, this should run on iOS AOT'ed so we would see how it behaves there. Assuming the tests are running there following #1703

public void Simple()
{
var assembly = typeof(AttributeReaderTests).Assembly;
Assert.True(AttributeReader.TryGetProjectDirectory(assembly, out var projectDirectory));
Assert.NotNull(projectDirectory);
}
}
19 changes: 19 additions & 0 deletions test/Sentry.Tests/Internals/SentryStackTraceFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// ReSharper disable once CheckNamespace
// Stack trace filters out Sentry frames by namespace
Expand Down Expand Up @@ -105,6 +106,24 @@ public void Create_WithExceptionAndAttachStackTraceOptionOn_HasStackTrace()
Assert.Equal(new StackTrace(exception, true).FrameCount, stackTrace?.Frames.Count);
}

[Fact]
public void FileNameShouldBeRelative()
{
Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
_fixture.SentryOptions.AttachStacktrace = true;
var sut = _fixture.GetSut();

Exception exception;
try
{
Throw();
void Throw() => throw new();
}
catch (Exception e) { exception = e; }

Assert.Equal(@"Internals\SentryStackTraceFactoryTests.cs", sut.Create(exception)!.Frames.First().FileName);
}

[Theory]
[InlineData(StackTraceMode.Original, "ByRefMethodThatThrows")]
[InlineData(StackTraceMode.Enhanced, "(Fixture f, int b) SentryStackTraceFactoryTests.ByRefMethodThatThrows(int value, in int valueIn, ref int valueRef, out int valueOut)")]
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/Sentry.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@
</Compile>
</ItemGroup>

<Import Project="$(ProjectDir)..\..\src\Sentry\buildTransitive\Sentry.props" />

</Project>