fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852
fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852cursor[bot] wants to merge 2 commits intomainfrom
Conversation
|
…ocess.env (#820) `sentry init` was not detecting DSNs supplied via framework-specific environment variables like `NEXT_PUBLIC_SENTRY_DSN`, `REACT_APP_SENTRY_DSN`, `VITE_SENTRY_DSN`, etc. This caused init to create a new Sentry project instead of reusing the existing one when the DSN was provided through a framework-prefixed env var. The .env file scanner (env-file.ts) only matched the exact pattern `SENTRY_DSN=...`. The runtime env detection (env.ts) only checked `process.env.SENTRY_DSN`. Fix: - Expanded the .env file regex from `^SENTRY_DSN` to `^(?:\w+_)?SENTRY_DSN` to match framework-prefixed variants (NEXT_PUBLIC_, REACT_APP_, VITE_, etc.) - Added explicit checks for the 5 most common framework-prefixed env vars in the runtime env detection, checked after the canonical SENTRY_DSN Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
f9e2abb to
788ce91
Compare
Codecov Results 📊✅ 6530 passed | Total: 6530 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ✅ Patch coverage is 90.48%. Project has 13874 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 76.02% 75.31% -0.71%
==========================================
Files 296 296 —
Lines 55640 56184 +544
Branches 0 0 —
==========================================
+ Hits 42298 42310 +12
- Misses 13342 13874 +532
- Partials 0 0 —Generated by Codecov Action |
Address Bugbot review: - Invalid DSN in SENTRY_DSN no longer causes premature return; the loop continues to check framework-prefixed vars. - The actual env var name (e.g. NEXT_PUBLIC_SENTRY_DSN) is passed as sourcePath so getDsnSourceDescription reports the correct variable.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 30b4cc3. Configure here.
| * Also handles trailing comments: VAR=value # comment | ||
| */ | ||
| const ENV_DSN_PATTERN = /^SENTRY_DSN\s*=\s*(['"]?)(.+?)\1\s*(?:#.*)?$/; | ||
| const ENV_DSN_PATTERN = /^(?:\w+_)?SENTRY_DSN\s*=\s*(['"]?)(.+?)\1\s*(?:#.*)?$/; |
There was a problem hiding this comment.
Inconsistent canonical SENTRY_DSN priority between detection methods
Low Severity
detectFromEnv in env.ts explicitly prioritizes the canonical SENTRY_DSN over framework-prefixed variants by checking it first in allVars. However, extractDsnFromEnvContent in env-file.ts returns whichever DSN variable appears first by line position in the file. If a .env file lists NEXT_PUBLIC_SENTRY_DSN before SENTRY_DSN with different values, the file scanner picks the framework-prefixed one while the runtime env scanner would pick the canonical one, leading to inconsistent detection results across code paths.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 30b4cc3. Configure here.


Summary
DSN detection only matched
SENTRY_DSN=...in .env files andprocess.env.SENTRY_DSNat runtime. Framework-prefixed variants likeNEXT_PUBLIC_SENTRY_DSN,REACT_APP_SENTRY_DSN,VITE_SENTRY_DSNwere not detected, causingsentry initto create a new project instead of reusing the existing one.Trigger: Having a DSN via
NEXT_PUBLIC_SENTRY_DSNin.env.localand runningsentry init.Changes
env-file.ts: Expanded the .env file regex from^SENTRY_DSNto^(?:\w+_)?SENTRY_DSNto match any framework-prefixed variant (NEXT_PUBLIC_, REACT_APP_, VITE_, etc.)env.ts: Added explicit runtime checks for the 5 most common framework-prefixed env vars after the canonicalSENTRY_DSNFixes #820