Skip to content

fix(config): tag browser errors with the deployment, not NODE_ENV - #763

Merged
sydevs-bot merged 7 commits into
mainfrom
claude/fix-737-client-sentry-environment
Sep 12, 2026
Merged

sydevs-bot merged 7 commits into
mainfrom
claude/fix-737-client-sentry-environment

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

  • next.config.mjs — an env block inlining NEXT_PUBLIC_DEPLOYMENT_ENVIRONMENT at build time.
  • src/lib/env/deploymentEnvironment.ts — clientDeploymentEnvironment(), the browser twin.
  • src/instrumentation-client.ts — the browser's one Sentry.init, now tagged with the deployment.
  • src/app/(payload)/api/health/route.ts — the same helper, server-side. Nothing reads the field.
  • docs/environment.md — the canonical statement of the mechanism, plus the line that keeps the Validation section from contradicting it. docs/architecture.md and src/AGENTS.md carry a sentence each.
  • tests/unit/client-deployment-environment.spec.ts — pins the config's chain against the helper's.

Acceptance criteria

# Criterion State
1 Preview browser error tagged pr-<n> ✅ bundle carries environment:"pr-763" beside the DSN
2 Production browser error tagged production ✅ same chain, pinned by the unit spec
3 Local browser error tagged development ⚠️ see below — #771 stops local reporting entirely
4 grep -rn "environment: process.env.NODE_ENV" src/ returns nothing ✅ returns nothing
5 Mechanism documented, docs/architecture.md warning removed ✅

Criterion 3 is the one open question. #771 gates the browser Sentry.init on NODE_ENV === 'production', because .env is git-tracked with a real DSN. So a local run now sends no browser event at all, rather than one tagged development. That is a deliberate decision upstream, and it satisfies the intent of #737 — nothing local is mislabelled production. It does not satisfy the criterion as written. Your call whether that closes #737.

Test results

  • Lean gate: ✓ lint, typecheck, typecheck:tests, test:unit (1719 passed) — re-run after the main merge
  • Build: ✓ RAILWAY_ENVIRONMENT_NAME=pr-763 pnpm build

Manual verification

  1. On this PR's Railway preview, open the admin panel and trigger an error.
  2. Confirm the Sentry event carries environment: pr-763, not production.
  3. curl <preview>/api/health returns "environment": "pr-763".

Notes for reviewer

What the build proves now, and what it could not before. RAILWAY_ENVIRONMENT_NAME=pr-763 pnpm build emits one client chunk containing dsn:"https://b198…",environment:"pr-763",tracesSampleRate:0. The DSN is the new half. Before #771, clientEnv parsed a bare process.env — an empty object in the browser — so the DSN reached no client chunk and Sentry.init never ran. The tag was correct and invisible. That is why #737 waited on #760.

Merging main shrank this PR. #771 deleted ErrorBoundary's duplicate Sentry.init, so the two client call sites became one. ErrorBoundary.tsx is no longer in this diff — it captures through the client instrumentation-client.ts installs, which is where the tag is set. Both conflicts resolved to main's version plus the environment tag.

One judgement worth checking: two helpers, not one. A review pass proposed folding the public key into deploymentEnvironment() as a third ?? link, deleting the twin. Rejected: that puts a process.env.RAILWAY_ENVIRONMENT_NAME read into browser code whose correctness depends on a silent fallthrough through a key Next never inlines there — the same "reads like config, returns nothing" shape as #760. The twin states the build-time dependency in its name.

Detail

Why a build-time value. Next inlines config.env keys as process.env.<KEY> in both bundles, and Railway exposes every service variable to the build (headers() already relies on this). Each environment builds separately, so a preview created next month publishes its own name with no setup. A bundle only ever serves the deployment that built it, so fixing the value at build time is correct rather than a compromise.

The duplicated chain. A .mjs config cannot import the TypeScript helper, so RAILWAY_ENVIRONMENT_NAME ?? RAILWAY_ENVIRONMENT ?? NODE_ENV is written twice. tests/unit/client-deployment-environment.spec.ts imports the real config and asserts both agree, including the legacy variable and an unset NODE_ENV. That last case exists because a development value passes either way — only an unset NODE_ENV separates a matching chain from one carrying an extra link. The first draft did carry an extra ?? 'development'; the review pass caught it, and the case now fails against it.

Why NEXT_PUBLIC_DEPLOYMENT_ENVIRONMENT is absent from ClientEnvSchema and .env.example. Both are for variables a person sets; this one is written by the build. Next substitutes literal process.env.<KEY> expressions only, which is now the repo-wide rule #771 established. docs/environment.md's Validation section names the exception where the add-a-variable procedure is stated, so the two do not contradict each other.

Doc lines the merge made stale, and this push fixed. docs/architecture.md claimed neither browser site reports anything, and named ErrorBoundary as a call site. docs/environment.md twice explained the key in terms of clientEnv, which #771 deleted. Both now state the literal-member-expression rule instead.

Review findings dismissed, with reasons.

  • Convert next.config.mjs to next.config.ts and import the helper, removing the duplication. Out of scope for a fix ticket — it changes how Railway loads the build config, and whether Next's config loader resolves @/ aliases into app source is unverified. Worth its own ticket if the duplication ever bites.
  • The real-config import costs ~270ms cold in the unit lane; hoist the expression into a dependency-free module both import. That is the one assertion no pure spec can make: a correct helper reading a key nothing publishes is exactly the failure here. 270ms against a 20s lane, for the wiring half of the test.
  • Drop the helper's ?? process.env.NODE_ENV as unreachable. It is what keeps the function honest outside a Next build — the unit lane included, where nothing inlines the key.

Security review: ran (the diff touches src/app/.*/api/). No findings. The env block publishes one non-secret string, and /api/health already answered unauthenticated with NODE_ENV.

Closes #737

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_01QSi3rvDs4nANUBKcccRUME

#733 fixed the two server Sentry call sites. The two client ones still
read `process.env.NODE_ENV`, and Railway builds a preview with
`NODE_ENV=production`, so a browser error from a PR preview was tagged
`environment: production` — indistinguishable from a real incident.

A browser bundle reads no environment at runtime, so the name has to be
inlined at build time. `next.config.mjs`'s `env` block derives
`NEXT_PUBLIC_DEPLOYMENT_ENVIRONMENT` from the same Railway variables
`deploymentEnvironment()` reads, and `clientDeploymentEnvironment()`
reads it back. This needs no dashboard variable: Railway exposes every
service variable to the build, and each environment builds separately,
so a preview created next month publishes its own name with no setup.
Fixing it at build time is correct here — a bundle only ever serves the
deployment that built it.

Verified against a real production build with
`RAILWAY_ENVIRONMENT_NAME=pr-737`: `clientDeploymentEnvironment` compiles
to `() => "pr-737"` in `.next/static/chunks`.

The config cannot import the TypeScript helper, so the fallback chain is
written twice. `tests/unit/client-deployment-environment.spec.ts` imports
the real config and asserts the two agree, including the legacy
`RAILWAY_ENVIRONMENT` — the half most likely to be dropped from one copy.
Both new specs were watched failing against the reintroduced defect.

The health route's `environment` field takes the same helper. Nothing
reads that field, so it is consistency rather than a fix.

Refs #737

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CeokcWLWscbWQnSu8cE6ZP
`docs/architecture.md` carried a ⚠ saying the two client Sentry call
sites still read `NODE_ENV` and that #737 tracked it. Replace it with the
mechanism that shipped, and with what a build proved on the way: neither
browser site reports anything today, because both read the DSN through
`clientEnv`, which parses a bare `process.env` — an empty object in the
browser. Filed as #760.

`docs/environment.md` gains the `NEXT_PUBLIC_DEPLOYMENT_ENVIRONMENT`
section: set by the build, never by hand, and not readable through
`clientEnv`.

Refs #737

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CeokcWLWscbWQnSu8cE6ZP
Four review lenses ran over the branch. Three findings applied:

- The config chain ended `?? 'development'`, a fourth link
  `deploymentEnvironment()` does not have. Two copies said to be identical
  had already diverged, and no case covered an unset `NODE_ENV`. Drop the
  literal, and pin the absence with a case that unsets `NODE_ENV` — the
  only state that separates the two chains, since a value of
  `development` passes either way.
- The config comment restated `docs/environment.md`, added in the same
  branch. Cut it to the local facts plus a pointer.
- The health-route comment restated the Railway/NODE_ENV story a fourth
  time. Cut to what is new: nothing reads the field.

Also adds the off-Railway config case, which pins the ticket's
local-development criterion at the layer that decides it.

Refs #737

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CeokcWLWscbWQnSu8cE6ZP
@railway-app

railway-app Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-763 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 12, 2026 at 5:48 am UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-763 September 10, 2026 05:29 Destroyed
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 10, 2026
@sydevs-bot

sydevs-bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 05:38Z address-review · attempt 1 · session · done
  • 05:38Z address-review · attempt 1 · session
  • 00:02Z address-review · attempt 1 · session · done
  • 00:02Z address-review · attempt 1 · session
  • 23:52Z adversarial-review · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot

Copy link
Copy Markdown
Contributor Author

This draft has had no CI activity for hours and no session holds it. Comment or push to wake me.

🤖 sydevs dispatcher

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. awaiting labels Sep 10, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

The session for this item did not finish after 3 attempts. I have stopped retrying. A comment, a verb, or a push from you starts me again.

🤖 sydevs dispatcher

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. awaiting and removed awaiting bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 10, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

The session for this item did not finish after 3 attempts. I have stopped retrying. A comment, a verb, or a push from you starts me again.

🤖 sydevs dispatcher

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed awaiting bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 11, 2026

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧐 Adversarial review

Scope matches the ticket. #737 asked for a decision and two client call sites, and this is option 2 in 68 code lines. All four Sentry environment sites now read one of the two helpers. The health-route change is the tidy-up #737 sanctioned.

One question for you: should this close #737? Its first acceptance criterion stays unobservable until #760 lands. The evidence here is a bundle grep, not a Sentry event.

Findings

  • should-fix — docs/environment.md:79 contradicts the add-a-variable procedure at line 53.
  • consider — src/lib/env/deploymentEnvironment.ts:44 explains the mechanism a sixth time.

What was checked

  • Read #737, the diff, both call sites, client.ts, next.config.mjs, the spec, both docs.
  • The "env beats a dashboard variable" claim is correct. [email protected] dist/build/define-env.js:60 spreads nextConfigEnv after nextPublicEnv.
  • Health-route readers railway.toml:18 and get-railway-preview-url.ts:58 want a 200 only.
  • grep Sentry.init src/ returns four sites. All converted.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread docs/environment.md Resolved
Comment thread src/lib/env/deploymentEnvironment.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 12, 2026
`docs/environment.md`'s Validation section said every `NEXT_PUBLIC_*`
variable gets a Zod rule in `ClientEnvSchema` plus an `.env.example`
entry. `NEXT_PUBLIC_DEPLOYMENT_ENVIRONMENT` has neither, and the section
26 lines below explains why it cannot. Two contradictory procedures sat
in one file, unlinked, and a reader of `src/lib/env/client.ts` had no way
to learn the key exists.

Name the exception where the procedure is stated, and link the section
that owns it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NaKg4ZPsnog297UaDCJxRn
`clientDeploymentEnvironment()` carried 19 lines of JSDoc restating a
mechanism `next.config.mjs`, `docs/environment.md`, `docs/architecture.md`
and `src/AGENTS.md` already state. Point at the canonical statement
instead.

Kept inline, because both are rules about this code rather than story:
the literal `process.env.<KEY>` constraint, which a refactor here would
break silently, and what `?? process.env.NODE_ENV` is for, which a reader
otherwise reads as dead.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NaKg4ZPsnog297UaDCJxRn
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-763 September 12, 2026 00:07 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both adversarial-review threads are addressed and resolved. Pushed a7dcb24.

Lean gate green after the change: lint, both typechecks, 1678 unit tests. main is merged in.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 12, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 12, 2026 00:17
@sydevs-bot
sydevs-bot requested a review from Ardnived September 12, 2026 00:17
@Ardnived

Copy link
Copy Markdown
Contributor

@sydevs-bot #760 has now been merged. Use this to verify the acceptance criteria. If the criteria are met, go ahead and merge this PR

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed awaiting labels Sep 12, 2026
…-sentry-environment

# Conflicts:
#	src/components/ErrorBoundary.tsx
#	src/instrumentation-client.ts
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-763 September 12, 2026 05:45 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Merged main and verified the criteria against a real bundle. Pushed e34c978.

#771 changed the shape. It deleted ErrorBoundary's duplicate Sentry.init, so this PR now touches one client call site, not two. Both conflicts resolved to main's version plus the tag.

Criteria 1, 2, 4 and 5 are met. RAILWAY_ENVIRONMENT_NAME=pr-763 pnpm build emits this into a client chunk:

dsn:"https://b198…",environment:"pr-763",tracesSampleRate:0

The DSN is present. Before #771 it reached no client chunk, which is what made criterion 1 unobservable.

Criterion 3 needs your call. It asks that a local browser error still carry environment: development. Under #771's NODE_ENV === 'production' gate, a local run now sends nothing at all. That is deliberate upstream, but it is not what #737 wrote.

Gate green: lint, both typechecks, 1719 unit tests.

I cannot merge. The loop opens and revises PRs and never merges one. That verb stays yours.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 12, 2026
@sydevs-bot
sydevs-bot merged commit d8503f7 into main Sep 12, 2026
10 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-737-client-sentry-environment branch September 12, 2026 05:56
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 12, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 12, 2026
`main` moved five commits past this branch's base and `tests/COVERAGE.md`
conflicted: both sides appended a row to the same table. Kept both — the
`#743` guard row stays with the two Sentry rows above it, and #750's
translations-seed row follows.

Everything else merged clean. The overlap worth checking was
`src/lib/env/deploymentEnvironment.ts`, which `credentialRejection.ts`
imports: #763 only added `clientDeploymentEnvironment` beside it, so the
server-side read is untouched. `main` changed nothing under
`src/plugins/sentry/` or `src/lib/endpoints/`.

Lean gate green after the merge: lint, typecheck, typecheck:tests, and
1786 unit tests across 135 files.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WMc58cJat8rdZnpNWc4Fnp

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-763 — e34c9781 Deployed Sep 12, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(config): browser errors from a PR preview still report to Sentry as environment=production

3 participants