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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | 159x 159x 159x 159x 159x 159x 159x 159x 8427x 8427x 80x 8297x 8464x 110x 770x 110x 22214x 2x 108x 126x 126x 126x 123x 2x 2x 125x 59x 66x 3x 63x 1x 62x 126x 126x 125x 125x 121x 121x 10x 110x 122x 122x 119x 119x 664x 664x 664x 643x 20x 20x 20x 18x 100x 95x 95x 119x 118x 31x 2x 29x 1x 28x 18x 43x 43x 61x 61x 94x 94x 94x 94x 94x 119x 119x 118x 18x 18x 100x 43x 43x 57x 2x 57x 2x 57x 31x 31x 31x 94x 94x 94x 94x 92x 92x | /**
* Project Root Detection
*
* Walks up from starting directory to find project root, optionally
* detecting DSN along the way for early exit.
*
* Priority:
* 1. .env with SENTRY_DSN → immediate return
* 2. VCS/CI markers → definitive repo root (stop walking)
* 3. Language markers → closest to cwd wins
* 4. Build system markers → last resort
*
* Stops at: home directory or filesystem root
*/
import { opendir, readFile, stat } from "node:fs/promises";
import { homedir } from "node:os";
import { join, resolve } from "node:path";
import pLimit from "p-limit";
import picomatch from "picomatch";
import { anyTrue } from "../promises.js";
import {
applyGlobalFallbacks,
applySentryCliRcDir,
createSentryCliRcConfig,
CONFIG_FILENAME as SENTRYCLIRC_FILENAME,
type SentryCliRcConfig,
setSentryCliRcCache,
} from "../sentryclirc.js";
import { withFsSpan, withTracingSpan } from "../telemetry.js";
import { walkUpFrom } from "../walk-up.js";
import { ENV_FILES, extractDsnFromEnvContent } from "./env-file.js";
import { handleFileError, isRegularFile } from "./fs-utils.js";
import { createDetectedDsn } from "./parser.js";
import type { DetectedDsn } from "./types.js";
/** Why a directory was chosen as project root */
export type ProjectRootReason =
| "env_dsn" // Found .env with SENTRY_DSN
| "vcs" // Version control (.git, .hg, etc.)
| "ci" // CI/CD markers (.github, etc.)
| "editorconfig" // .editorconfig with root=true
| "language" // Language/package marker
| "build_system" // Build system marker
| "fallback"; // No markers found, using cwd
/** Result of project root detection */
export type ProjectRootResult = {
/** The determined project root directory */
projectRoot: string;
/** DSN found in .env while walking up (early exit) */
foundDsn?: DetectedDsn;
/** Why this directory was chosen as root */
reason: ProjectRootReason;
/** Number of directories traversed to find root */
levelsTraversed: number;
};
/** VCS directories - definitive repo root */
const VCS_MARKERS = [
".git",
".hg",
".svn",
".bzr",
"_darcs",
".fossil",
".pijul",
] as const;
/** CI/CD markers - definitive repo root */
const CI_MARKERS = [
".github",
".gitlab-ci.yml",
".circleci",
"Jenkinsfile",
".travis.yml",
"azure-pipelines.yml",
".buildkite",
"bitbucket-pipelines.yml",
".drone.yml",
".woodpecker.yml",
".forgejo",
".gitea",
] as const;
/** Language/package markers - strong project boundary */
const LANGUAGE_MARKERS = [
// Sentry CLI config — treated as a project boundary (not definitive root,
// so the walk continues past it to find VCS markers in monorepos)
SENTRYCLIRC_FILENAME,
// JavaScript/Node ecosystem
"package.json",
"deno.json",
"deno.jsonc",
// Python
"pyproject.toml",
"setup.py",
"setup.cfg",
"Pipfile",
// Go
"go.mod",
"go.work",
// Rust
"Cargo.toml",
// Ruby
"Gemfile",
// PHP
"composer.json",
// Java/JVM
"pom.xml",
"build.gradle",
"build.gradle.kts",
"settings.gradle",
"settings.gradle.kts",
"build.sbt",
// .NET
"global.json",
// Swift
"Package.swift",
// Elixir
"mix.exs",
// Dart/Flutter
"pubspec.yaml",
// Haskell
"stack.yaml",
"cabal.project",
// OCaml
"dune-project",
// Zig
"build.zig",
"build.zig.zon",
// Clojure
"project.clj",
"deps.edn",
] as const;
/** Glob patterns for language markers that need wildcard matching */
const LANGUAGE_MARKER_GLOBS = [
"*.sln",
"*.csproj",
"*.fsproj",
"*.vbproj",
"*.cabal",
"*.opam",
"*.nimble",
] as const;
/** Build system markers - last resort */
const BUILD_SYSTEM_MARKERS = [
"Makefile",
"GNUmakefile",
"makefile",
"CMakeLists.txt",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
"MODULE.bazel",
"meson.build",
"Justfile",
"justfile",
"Taskfile.yml",
"Taskfile.yaml",
"SConstruct",
"xmake.lua",
"premake5.lua",
"premake4.lua",
"wscript",
"Earthfile",
] as const;
/**
* Regex for detecting root=true in .editorconfig.
* Leading whitespace is valid per EditorConfig spec (allows indentation).
*/
const EDITORCONFIG_ROOT_REGEX = /^\s*root\s*=\s*true\s*$/im;
/**
* Maximum concurrent stat() calls across all project-root detection work.
*
* Shared across every anyExists() call so the budget bounds total FD
* pressure, not per-group pressure. processDirectoryLevel fires 4
* parallel anyExists() groups (VCS: 7, CI: 12, language: 30, build: 19 —
* up to 68 stat() calls combined) and this limiter caps their combined
* concurrency.
*
* macOS kqueue has a ~256 FD limit per process; exceeding it returns
* EINVAL (CLI-19A). 32 leaves generous headroom while keeping the
* common-case marker checks (VCS: 7, CI: 12) fully parallel. Matches
* the shared pLimit pattern in response-cache.ts.
*/
export const STAT_CONCURRENCY = 32;
const statLimit = pLimit(STAT_CONCURRENCY);
/**
* Check if a path exists (file or directory) using stat.
*/
async function pathExists(filePath: string): Promise<boolean> {
try {
await stat(filePath);
return true;
} catch {
return false;
}
}
/**
* Check if any of the given paths exist in a directory.
* Runs checks in parallel and resolves as soon as any returns true.
*
* The shared statLimit limiter caps total concurrent stat() calls across
* all marker groups (VCS, CI, language, build) to avoid exhausting the
* OS file descriptor table (kqueue on macOS, epoll on Linux).
*
* @param dir - Directory to check
* @param names - Array of file/directory names to check
* @returns True if any path exists
*/
function anyExists(dir: string, names: readonly string[]): Promise<boolean> {
return anyTrue(names, (name) => statLimit(() => pathExists(join(dir, name))));
}
/**
* Check if any files matching glob patterns exist in a directory.
* Uses `opendir` to lazily stream directory entries and exits on first match
* without reading the entire directory. Matches via synchronous
* `picomatch` (no async I/O, event-loop safe).
*
* @param dir - Directory to check
* @param patterns - Glob patterns to match
* @returns True if any matching file exists
*/
async function anyGlobMatches(
dir: string,
patterns: readonly string[]
): Promise<boolean> {
// 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 {
// Pre-compile matchers outside the loop to avoid recompiling per entry.
const matchers = patterns.map((p) => picomatch(p, { dot: true }));
for await (const entry of await opendir(dir)) {
if (entry.isFile() && matchers.some((m) => m(entry.name))) {
return true;
}
}
return false;
} catch {
return false;
}
}
/**
* Check if .editorconfig exists and contains root=true.
* Reads file directly and handles ENOENT gracefully.
*
* @param dir - Directory to check
* @returns True if .editorconfig with root=true found
*/
async function checkEditorConfigRoot(dir: string): Promise<boolean> {
const editorConfigPath = join(dir, ".editorconfig");
try {
// Guard: skip non-regular files (FIFOs, sockets, etc.) that would block
if (
!(await isRegularFile(editorConfigPath, "checkEditorConfigRoot.stat"))
) {
return false;
}
const content = await readFile(editorConfigPath, "utf-8");
return EDITORCONFIG_ROOT_REGEX.test(content);
} catch (error) {
handleFileError(error, {
operation: "checkEditorConfigRoot",
path: editorConfigPath,
});
return false;
}
}
/**
* Determine the type of repo root marker found.
* Returns in priority order: VCS > CI > editorconfig.
*
* Note: This only handles "definitive" repo root markers (VCS, CI, editorconfig).
* Language and build markers are handled separately in walkUpDirectories() because
* they indicate project boundaries but don't definitively mark the repository root.
*/
function getRepoRootType(
hasVcs: boolean,
hasCi: boolean,
hasEditorConfigRoot: boolean
): "vcs" | "ci" | "editorconfig" | undefined {
if (hasVcs) {
return "vcs";
}
if (hasCi) {
return "ci";
}
if (hasEditorConfigRoot) {
return "editorconfig";
}
return;
}
/**
* Check if directory has VCS or CI/CD markers (definitive repo root)
*
* @param dir - Directory to check
* @returns Object with found status and marker type
*/
export function hasRepoRootMarker(
dir: string
): Promise<{ found: boolean; type?: "vcs" | "ci" | "editorconfig" }> {
return withFsSpan("hasRepoRootMarker", async () => {
// Check all marker types in parallel
const [hasVcs, hasCi, hasEditorConfigRoot] = await Promise.all([
anyExists(dir, VCS_MARKERS),
anyExists(dir, CI_MARKERS),
checkEditorConfigRoot(dir),
]);
const type = getRepoRootType(hasVcs, hasCi, hasEditorConfigRoot);
return type ? { found: true, type } : { found: false };
});
}
/**
* Check if directory has language/package markers
*
* @param dir - Directory to check
* @returns True if any language marker found
*/
export function hasLanguageMarker(dir: string): Promise<boolean> {
return withFsSpan("hasLanguageMarker", async () => {
// Check exact filenames first (more common), then glob patterns
if (await anyExists(dir, LANGUAGE_MARKERS)) {
return true;
}
return anyGlobMatches(dir, LANGUAGE_MARKER_GLOBS);
});
}
/**
* Check if directory has build system markers
*
* @param dir - Directory to check
* @returns True if any build system marker found
*/
export function hasBuildSystemMarker(dir: string): Promise<boolean> {
return withFsSpan("hasBuildSystemMarker", async () =>
anyExists(dir, BUILD_SYSTEM_MARKERS)
);
}
/**
* Check .env files in a directory for SENTRY_DSN.
* Reads files directly and handles ENOENT gracefully.
*
* @param dir - Directory to check
* @returns Detected DSN or null
*/
function checkEnvForDsn(dir: string): Promise<DetectedDsn | null> {
return withFsSpan("checkEnvForDsn", async () => {
// Check env files in priority order, stop on first DSN found
for (const filename of ENV_FILES) {
const filePath = join(dir, filename);
try {
// Guard: skip non-regular files (FIFOs, sockets, etc.) that would block.
// 1Password streams secrets via symlinked named pipes; Bun.file().text()
// blocks indefinitely on these.
if (!(await isRegularFile(filePath, "checkEnvForDsn.stat"))) {
continue;
}
const content = await readFile(filePath, "utf-8");
const dsn = extractDsnFromEnvContent(content);
if (dsn) {
return createDetectedDsn(dsn, "env_file", filename);
}
} catch (error) {
handleFileError(error, { operation: "checkEnvForDsn", path: filePath });
}
}
return null;
});
}
/**
* Get the stop boundary for project root search.
* Returns home directory if it exists, otherwise filesystem root.
*/
export function getStopBoundary(): string {
try {
return homedir();
} catch {
return "/";
}
}
/**
* Process one directory level during the walk-up search
*/
async function processDirectoryLevel(
currentDir: string,
languageMarkerAt: string | null,
buildSystemAt: string | null
): Promise<{
dsnResult: DetectedDsn | null;
repoRootResult: { found: boolean; type?: "vcs" | "ci" | "editorconfig" };
hasLang: boolean;
hasBuild: boolean;
}> {
// Run all checks for this level in parallel
const [dsnResult, repoRootResult, hasLang, hasBuild] = await Promise.all([
checkEnvForDsn(currentDir),
hasRepoRootMarker(currentDir),
languageMarkerAt ? Promise.resolve(false) : hasLanguageMarker(currentDir),
buildSystemAt ? Promise.resolve(false) : hasBuildSystemMarker(currentDir),
]);
return { dsnResult, repoRootResult, hasLang, hasBuild };
}
/**
* Determine the final project root from candidates
*/
function selectProjectRoot(
languageMarkerAt: string | null,
buildSystemAt: string | null,
fallback: string
): { projectRoot: string; reason: ProjectRootReason } {
if (languageMarkerAt) {
return { projectRoot: languageMarkerAt, reason: "language" };
}
if (buildSystemAt) {
return { projectRoot: buildSystemAt, reason: "build_system" };
}
return { projectRoot: fallback, reason: "fallback" };
}
/**
* Create result when DSN is found in .env file
*/
function createDsnFoundResult(
currentDir: string,
dsnResult: DetectedDsn,
levelsTraversed: number
): ProjectRootResult {
return {
projectRoot: currentDir,
foundDsn: dsnResult,
reason: "env_dsn",
levelsTraversed,
};
}
/**
* Create result when repo root marker is found.
* Maps marker type to reason (defaults to "vcs" for undefined).
*
* If a language marker was already found closer to cwd, prefer it over the
* repo root to support monorepos (e.g., user working in packages/frontend
* should use that as root, not the repo root).
*/
function createRepoRootResult(
currentDir: string,
markerType: "vcs" | "ci" | "editorconfig" | undefined,
levelsTraversed: number,
languageMarkerAt: string | null
): ProjectRootResult {
// Prefer closer language marker over repo root for monorepo support
Iif (languageMarkerAt) {
return {
projectRoot: languageMarkerAt,
reason: "language",
levelsTraversed,
};
}
return {
projectRoot: currentDir,
reason: markerType ?? "vcs",
levelsTraversed,
};
}
/**
* Finalize accumulated sentryclirc config: apply global fallbacks and cache.
*/
async function finalizeSentryCliRc(
cwd: string,
config: SentryCliRcConfig
): Promise<void> {
await applyGlobalFallbacks(config);
setSentryCliRcCache(cwd, config);
}
/**
* Walk up directories searching for project root.
*
* Uses the shared {@link walkUpFrom} generator for directory traversal
* (with symlink cycle detection). Also reads `.sentryclirc` files at each
* level and populates the sentryclirc cache (with global fallbacks applied),
* so that a later `loadSentryCliRc` call for the same `cwd` is a cache hit
* instead of a second walk.
*
* Stops at the `stopBoundary` (home dir) after processing it, or when the
* generator reaches the filesystem root.
*/
async function walkUpDirectories(
resolvedStart: string,
stopBoundary: string
): Promise<ProjectRootResult> {
let levelsTraversed = 0;
let languageMarkerAt: string | null = null;
let buildSystemAt: string | null = null;
const rcConfig = createSentryCliRcConfig();
for await (const currentDir of walkUpFrom(resolvedStart)) {
levelsTraversed += 1;
// Check project-root markers AND .sentryclirc in parallel
const [{ dsnResult, repoRootResult, hasLang, hasBuild }] =
await Promise.all([
processDirectoryLevel(currentDir, languageMarkerAt, buildSystemAt),
applySentryCliRcDir(rcConfig, currentDir),
]);
// 1. Check for DSN in .env files - immediate return (unless at/above home directory)
// Don't use a .env in the home directory as a project root indicator,
// as users may have global configs that shouldn't define project boundaries
if (dsnResult && currentDir !== stopBoundary) {
await finalizeSentryCliRc(resolvedStart, rcConfig);
return createDsnFoundResult(currentDir, dsnResult, levelsTraversed);
}
// 2. Check for VCS/CI markers - definitive root, stop walking
if (repoRootResult.found) {
await finalizeSentryCliRc(resolvedStart, rcConfig);
return createRepoRootResult(
currentDir,
repoRootResult.type,
levelsTraversed,
languageMarkerAt
);
}
// 3. Remember language marker (closest to cwd wins)
if (!languageMarkerAt && hasLang) {
languageMarkerAt = currentDir;
}
// 4. Remember build system marker (last resort)
if (!buildSystemAt && hasBuild) {
buildSystemAt = currentDir;
}
// Stop at boundary after processing it (e.g., home dir)
Iif (currentDir === stopBoundary) {
break;
}
}
// Populate sentryclirc cache from accumulated data
setSentryCliRcCache(resolvedStart, rcConfig);
// Determine project root from candidates (priority order)
const selected = selectProjectRoot(
languageMarkerAt,
buildSystemAt,
resolvedStart
);
return {
projectRoot: selected.projectRoot,
reason: selected.reason,
levelsTraversed,
};
}
/**
* Find project root by walking up from starting directory.
*
* Checks for DSN in .env files at each level (early exit if found).
* Stops at VCS/CI markers (definitive repo root).
* Falls back to language markers, then build system markers.
*
* @param startDir - Directory to start searching from
* @returns Project root result with optional DSN
*/
export function findProjectRoot(startDir: string): Promise<ProjectRootResult> {
return withTracingSpan(
"findProjectRoot",
"dsn.detect",
async (span) => {
const resolvedStart = resolve(startDir);
const stopBoundary = getStopBoundary();
const result = await walkUpDirectories(resolvedStart, stopBoundary);
span.setAttributes({
"dsn.found": result.foundDsn !== undefined,
"dsn.reason": result.reason,
"dsn.levels_traversed": result.levelsTraversed,
"dsn.project_root": result.projectRoot,
});
return result;
},
{ "dsn.start_dir": startDir }
);
}
|