Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | 160x 160x 160x 800x 160x 338x 338x 350x 350x 40x 310x 310x 277x 61x 160x 160x 45x 45x 12x 12x 11x 45x 45x 45x 26x 26x 26x 26x 11x 11x 11x 26x 26x 26x 26x 26x 26x 26x 26x 26x 32x 32x 32x 32x 320x 320x 320x 11x 1x 10x 10x 10x 10x 10x 9x 9x 32x 10x 9x 9x 9x 9x 10x 10x 9x 10x 160x | /**
* Environment File Detection
*
* Detects DSN from .env files in the project directory.
* Supports various .env file variants in priority order.
*
* For monorepos, also scans common package directories (packages/, apps/, etc.)
* to find DSNs in individual packages/apps.
*/
import { opendir } from "node:fs/promises";
import { join } from "node:path";
import { withTracingSpan } from "../telemetry.js";
import { FRAMEWORK_ENV_PREFIXES } from "./env.js";
import { createDetectedDsn } from "./parser.js";
import { scanSpecificFiles } from "./scanner.js";
import type { DetectedDsn } from "./types.js";
import { MONOREPO_ROOTS } from "./types.js";
/**
* Result of scanning env files, including mtimes for caching.
*/
export type EnvFileScanResult = {
/** All detected DSNs from env files */
dsns: DetectedDsn[];
/** Map of source file paths to their mtimes (only files containing DSNs) */
sourceMtimes: Record<string, number>;
/** Number of package directories scanned for env files (monorepo scan only) */
packagesScanned?: number;
};
/**
* .env file names to search (in priority order).
*
* More specific files (.env.local, .env.development.local) are checked first
* as they typically contain environment-specific overrides.
*/
export const ENV_FILES = [
".env.local",
".env.development.local",
".env.production.local",
".env",
".env.development",
".env.production",
] as const;
/**
* Pattern to match Sentry DSN variables in .env files.
* Matches the canonical `SENTRY_DSN` plus framework-prefixed variants
* from the shared {@link FRAMEWORK_ENV_PREFIXES} allowlist:
* NEXT_PUBLIC_SENTRY_DSN, REACT_APP_SENTRY_DSN, VITE_SENTRY_DSN,
* EXPO_PUBLIC_SENTRY_DSN, NUXT_PUBLIC_SENTRY_DSN.
*
* Handles: VAR=value, VAR="value", VAR='value'
* Also handles trailing comments: VAR=value # comment
*/
const TRAILING_UNDERSCORE = /_$/;
const ENV_DSN_PREFIXES = FRAMEWORK_ENV_PREFIXES.map((p) =>
p.replace(TRAILING_UNDERSCORE, "")
).join("|");
const ENV_DSN_PATTERN = new RegExp(
`^(?:(?:${ENV_DSN_PREFIXES})_)?SENTRY_DSN\\s*=\\s*(['"]?)(.+?)\\1\\s*(?:#.*)?$`
);
/**
* Extract SENTRY_DSN value from .env file content.
*
* Parses the content line by line, skipping comments and empty lines.
* Handles quoted values and trailing comments.
*
* @param content - Raw file content
* @returns DSN string or null if not found
*
* @example
* ```typescript
* extractDsnFromEnvContent('SENTRY_DSN="https://key@sentry.io/123"')
* // Returns: "https://key@sentry.io/123"
* ```
*/
export function extractDsnFromEnvContent(content: string): string | null {
const lines = content.split("\n");
for (const line of lines) {
const trimmed = line.trim();
// Skip comments and empty lines
if (!trimmed || trimmed.startsWith("#")) {
continue;
}
// Match SENTRY_DSN=value or SENTRY_DSN="value" or SENTRY_DSN='value'
const match = trimmed.match(ENV_DSN_PATTERN);
if (match?.[2]) {
return match[2];
}
}
return null;
}
// Legacy alias for backwards compatibility
export const parseEnvFile = extractDsnFromEnvContent;
export const extractDsnFromEnvFile = extractDsnFromEnvContent;
/**
* Detect DSN from .env files in the given directory.
*
* Searches files in priority order and returns the first valid DSN found.
* Does NOT scan monorepo subdirectories - use detectFromAllEnvFiles for that.
*
* @param cwd - Directory to search in
* @returns First detected DSN or null if not found
*/
export async function detectFromEnvFiles(
cwd: string
): Promise<DetectedDsn | null> {
return await withTracingSpan(
"detectFromEnvFiles",
"dsn.detect.env",
async (span) => {
const { dsns } = await scanSpecificFiles(cwd, [...ENV_FILES], {
stopOnFirst: true,
processFile: (_relativePath, content) => {
const dsn = extractDsnFromEnvContent(content);
return dsn ? { dsn } : null;
},
createDsn: (raw, relativePath) =>
createDetectedDsn(raw, "env_file", relativePath),
});
span.setAttribute("dsn.env_files_checked", ENV_FILES.length);
span.setAttribute("dsn.env_dsn_found", dsns.length > 0);
return dsns[0] ?? null;
}
);
}
/**
* Detect DSN from ALL .env files including monorepo packages.
*
* Searches:
* 1. Root .env files (.env.local, .env, etc.)
* 2. Monorepo package directories (packages/star/.env, apps/star/.env, etc.)
*
* @param cwd - Directory to search in
* @returns Object with all detected DSNs and source file mtimes
*/
export async function detectFromAllEnvFiles(
cwd: string
): Promise<EnvFileScanResult> {
return await withTracingSpan(
"detectFromAllEnvFiles",
"dsn.detect.env",
async (span) => {
const allDsns: DetectedDsn[] = [];
const allMtimes: Record<string, number> = {};
// 1. Check root .env files (all of them, not just first)
const { dsns: rootDsns, sourceMtimes: rootMtimes } =
await scanSpecificFiles(cwd, [...ENV_FILES], {
stopOnFirst: false,
processFile: (_relativePath, content) => {
const dsn = extractDsnFromEnvContent(content);
return dsn ? { dsn } : null;
},
createDsn: (raw, relativePath) =>
createDetectedDsn(raw, "env_file", relativePath),
});
allDsns.push(...rootDsns);
Object.assign(allMtimes, rootMtimes);
// 2. Check monorepo package directories
const {
dsns: monorepoDsns,
sourceMtimes: monorepoMtimes,
packagesScanned = 0,
} = await detectFromMonorepoEnvFiles(cwd);
allDsns.push(...monorepoDsns);
Object.assign(allMtimes, monorepoMtimes);
// Root checks ENV_FILES.length names; each monorepo package also checks ENV_FILES.length
const filesChecked = ENV_FILES.length * (1 + packagesScanned);
span.setAttribute("dsn.env_files_checked", filesChecked);
span.setAttribute("dsn.env_dsn_found", allDsns.length > 0);
return { dsns: allDsns, sourceMtimes: allMtimes };
}
);
}
/**
* Detect DSN from .env files in monorepo package directories.
*
* Scans common monorepo patterns like packages/*, apps/*, etc.
* for .env files containing SENTRY_DSN.
*
* @param cwd - Root directory to search from
* @returns Object with detected DSNs (with packagePath set) and source file mtimes
*/
export async function detectFromMonorepoEnvFiles(
cwd: string
): Promise<EnvFileScanResult> {
const dsns: DetectedDsn[] = [];
const sourceMtimes: Record<string, number> = {};
let packagesScanned = 0;
for (const monorepoRoot of MONOREPO_ROOTS) {
const rootDir = join(cwd, monorepoRoot);
// Bun's opendir() may not throw on a missing directory — the error
// surfaces when iterating. Wrap the full open+iterate in one try/catch.
// No explicit handle.close() needed: for-await-of auto-closes the Dir
// handle when the loop exits (including early return or break).
try {
for await (const entry of await opendir(rootDir)) {
// Skip hidden dirs (.git, .cache) and non-directories. Accept
// symlinks too — pnpm/Yarn workspaces can symlink packages, and
// detectDsnInPackage gracefully handles non-directory targets.
if (
entry.name.startsWith(".") ||
!(entry.isDirectory() || entry.isSymbolicLink())
) {
continue;
}
packagesScanned += 1;
const pkgDir = join(rootDir, entry.name);
const packagePath = `${monorepoRoot}/${entry.name}`;
const result = await detectDsnInPackage(pkgDir, packagePath);
if (result.dsn) {
dsns.push(result.dsn);
Object.assign(sourceMtimes, result.sourceMtimes);
}
}
} catch {
// Directory doesn't exist or scan failed, skip this monorepo root
}
}
return { dsns, sourceMtimes, packagesScanned };
}
/** Result of detecting DSN in a single package */
type PackageDsnResult = {
dsn: DetectedDsn | null;
sourceMtimes: Record<string, number>;
};
/**
* Detect DSN from .env files in a specific package directory.
*
* @param pkgDir - Full path to the package directory
* @param packagePath - Relative package path (e.g., "packages/frontend")
* @returns Object with detected DSN (or null) and source file mtimes
*/
async function detectDsnInPackage(
pkgDir: string,
packagePath: string
): Promise<PackageDsnResult> {
const { dsns, sourceMtimes: rawMtimes } = await scanSpecificFiles(
pkgDir,
[...ENV_FILES],
{
stopOnFirst: true,
processFile: (_relativePath, content) => {
const dsn = extractDsnFromEnvContent(content);
return dsn ? { dsn, metadata: { packagePath } } : null;
},
createDsn: (raw, relativePath, metadata) => {
const sourcePath = `${packagePath}/${relativePath}`;
return createDetectedDsn(
raw,
"env_file",
sourcePath,
metadata?.packagePath
);
},
}
);
// Prefix mtimes with package path for uniqueness
const sourceMtimes: Record<string, number> = {};
for (const [file, mtime] of Object.entries(rawMtimes)) {
sourceMtimes[`${packagePath}/${file}`] = mtime;
}
return { dsn: dsns[0] ?? null, sourceMtimes };
}
// Legacy export name for backwards compatibility with detector.ts
export const detectEnvFilesInMonorepo = detectFromMonorepoEnvFiles;
|