Skip to content

feat!: make Scope.Environment non-nullable - #5587

Draft
jamescrosswell wants to merge 1 commit into
version7from
feat/scope-environment-non-nullable
Draft

jamescrosswell wants to merge 1 commit into
version7from
feat/scope-environment-non-nullable

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

Summary

Scope.Environment is now declared as string rather than string?.

#5365 made the setter revert null to SentryOptions.Environment, but the property could still return null: a scope whose environment was never set had a null backing field, and SentryOptions.Environment is itself nullable. To make the non-nullable declaration truthful:

  • The getter falls back to SettingLocator.GetEnvironment() (options → SENTRY_ENVIRONMENTproduction/debug), the same value the Enricher already stamps on events.
  • The setter keeps accepting null ([AllowNull], so it still satisfies IEventLike.Environment) and resets to that resolved default.
  • Scope.Apply copies only an explicitly set environment between scopes. Otherwise the non-null getter would make ??= never copy onto a cloned scope.

Notes for review

  • Breaking change (public API signature), hence targeting version7.
  • Side effect: Apply to an event now writes the resolved default environment from the scope rather than leaving it for the Enricher. The value is identical.
  • The Populate_RouteData_SetToScope snapshot now includes Environment: production, since serializing a scope reads the resolved value.
  • Apply_Environment_Null asserted a target scope's environment stays null, which is no longer possible. It now asserts the options fallback.

Closes #5388

🤖 Generated with Claude Code

When no environment has been set on the scope, the getter now falls back
to the environment resolved from the options (options, SENTRY_ENVIRONMENT,
then the default), so it never returns null.

Closes #5388

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (version7@8d4f28b). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             version7    #5587   +/-   ##
===========================================
  Coverage            ?   74.73%           
===========================================
  Files               ?      515           
  Lines               ?    18902           
  Branches            ?     3689           
===========================================
  Hits                ?    14126           
  Misses              ?     3896           
  Partials            ?      880           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell jamescrosswell linked an issue Sep 17, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make Scope.Environment non nullable (next major)

1 participant