fix: set tests_project_rootdir to tests_root for Java projects (Bug #2)#1341
Conversation
Bug #2: Test file name mapping returns null for Java tests Root cause: For Java projects, tests_project_rootdir was incorrectly set to the project root instead of the actual tests directory. This caused test file resolution to fail in parse_test_xml when parsing JUnit XML from Maven Surefire, which doesn't include file attributes. JavaScript already had this fix (line 654), but Java was missing it. Fix: Add Java to the language check that sets tests_project_rootdir equal to tests_root, ensuring instrumented test files can be found at src/test/java/com/example/Test__perfinstrumented.java Changes: - Added is_java import to discover_unit_tests.py - Added Java check: if is_java(): cfg.tests_project_rootdir = cfg.tests_root - Added comprehensive test coverage with 2 test cases Tests: - test_java_tests_project_rootdir_set_to_tests_root: verifies fix for Java - test_python_tests_project_rootdir_unchanged: verifies Python unchanged Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
E2E Test ResultsGoal: Fix test file resolution for Java by setting tests_project_rootdir correctly. Result: ✅ PASS What was testedRan E2E optimization on Test commands# Checkout PR branch
cd /home/ubuntu/code/codeflash
git checkout fix/java-tests-project-rootdir
# Run unit tests
pytest tests/test_java_tests_project_rootdir.py -v
# Result: 2/2 tests passed
# Run E2E optimization
cd /home/ubuntu/code/Java
CODEFLASH_AIS_SERVER=local uv run codeflash \
--file src/main/java/com/thealgorithms/maths/Factorial.java \
--function factorial --no-pr -vEvidence1. Unit tests pass: 2. Test discovery succeeded: 3. Test instrumentation succeeded: 4. Instrumented test files written to correct paths: 5. No "could not find test" errors: # Checked for the specific error this PR fixes
grep -i "could not find.*test" /tmp/pr1341-test-local.log
# Result: No output (error does not appear)VerificationThe fix correctly sets Code change verified: if is_java():
cfg.tests_project_rootdir = cfg.tests_rootThis unblocks Java E2E optimizations by fixing test file resolution during instrumentation. Tested with Claude Code |
| from codeflash.languages import is_java, is_javascript, is_python | ||
|
|
||
| # Detect language from functions being optimized | ||
| language = _detect_language_from_functions(file_to_funcs_to_optimize) |
There was a problem hiding this comment.
Maybe we should check when we have language detection, it should automatically select the configuration.
Bug #2: Test File Name Mapping Returns Null for Java
Problem
Java E2E optimizations were failing with "Could not find the test for file name" errors, blocking all optimizations.
Root Cause
resolve_test_file_from_class_path()withbase_dir = tests_project_rootdirtests_project_rootdirwas set to project root (e.g.,/project) instead of tests root (e.g.,/project/src/test/java)Solution
Added Java to the language check that sets
tests_project_rootdir = tests_root:```python
if is_javascript():
cfg.tests_project_rootdir = cfg.tests_root
if is_java(): # NEW
cfg.tests_project_rootdir = cfg.tests_root
```
This ensures instrumented test files can be found at their actual location:
`/project/src/test/java/com/example/Test__perfinstrumented.java`
Code Changes
codeflash/discovery/discover_unit_tests.py:
tests/test_java_tests_project_rootdir.py:
Testing
```bash
pytest tests/test_java_tests_project_rootdir.py -v
✓ 2 passed in 0.03s
```
Impact
🤖 Generated with Claude Sonnet 4.5