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 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
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
16 changes: 16 additions & 0 deletions src/Sentry/AttributeReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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

.FirstOrDefault(x => x.Key == "Sentry.ProjectDirectory")
?.Value;
return projectDirectory != null;
}
}
}
12 changes: 10 additions & 2 deletions src/Sentry/Extensibility/SentryStackTraceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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 @@ -178,11 +178,19 @@ 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>
<None Include="buildTransitive\Sentry.props" Pack="true" PackagePath="buildTransitive\Sentry.props" />
<None Include="buildTransitive\Sentry.props" Pack="true" PackagePath="build\Sentry.props" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions src/Sentry/buildTransitive/Sentry.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 Condition="$(Language) != 'F#'"
TaskParameter="OutputFile"
ItemName="Compile"/>
<Output Condition="$(Language) == 'F#'"
TaskParameter="OutputFile"
ItemName="CompileBefore"/>
<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 @@
namespace Sentry.Tests;

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
FileName: {ProjectDirectory}Internals/SentryStackTraceFactoryTests.cs,
FileName: Internals/SentryStackTraceFactoryTests.cs,
Function: void SentryStackTraceFactoryTests.GenericMethodThatThrows<T>(T value),
InApp: true
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
FileName: {ProjectDirectory}Internals/SentryStackTraceFactoryTests.cs,
FileName: Internals/SentryStackTraceFactoryTests.cs,
Function: GenericMethodThatThrows,
Module: Other.Tests.Internals.SentryStackTraceFactoryTests,
InApp: true
Expand Down
23 changes: 23 additions & 0 deletions test/Sentry.Tests/Internals/SentryStackTraceFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ public void Create_WithExceptionAndAttachStackTraceOptionOn_HasStackTrace()
Assert.Equal(new StackTrace(exception, true).FrameCount, stackTrace?.Frames.Count);
}

[SkippableFact]
public void FileNameShouldBeRelative()
{
Skip.If(RuntimeInfo.GetRuntime().IsMono());

_fixture.SentryOptions.AttachStacktrace = true;
var sut = _fixture.GetSut();

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

var expected = Path.Combine("Internals", "SentryStackTraceFactoryTests.cs");
Assert.Equal(expected, sut.Create(exception)!.Frames[0].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="..\..\src\Sentry\buildTransitive\Sentry.props" />

</Project>