Skip to content

Samples fail the first build in a fresh checkout when SENTRY_DSN is set #5695

Description

@jamescrosswell

Description

With SENTRY_DSN set, the first build of a fresh checkout fails in every sample that reads the DSN from the generated EnvironmentVariables class. Building again succeeds.

rm -f samples/EnvironmentVariables.g.cs
SENTRY_DSN=https://key@sentry.invalid/1 dotnet build samples/Sentry.Samples.AspNetCore.Serilog
# error CS0103: The name 'EnvironmentVariables' does not exist in the current context
SENTRY_DSN=https://key@sentry.invalid/1 dotnet build samples/Sentry.Samples.AspNetCore.Serilog
# Build succeeded

The affected samples are Android, Blazor WebAssembly, ASP.NET Core Serilog, iOS, Mac Catalyst and MAUI. A full dotnet build Sentry-CI-Build-macOS.slnf in a new worktree fails on all of them.

Cause

samples/Directory.Build.targets writes samples/EnvironmentVariables.g.cs in the GenerateSharedDsnConstant target (BeforeTargets="BeforeCompile"), but compiles it through a static item:

<Compile Include="$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs" Condition="Exists('$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs')" />

MSBuild evaluates static items before any target runs, so the build that creates the file doesn't compile it. The file is gitignored, so this happens once in every new clone or worktree, and after every git clean -fdx. CI doesn't set SENTRY_DSN, so it isn't affected.

Suggested fix

Add the Compile item from inside GenerateSharedDsnConstant, so the build that writes the file also compiles it. Builds without SENTRY_DSN should behave as they do now.

Several sample projects write the same shared file during a parallel build, so the fix shouldn't introduce a race. For example, use WriteOnlyWhenDifferent="true", or write the file per project under $(IntermediateOutputPath).

A fix should mean that the first build after deleting samples/EnvironmentVariables.g.cs succeeds with SENTRY_DSN set.

In a worktree, building any sample also hits #5655. Pass -p:CI=true after building src/Sentry/Sentry.csproj once to avoid it.

Activity

  1. linear-code commented on Oct 8, 2026

    @linear-code
  2. added
    .NETPull requests that update .net code
    BugSomething isn't working
    on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .net codeBugSomething isn't workingSamples

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions