Repository navigation
fix(config): tag browser errors with the deployment, not NODE_ENV - #763
Conversation
#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
|
🚅 Deployed to the SahajCloud-pr-763 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
|
This draft has had no CI activity for hours and no session holds it. Comment or push to wake me. 🤖 sydevs dispatcher |
|
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 |
|
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
left a comment
There was a problem hiding this comment.
🧐 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:79contradicts the add-a-variable procedure at line 53.consider—src/lib/env/deploymentEnvironment.ts:44explains 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 "
envbeats a dashboard variable" claim is correct.[email protected]dist/build/define-env.js:60spreadsnextConfigEnvafternextPublicEnv. - Health-route readers
railway.toml:18andget-railway-preview-url.ts:58want 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
`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
…-sentry-environment
|
Both adversarial-review threads are addressed and resolved. Pushed
Lean gate green after the change: lint, both typechecks, 1678 unit tests. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
@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 |
…-sentry-environment # Conflicts: # src/components/ErrorBoundary.tsx # src/instrumentation-client.ts
|
Merged #771 changed the shape. It deleted Criteria 1, 2, 4 and 5 are met. 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 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 |
`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
Summary
production.next.config.mjspublishes the deployment name to the bundle. No dashboard variable, on any environment.Changes
next.config.mjs— anenvblock inliningNEXT_PUBLIC_DEPLOYMENT_ENVIRONMENTat build time.src/lib/env/deploymentEnvironment.ts—clientDeploymentEnvironment(), the browser twin.src/instrumentation-client.ts— the browser's oneSentry.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.mdandsrc/AGENTS.mdcarry a sentence each.tests/unit/client-deployment-environment.spec.ts— pins the config's chain against the helper's.Acceptance criteria
pr-<n>environment:"pr-763"beside the DSNproductiondevelopmentgrep -rn "environment: process.env.NODE_ENV" src/returns nothingdocs/architecture.mdwarning removedCriterion 3 is the one open question. #771 gates the browser
Sentry.initonNODE_ENV === 'production', because.envis git-tracked with a real DSN. So a local run now sends no browser event at all, rather than one taggeddevelopment. That is a deliberate decision upstream, and it satisfies the intent of #737 — nothing local is mislabelledproduction. It does not satisfy the criterion as written. Your call whether that closes #737.Test results
typecheck,typecheck:tests,test:unit(1719 passed) — re-run after themainmergeRAILWAY_ENVIRONMENT_NAME=pr-763 pnpm buildManual verification
environment: pr-763, notproduction.curl <preview>/api/healthreturns"environment": "pr-763".Notes for reviewer
What the build proves now, and what it could not before.
RAILWAY_ENVIRONMENT_NAME=pr-763 pnpm buildemits one client chunk containingdsn:"https://b198…",environment:"pr-763",tracesSampleRate:0. The DSN is the new half. Before #771,clientEnvparsed a bareprocess.env— an empty object in the browser — so the DSN reached no client chunk andSentry.initnever ran. The tag was correct and invisible. That is why #737 waited on #760.Merging
mainshrank this PR. #771 deletedErrorBoundary's duplicateSentry.init, so the two client call sites became one.ErrorBoundary.tsxis no longer in this diff — it captures through the clientinstrumentation-client.tsinstalls, 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 aprocess.env.RAILWAY_ENVIRONMENT_NAMEread 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.envkeys asprocess.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
.mjsconfig cannot import the TypeScript helper, soRAILWAY_ENVIRONMENT_NAME ?? RAILWAY_ENVIRONMENT ?? NODE_ENVis written twice.tests/unit/client-deployment-environment.spec.tsimports the real config and asserts both agree, including the legacy variable and an unsetNODE_ENV. That last case exists because adevelopmentvalue passes either way — only an unsetNODE_ENVseparates 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_ENVIRONMENTis absent fromClientEnvSchemaand.env.example. Both are for variables a person sets; this one is written by the build. Next substitutes literalprocess.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.mdclaimed neither browser site reports anything, and namedErrorBoundaryas a call site.docs/environment.mdtwice explained the key in terms ofclientEnv, which #771 deleted. Both now state the literal-member-expression rule instead.Review findings dismissed, with reasons.
next.config.mjstonext.config.tsand 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.?? process.env.NODE_ENVas 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. Theenvblock publishes one non-secret string, and/api/healthalready answered unauthenticated withNODE_ENV.Closes #737
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01QSi3rvDs4nANUBKcccRUME