Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions codeflash/code_utils/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
f.flush()
tmp_file = Path(f.name)
try:
format_code(formatter_cmds, tmp_file, print_status=False)
format_code(formatter_cmds, tmp_file, print_status=False, exit_on_failure=exit_on_failure)
except Exception:
exit_with_message(
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again.",
error_on_exit=True,
)

return return_code


Expand Down
10 changes: 7 additions & 3 deletions codeflash/code_utils/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def apply_formatter_cmds(
path: Path,
test_dir_str: Optional[str],
print_status: bool, # noqa
exit_on_failure: bool = True, # noqa
) -> tuple[Path, str]:
# TODO: Only allow a particular whitelist of formatters here to prevent arbitrary code execution
formatter_name = cmds[0].lower()
Expand Down Expand Up @@ -84,8 +85,8 @@ def apply_formatter_cmds(
expand=False,
)
console.print(panel)

raise e from None
if exit_on_failure:
raise e from None

return file_path, file_path.read_text(encoding="utf8")

Expand All @@ -106,6 +107,7 @@ def format_code(
optimized_function: str = "",
check_diff: bool = False, # noqa
print_status: bool = True, # noqa
exit_on_failure: bool = True, # noqa
) -> str:
with tempfile.TemporaryDirectory() as test_dir_str:
if isinstance(path, str):
Expand Down Expand Up @@ -138,7 +140,9 @@ def format_code(
)
return original_code
# TODO : We can avoid formatting the whole file again and only formatting the optimized code standalone and replace in formatted file above.
_, formatted_code = apply_formatter_cmds(formatter_cmds, path, test_dir_str=None, print_status=print_status)
_, formatted_code = apply_formatter_cmds(
formatter_cmds, path, test_dir_str=None, print_status=print_status, exit_on_failure=exit_on_failure
)
logger.debug(f"Formatted {path} with commands: {formatter_cmds}")
return formatted_code

Expand Down
Loading