From 4d773fb29a9d6ec17898d80ea2bd899242a733df Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Thu, 19 Mar 2026 11:41:48 -0700 Subject: [PATCH 1/3] trigger cf --- code_to_optimize/java/src/main/java/com/example/Fibonacci.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code_to_optimize/java/src/main/java/com/example/Fibonacci.java b/code_to_optimize/java/src/main/java/com/example/Fibonacci.java index b604fb928..70733ba4a 100644 --- a/code_to_optimize/java/src/main/java/com/example/Fibonacci.java +++ b/code_to_optimize/java/src/main/java/com/example/Fibonacci.java @@ -35,7 +35,7 @@ public static boolean isFibonacci(long num) { return false; } long check1 = 5 * num * num + 4; - long check2 = 5 * num * num - 4; + long check2 = 5 * num * num - 4; \\ helloworld return isPerfectSquare(check1) || isPerfectSquare(check2); } From 2d2c89b4fa0a2652490351cf7086baec98a520b4 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Thu, 19 Mar 2026 12:15:03 -0700 Subject: [PATCH 2/3] fix: prefer codeflash.toml over pyproject.toml for config loading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_config_file() computed closest_toml_path but never used it for actual config loading — it always fell through to find_pyproject_toml() which hard-prioritizes pyproject.toml. This caused codeflash.toml settings (e.g. Java module-root) to be ignored when pyproject.toml exists in the same or parent directory. Co-Authored-By: Claude Opus 4.6 --- codeflash/code_utils/config_parser.py | 17 +++- tests/code_utils/test_config_parser.py | 115 +++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 tests/code_utils/test_config_parser.py diff --git a/codeflash/code_utils/config_parser.py b/codeflash/code_utils/config_parser.py index ef21ce051..da454a40f 100644 --- a/codeflash/code_utils/config_parser.py +++ b/codeflash/code_utils/config_parser.py @@ -100,9 +100,15 @@ def parse_config_file( # Pick the closest toml config (pyproject.toml or codeflash.toml). # Java projects use codeflash.toml; Python projects use pyproject.toml. + # When both exist at the same depth, prefer codeflash.toml (dedicated config). closest_toml_path = None if pyproject_toml_path and codeflash_toml_path: - closest_toml_path = max(pyproject_toml_path, codeflash_toml_path, key=lambda p: len(p.parent.parts)) + pyproject_depth = len(pyproject_toml_path.parent.parts) + codeflash_depth = len(codeflash_toml_path.parent.parts) + if codeflash_depth >= pyproject_depth: + closest_toml_path = codeflash_toml_path + else: + closest_toml_path = pyproject_toml_path else: closest_toml_path = pyproject_toml_path or codeflash_toml_path @@ -134,8 +140,13 @@ def parse_config_file( ) return config, path - # Fall back to pyproject.toml - config_file_path = find_pyproject_toml(config_file_path) + # Fall back to the closest toml config file (pyproject.toml or codeflash.toml). + # Use closest_toml_path when available to respect codeflash.toml over pyproject.toml + # when codeflash.toml is closer to CWD (e.g., Java projects in a Python repo). + if config_file_path is None and closest_toml_path is not None: + config_file_path = closest_toml_path + else: + config_file_path = find_pyproject_toml(config_file_path) try: with config_file_path.open("rb") as f: data = tomlkit.parse(f.read()) diff --git a/tests/code_utils/test_config_parser.py b/tests/code_utils/test_config_parser.py new file mode 100644 index 000000000..a0db5a881 --- /dev/null +++ b/tests/code_utils/test_config_parser.py @@ -0,0 +1,115 @@ +from __future__ import annotations + +from pathlib import Path + +import pytest + +from codeflash.code_utils.config_parser import ALL_CONFIG_FILES, PYPROJECT_TOML_CACHE, parse_config_file + + +@pytest.fixture(autouse=True) +def clear_caches() -> None: + PYPROJECT_TOML_CACHE.clear() + ALL_CONFIG_FILES.clear() + + +PYPROJECT_TOML_CONTENT = """\ +[tool.codeflash] +module-root = "src/python" +tests-root = "tests" +""" + +CODEFLASH_TOML_CONTENT = """\ +[tool.codeflash] +module-root = "src/main/java" +tests-root = "src/test/java" +""" + + +class TestParseConfigFileTomlPriority: + def test_codeflash_toml_preferred_over_pyproject_when_same_dir(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + (tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8") + (tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "main" / "java").mkdir(parents=True) + (tmp_path / "src" / "test" / "java").mkdir(parents=True) + (tmp_path / "src" / "python").mkdir(parents=True) + (tmp_path / "tests").mkdir(parents=True) + monkeypatch.chdir(tmp_path) + + config, config_path = parse_config_file() + + assert config_path == tmp_path / "codeflash.toml" + assert config["module_root"] == str((tmp_path / "src" / "main" / "java").resolve()) + assert config["tests_root"] == str((tmp_path / "src" / "test" / "java").resolve()) + + def test_only_pyproject_toml_still_works(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + (tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "python").mkdir(parents=True) + (tmp_path / "tests").mkdir(parents=True) + monkeypatch.chdir(tmp_path) + + config, config_path = parse_config_file() + + assert config_path == tmp_path / "pyproject.toml" + assert config["module_root"] == str((tmp_path / "src" / "python").resolve()) + + def test_only_codeflash_toml_still_works(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + (tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "main" / "java").mkdir(parents=True) + (tmp_path / "src" / "test" / "java").mkdir(parents=True) + monkeypatch.chdir(tmp_path) + + config, config_path = parse_config_file() + + assert config_path == tmp_path / "codeflash.toml" + assert config["module_root"] == str((tmp_path / "src" / "main" / "java").resolve()) + + def test_closer_codeflash_toml_preferred_over_parent_pyproject(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + # pyproject.toml in parent, codeflash.toml in child (closer to CWD) + (tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "python").mkdir(parents=True) + (tmp_path / "tests").mkdir(parents=True) + + child = tmp_path / "subproject" + child.mkdir() + (child / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8") + (child / "src" / "main" / "java").mkdir(parents=True) + (child / "src" / "test" / "java").mkdir(parents=True) + monkeypatch.chdir(child) + + config, config_path = parse_config_file() + + assert config_path == child / "codeflash.toml" + assert config["module_root"] == str((child / "src" / "main" / "java").resolve()) + + def test_closer_pyproject_preferred_over_parent_codeflash_toml(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + # codeflash.toml in parent, pyproject.toml in child (closer to CWD) + (tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "main" / "java").mkdir(parents=True) + (tmp_path / "src" / "test" / "java").mkdir(parents=True) + + child = tmp_path / "subproject" + child.mkdir() + (child / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8") + (child / "src" / "python").mkdir(parents=True) + (child / "tests").mkdir(parents=True) + monkeypatch.chdir(child) + + config, config_path = parse_config_file() + + assert config_path == child / "pyproject.toml" + assert config["module_root"] == str((child / "src" / "python").resolve()) + + def test_explicit_config_file_path_bypasses_discovery(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + (tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8") + (tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8") + (tmp_path / "src" / "main" / "java").mkdir(parents=True) + (tmp_path / "src" / "test" / "java").mkdir(parents=True) + (tmp_path / "src" / "python").mkdir(parents=True) + (tmp_path / "tests").mkdir(parents=True) + monkeypatch.chdir(tmp_path) + + config, config_path = parse_config_file(config_file_path=tmp_path / "pyproject.toml") + + assert config_path == tmp_path / "pyproject.toml" + assert config["module_root"] == str((tmp_path / "src" / "python").resolve()) From a016a5a73e0075e307a3b309f534cceeb9bc42e1 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Thu, 19 Mar 2026 12:16:55 -0700 Subject: [PATCH 3/3] undo irrelevant change --- code_to_optimize/java/src/main/java/com/example/Fibonacci.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code_to_optimize/java/src/main/java/com/example/Fibonacci.java b/code_to_optimize/java/src/main/java/com/example/Fibonacci.java index 70733ba4a..b604fb928 100644 --- a/code_to_optimize/java/src/main/java/com/example/Fibonacci.java +++ b/code_to_optimize/java/src/main/java/com/example/Fibonacci.java @@ -35,7 +35,7 @@ public static boolean isFibonacci(long num) { return false; } long check1 = 5 * num * num + 4; - long check2 = 5 * num * num - 4; \\ helloworld + long check2 = 5 * num * num - 4; return isPerfectSquare(check1) || isPerfectSquare(check2); }