Fix: Recalculate js_project_root per function in monorepos#1984
Conversation
**Issue:** When optimizing multiple functions in a monorepo with nested package.json files (e.g., extensions/discord/package.json), the js_project_root was set once for the first function and reused for all subsequent functions. This caused vitest to look for setupFiles in the wrong directory. **Root Cause:** test_cfg.js_project_root was set during initial setup and never recalculated. When function #1 was in extensions/discord/, all subsequent functions in src/ inherited this wrong project root. **Fix:** - Added _get_js_project_root() method to FunctionOptimizer - Calculate js_project_root fresh for each function using find_node_project_root() - Updated all test execution paths (behavior, performance, line_profile) **Impact:** - Vitest now runs from the correct working directory for each function - setupFiles can be resolved correctly - Functions in different monorepo packages can be optimized correctly Fixes trace IDs: 12d26b00-cbae-49a8-a3cd-c36024ee06ec, 1cde1c65-ef42-4072-afbc-165b0c235688, and 18 others Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 5m 23s —— View job PR Review SummaryPrek ChecksOne auto-fixable ruff issue was detected ( Code ReviewThe fix is correct and addresses the root cause. A few convention issues were pushed as a follow-up commit: Naming (fixed): Redundant import (fixed): Docstring on new method (fixed): CLAUDE.md says not to add docstrings to new or changed code. Removed.
Tests (fixed)Both test files violated project testing conventions:
The second test file ( Duplicate DetectionNo duplicates detected. Changes pushed: |
- Rename `_get_js_project_root` → `get_js_project_root` (no leading underscores per convention) - Remove redundant `from pathlib import Path` import inside method (already imported at top) - Remove unnecessary docstring from new method - Rewrite tests to use `tmp_path` fixture instead of `tempfile.TemporaryDirectory()` - Add `.resolve()` calls and `encoding="utf-8"` per project conventions - Simplify second test file to focus on the actual caching behavior Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
Issue
When optimizing multiple functions in a monorepo with nested
package.jsonfiles (e.g.,extensions/discord/package.json), thejs_project_rootwas set once for the first function and reused for all subsequent functions. This caused vitest to fail with:Root Cause
test_cfg.js_project_rootwas set during initialsetup_test_config()callextensions/discord/, all subsequent functions insrc/inherited this wrong project rootsetupFiles: ["test/setup.ts"]relative to the wrong directoryFix
_get_js_project_root()method toFunctionOptimizerclassjs_project_rootfresh for each function usingfind_node_project_root()TestingMode.BEHAVIOR)TestingMode.PERFORMANCE)TestingMode.LINE_PROFILE)Testing
uv run prekpasses)Impact
Trace IDs Fixed
Affects ALL optimization runs where first function is in a subdirectory with its own package.json:
12d26b00-cbae-49a8-a3cd-c36024ee06ec1cde1c65-ef42-4072-afbc-165b0c235688🤖 Generated with Claude Code