Skip to main content
Sentry CLI can automatically detect your project’s DSN by scanning source code, environment files, and environment variables. This enables zero-configuration workflows where the CLI infers the target project from your codebase.

Detection Priority

DSNs are detected in priority order (highest to lowest):
  1. Source code - Explicit DSN in Sentry.init() calls
  2. Environment files - .env, .env.local, .env.production, etc.
  3. Environment variable - SENTRY_DSN
Higher-priority sources override lower-priority ones. For example, a DSN in source code takes precedence over .env files.

Source Code Detection

The CLI scans source files for DSN patterns in Sentry.init() calls:

Supported Languages

JavaScript/TypeScript:
Python:
Go:
Java:
Ruby:
PHP:

Detection Algorithm

The code scanner uses language-specific patterns:
The scanner skips common directories like node_modules/, vendor/, .git/, dist/, and build/ to avoid false positives and improve performance.

Environment File Detection

The CLI searches for DSN in environment files following the priority order:
  1. .env.local
  2. .env.development.local
  3. .env.production.local
  4. .env.test.local
  5. .env
  6. .env.development
  7. .env.production
  8. .env.test

Environment File Format

DSN can be specified in various formats:

Parsing Logic

The environment file parser handles various formats:

Environment Variable Detection

As a fallback, the CLI checks the SENTRY_DSN environment variable:
This has the lowest priority and is checked only if no DSN is found in code or environment files.

Project Root Detection

Before scanning for DSNs, the CLI must determine the project root. It walks up from the current directory looking for markers:

VCS Markers

  • .git/
  • .hg/
  • .svn/

Language Markers

JavaScript/Node.js:
  • package.json
  • tsconfig.json
  • jsconfig.json
Python:
  • pyproject.toml
  • setup.py
  • setup.cfg
  • requirements.txt
  • Pipfile
Go:
  • go.mod
  • go.sum
Java:
  • pom.xml
  • build.gradle
  • build.gradle.kts
Ruby:
  • Gemfile
  • .ruby-version
PHP:
  • composer.json
Rust:
  • Cargo.toml

Detection Flow

Finding a DSN in .env during walk-up stops the walk (determines project root) but does NOT short-circuit detection - the CLI still scans for code DSNs which have higher priority.

Caching Strategy

Detection results are cached in SQLite to avoid repeated scans:

DSN Cache Schema

Cache Validation

Cached entries are validated before use:
Cache entries are invalidated when the source file changes or the DSN value differs.

Monorepo Support

The CLI detects multiple DSNs in monorepo structures:
Use detectAllDsns() to find all DSNs:
Each detected DSN includes its packagePath (e.g., apps/frontend) to track which package it belongs to.

DSN Format

Detected DSNs are parsed into components:

DSN Parsing Example

Some DSN formats (especially self-hosted) may not include an org ID in the host. In these cases, the CLI resolves the project using the DSN public key via the /api/0/projects?query=dsn:<key> endpoint.

Detection Performance

  • Fast path (cache hit): ~5ms - reads single file to verify
  • Slow path (cache miss): ~2-5s - full glob scan
The caching strategy ensures subsequent commands are nearly instant even in large monorepos.