Skip to content

fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852

Open
cursor[bot] wants to merge 2 commits intomainfrom
cursor/sentry-cli-bug-fixes-4540
Open

fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852
cursor[bot] wants to merge 2 commits intomainfrom
cursor/sentry-cli-bug-fixes-4540

Conversation

@cursor
Copy link
Copy Markdown
Contributor

@cursor cursor Bot commented Apr 27, 2026

Summary

DSN detection only matched SENTRY_DSN=... in .env files and process.env.SENTRY_DSN at runtime. Framework-prefixed variants like NEXT_PUBLIC_SENTRY_DSN, REACT_APP_SENTRY_DSN, VITE_SENTRY_DSN were not detected, causing sentry init to create a new project instead of reusing the existing one.

Trigger: Having a DSN via NEXT_PUBLIC_SENTRY_DSN in .env.local and running sentry init.

Changes

  • env-file.ts: Expanded the .env file regex from ^SENTRY_DSN to ^(?:\w+_)?SENTRY_DSN to 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 canonical SENTRY_DSN

Fixes #820

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 27, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-852/

Built to branch gh-pages at 2026-05-01 22:01 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

…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>
@BYK BYK force-pushed the cursor/sentry-cli-bug-fixes-4540 branch from f9e2abb to 788ce91 Compare May 1, 2026 21:46
@BYK BYK changed the title fix: three bug fixes — span list cross-project, init ANSI escapes, DSN env detection fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env May 1, 2026
@BYK BYK marked this pull request as ready for review May 1, 2026 21:47
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

Codecov Results 📊

6530 passed | Total: 6530 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 90.48%. Project has 13874 uncovered lines.
❌ Project coverage is 75.31%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
src/lib/dsn/env.ts 89.47% ⚠️ 2 Missing
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

Comment thread src/lib/dsn/env.ts Outdated
Comment thread src/lib/dsn/env.ts
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.
Copy link
Copy Markdown
Contributor Author

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread src/lib/dsn/env-file.ts
* 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*(?:#.*)?$/;
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.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 30b4cc3. Configure here.

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.

sentry init doesn't detect env vars when deciding whether to reuse a project

2 participants