Skip to content

Commit

Permalink
Merge pull request #20 from Fresa/fix-exception-reporting
Browse files Browse the repository at this point in the history
Fix exception reporting in the source generator
  • Loading branch information
Fresa authored Jan 27, 2025
2 parents e8a7766 + 524be03 commit 2fe6773
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ jobs:
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v1
id: dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Build
run: dotnet build -c Release
- name: Test
Expand All @@ -29,6 +30,8 @@ jobs:
shell: bash
run: |
git diff --ignore-space-at-eol --ignore-cr-at-eol --exit-code
outputs:
dotnet_version: ${{ steps.dotnet.outputs.dotnet-version }}

release:
name: Create Release
Expand All @@ -38,5 +41,6 @@ jobs:
with:
project_path: Kafka.Protocol
project_name: Kafka.Protocol
dotnet_version: ${{ needs.test.outputs.dotnet_version }}
secrets:
nuget_api_key: ${{ secrets.NUGET_API_KEY }}
7 changes: 7 additions & 0 deletions Kafka.Protocol.SourceGenerator/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Release 1

Check warning on line 1 in Kafka.Protocol.SourceGenerator/AnalyzerReleases.Shipped.md

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

Analyzer release file 'AnalyzerReleases.Shipped.md' has a missing or invalid release header '## Release 1' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 1 in Kafka.Protocol.SourceGenerator/AnalyzerReleases.Shipped.md

View workflow job for this annotation

GitHub Actions / Create Release / Create Release

Analyzer release file 'AnalyzerReleases.Shipped.md' has a missing or invalid release header '## Release 1' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
KP0001 | Compiler | Error | ProtocolGenerator
2 changes: 2 additions & 0 deletions Kafka.Protocol.SourceGenerator/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
56 changes: 33 additions & 23 deletions Kafka.Protocol.SourceGenerator/ProtocolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,38 +788,48 @@ private static Action<SourceProductionContext, T> WithExceptionReporting<T>(
catch (Exception e)
{
var stackTrace = new StackTrace(e, true);
StackFrame? frame = null;
StackFrame? firstFrameWithLineNumber = null;
for (var i = 0; i < stackTrace.FrameCount; i++)
{
frame = stackTrace.GetFrame(i);
var frame = stackTrace.GetFrame(i);
if (frame.GetFileLineNumber() != 0)
{
firstFrameWithLineNumber = frame;
break;
}
}

var location = Location.Create(
frame?.GetFileName() ?? string.Empty,
new TextSpan(),
new LinePositionSpan(
new LinePosition(
frame?.GetFileLineNumber() ?? 0,
frame?.GetFileColumnNumber() ?? 0),
new LinePosition(
frame?.GetFileLineNumber() ?? 0,
frame?.GetFileColumnNumber() + 1 ?? 0)));
var firstStackTraceLocation = firstFrameWithLineNumber == null ?
Location.None :
Location.Create(
firstFrameWithLineNumber.GetFileName(),
new TextSpan(),
new LinePositionSpan(
new LinePosition(
firstFrameWithLineNumber.GetFileLineNumber(),
firstFrameWithLineNumber.GetFileColumnNumber()),
new LinePosition(
firstFrameWithLineNumber.GetFileLineNumber(),
firstFrameWithLineNumber.GetFileColumnNumber() + 1)));

productionContext.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: "CS0001",
title: "Unhandled error",
messageFormat: "{0}",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: e.StackTrace,
customTags: WellKnownDiagnosticTags.AnalyzerException),
location: location,
[e.ToString().Replace("\r\n", " |").Replace("\n", " |")]));
UnhandledException,
location: firstStackTraceLocation,
// Only single line https://github.com/dotnet/roslyn/issues/1455
messageArgs: [e.ToString().Replace("\r\n", " |").Replace("\n", " |")]));
}
};

private static readonly DiagnosticDescriptor UnhandledException =
new(
id: "KP0001",
title: "Unhandled error",
// Only single line https://github.com/dotnet/roslyn/issues/1455
messageFormat: "{0}",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
// Doesn't work
description: null,
customTags: WellKnownDiagnosticTags.AnalyzerException);
}
1 change: 0 additions & 1 deletion Kafka.Protocol/Kafka.Protocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

<ItemGroup>
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 2fe6773

Please sign in to comment.