diff --git a/codeflash/languages/__init__.py b/codeflash/languages/__init__.py index c54f438bc..e63f19a5a 100644 --- a/codeflash/languages/__init__.py +++ b/codeflash/languages/__init__.py @@ -38,7 +38,6 @@ reset_current_language, set_current_language, ) - from codeflash.languages.registry import ( detect_project_language, get_language_support, diff --git a/codeflash/languages/java/context.py b/codeflash/languages/java/context.py index fb43b4ffc..338ac5102 100644 --- a/codeflash/languages/java/context.py +++ b/codeflash/languages/java/context.py @@ -660,6 +660,10 @@ def find_helper_functions( helper_files = find_helper_files(function.file_path, project_root, max_depth, analyzer) for file_path in helper_files: + # Skip non-existent files early to avoid expensive exception handling + if not file_path.exists(): + continue + try: source = file_path.read_text(encoding="utf-8") file_functions = discover_functions_from_source(source, file_path, analyzer=analyzer) @@ -713,6 +717,10 @@ def _find_same_class_helpers(function: FunctionToOptimize, analyzer: JavaAnalyze if not function.class_name: return helpers + # Check if file exists before trying to read it + if not function.file_path.exists(): + return helpers + try: source = function.file_path.read_text(encoding="utf-8") source_bytes = source.encode("utf8") diff --git a/codeflash/languages/registry.py b/codeflash/languages/registry.py index 637bef7e7..38688cab6 100644 --- a/codeflash/languages/registry.py +++ b/codeflash/languages/registry.py @@ -53,10 +53,10 @@ def _ensure_languages_registered() -> None: from codeflash.languages.python import support as _ with contextlib.suppress(ImportError): - from codeflash.languages.javascript import support as _ # noqa: F401 + from codeflash.languages.javascript import support as _ with contextlib.suppress(ImportError): - from codeflash.languages.java import support as _ # noqa: F401 + from codeflash.languages.java import support as _ _languages_registered = True