Skip to content

build: Fix reference to Sentry.SourceGenerators #4307

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 5 commits into
base: main
Choose a base branch
from

Conversation

Flash0ver
Copy link
Member

supersedes #4290

git clean -fdx
dotnet pack ./src/Sentry/Sentry.csproj --configuration Debug

before:
error : Could not find a part of the path './sentry-dotnet/src/Sentry.SourceGenerators/bin/Release/netstandard2.0'

after:
Build succeeded


#skip-changelog

@Flash0ver Flash0ver self-assigned this Jun 25, 2025
@Flash0ver Flash0ver added Repository Maintenance .NET Pull requests that update .net code Roslyn The .NET Compiler Platform, Roslyn Components and Extensions, Microsoft.CodeAnalysis labels Jun 25, 2025
@bruno-garcia
Copy link
Member

@sentry review

Copy link

On it! We are reviewing the PR and will provide feedback shortly.

Copy link

PR Description

This pull request aims to improve the packaging of the Sentry.SourceGenerators project as a Roslyn analyzer within the main Sentry NuGet package. The goal is to ensure that the source generators are correctly included and function as expected during the build process of projects that consume the Sentry SDK.

Click to see more

Key Technical Changes

The primary technical change is the replacement of a direct file inclusion (of the compiled Sentry.SourceGenerators.dll) with an MSBuild target-based approach. This involves using a <ProjectReference> to the Sentry.SourceGenerators project and a custom MSBuild target (_SentryPackAnalyzers) to build the source generators project and include its output assembly in the analyzers/dotnet/cs directory of the NuGet package. The SetTargetFramework attribute on the ProjectReference ensures the generator is built for netstandard2.0.

Architecture Decisions

The architectural decision is to leverage MSBuild's extensibility to dynamically build and package the analyzer. This approach avoids hardcoding file paths and ensures that the analyzer is always built with the correct configuration for the target environment. This also allows for better control over the build process and potential customization in the future.

Dependencies and Interactions

This change introduces a dependency on the MSBuild system to correctly build and package the analyzer. It interacts with the NuGet packaging process by adding the analyzer assembly to the appropriate directory within the package. It depends on the Sentry.SourceGenerators project being buildable and producing a valid analyzer assembly.

Risk Considerations

Potential risks include issues with the MSBuild target not correctly building or packaging the analyzer, leading to projects consuming the Sentry SDK not benefiting from the source generators. There's also a risk that changes to the Sentry.SourceGenerators project could break the build process if the MSBuild target is not updated accordingly. The SetTargetFramework attribute on the ProjectReference is unconventional and might cause issues with some MSBuild versions.

Notable Implementation Details

The _SentryPackAnalyzers target is defined to run before the _GetPackageFiles target, ensuring that the analyzer is built and included in the package before the package is created. The SentryAnalyzersPack metadata on the ProjectReference is used to identify the project that should be packaged as an analyzer. The Visible="false" attribute on the <None Include> element prevents the analyzer assembly from being included in the project's output directory.

@Flash0ver Flash0ver marked this pull request as draft June 27, 2025 10:31
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false" />
<ProjectReference Include="..\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj" OutputItemType="" ReferenceOutputAssembly="false" PrivateAssets="all" SetTargetFramework="TargetFramework=netstandard2.0" SentryAnalyzersPack="true" />
Copy link
Member Author

Choose a reason for hiding this comment

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

note: parallel builds

Workflows sometimes run into:

/Users/runner/.dotnet/sdk/9.0.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(323,5): error MSB4018: System.IO.IOException: The process cannot access the file '/Users/runner/work/sentry-dotnet/sentry-dotnet/src/Sentry.SourceGenerators/bin/Release/netstandard2.0/Sentry.SourceGenerators.deps.json' because it is being used by another process. [/Users/runner/work/sentry-dotnet/sentry-dotnet/src/Sentry.SourceGenerators/Sentry.SourceGenerators.csproj]

@@ -206,11 +206,12 @@
<!-- See https://learn.microsoft.com/dotnet/core/project-sdk/overview -->
<!-- See also https://github.com/ViktorHofer/PackAsAnalyzer -->
<ItemGroup>
<ProjectReference Include="..\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj" OutputItemType="" ReferenceOutputAssembly="false" PrivateAssets="all" SetTargetFramework="TargetFramework=netstandard2.0" SentryAnalyzersPack="true" />
<SentryAnalyzerProjectReference Include="..\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

We've got this in various other places throughout the solution. I'm wondering if it should be in one or more of the Directory.Build.props files (e.g. the one in the Samples directory, at the very least).

<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"/>

<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>

<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>

<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>

@jamescrosswell jamescrosswell self-requested a review July 2, 2025 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Pull requests that update .net code Repository Maintenance Roslyn The .NET Compiler Platform, Roslyn Components and Extensions, Microsoft.CodeAnalysis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants