refactor: Leverage Python project root detection for jest-runner resolution

Instead of duplicating monorepo detection logic in JavaScript, leverage
the existing Python _find_node_project_root and add _find_monorepo_root
functions. Pass the detected monorepo root via CODEFLASH_MONOREPO_ROOT
environment variable to the loop-runner.

This approach:
- Reuses existing Python project detection logic
- Provides a reliable monorepo root hint to JavaScript
- Maintains fallback directory traversal for edge cases

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
mohammedahmed18 2026-02-03 14:26:41 +00:00
parent 8223796576
commit 7f954d1544
2 changed files with 48 additions and 2 deletions

View file

@ -296,6 +296,33 @@ def _find_node_project_root(file_path: Path) -> Path | None:
return None
def _find_monorepo_root(start_path: Path) -> Path | None:
"""Find the monorepo workspace root by looking for workspace markers.
Traverses up from the given path to find a directory containing
monorepo workspace markers like yarn.lock, pnpm-workspace.yaml, etc.
Args:
start_path: A path within the monorepo.
Returns:
The monorepo root directory, or None if not found.
"""
monorepo_markers = ["yarn.lock", "pnpm-workspace.yaml", "lerna.json", "package-lock.json"]
current = start_path if start_path.is_dir() else start_path.parent
while current != current.parent:
# Check for monorepo markers
if any((current / marker).exists() for marker in monorepo_markers):
# Verify it has node_modules (it's the workspace root)
if (current / "node_modules").exists():
return current
current = current.parent
return None
def _find_jest_config(project_root: Path) -> Path | None:
"""Find Jest configuration file in the project.
@ -797,6 +824,12 @@ def run_jest_benchmarking_tests(
jest_env["JEST_JUNIT_SUITE_NAME"] = "{filepath}"
jest_env["JEST_JUNIT_ADD_FILE_ATTRIBUTE"] = "true"
jest_env["JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT"] = "true"
# Pass monorepo root to loop-runner for jest-runner resolution
monorepo_root = _find_monorepo_root(effective_cwd)
if monorepo_root:
jest_env["CODEFLASH_MONOREPO_ROOT"] = str(monorepo_root)
logger.debug(f"Detected monorepo root: {monorepo_root}")
codeflash_sqlite_file = get_run_tmp_file(Path("test_return_values_0.sqlite"))
jest_env["CODEFLASH_OUTPUT_FILE"] = str(codeflash_sqlite_file)
jest_env["CODEFLASH_TEST_ITERATION"] = "0"

View file

@ -34,7 +34,8 @@ const fs = require('fs');
/**
* Resolve jest-runner with monorepo support.
* Walks up the directory tree looking for node_modules/jest-runner.
* Uses CODEFLASH_MONOREPO_ROOT environment variable if available,
* otherwise walks up the directory tree looking for node_modules/jest-runner.
*/
function resolveJestRunner() {
// Try standard resolution first (works in simple projects)
@ -44,7 +45,19 @@ function resolveJestRunner() {
// Standard resolution failed - try monorepo-aware resolution
}
// Walk up from cwd looking for workspace root markers and node_modules
// If Python detected a monorepo root, check there first
const monorepoRoot = process.env.CODEFLASH_MONOREPO_ROOT;
if (monorepoRoot) {
const jestRunnerPath = path.join(monorepoRoot, 'node_modules', 'jest-runner');
if (fs.existsSync(jestRunnerPath)) {
const packageJsonPath = path.join(jestRunnerPath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
return jestRunnerPath;
}
}
}
// Fallback: Walk up from cwd looking for node_modules/jest-runner
const monorepoMarkers = ['yarn.lock', 'pnpm-workspace.yaml', 'lerna.json', 'package-lock.json'];
let currentDir = process.cwd();
const visitedDirs = new Set();