Skip to content

Commit de74e68

Browse files
committed
cleanup check formatter
1 parent 73c8fbd commit de74e68

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import json
44
import os
5+
import shlex
6+
import shutil
57
import tempfile
68
from functools import lru_cache
79
from pathlib import Path
@@ -14,21 +16,44 @@
1416

1517

1618
def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool = True) -> bool: # noqa
17-
return_code = True
18-
if formatter_cmds[0] == "disabled":
19-
return return_code
19+
if not formatter_cmds or formatter_cmds[0] == "disabled":
20+
return True
21+
22+
first_cmd = formatter_cmds[0]
23+
cmd_tokens = shlex.split(first_cmd) if isinstance(first_cmd, str) else [first_cmd]
24+
25+
if not cmd_tokens:
26+
return True
27+
28+
exe_name = cmd_tokens[0]
29+
command_str = " ".join(formatter_cmds)
30+
31+
if shutil.which(exe_name) is None:
32+
logger.error(
33+
f"⚠️ Formatter command not found: {command_str}\n"
34+
f"Please install '{exe_name}' or update the 'formatter-cmds' in your pyproject.toml config."
35+
)
36+
return False
37+
2038
tmp_code = """print("hello world")"""
21-
with tempfile.TemporaryDirectory() as tmpdir:
22-
tmp_file = Path(tmpdir) / "test_codeflash_formatter.py"
23-
tmp_file.write_text(tmp_code, encoding="utf-8")
24-
try:
25-
format_code(formatter_cmds, tmp_file, print_status=False, exit_on_failure=exit_on_failure)
26-
except Exception:
27-
exit_with_message(
28-
"⚠️ 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.",
29-
error_on_exit=True,
30-
)
31-
return return_code
39+
try:
40+
with tempfile.TemporaryDirectory() as tmpdir:
41+
tmp_file = Path(tmpdir) / "test_codeflash_formatter.py"
42+
tmp_file.write_text(tmp_code, encoding="utf-8")
43+
format_code(formatter_cmds, tmp_file, print_status=False, exit_on_failure=False)
44+
return True
45+
except FileNotFoundError:
46+
logger.error(
47+
f"⚠️ Formatter command not found: {command_str}\n"
48+
f"Please install '{exe_name}' or update the 'formatter-cmds' in your pyproject.toml config."
49+
)
50+
return False
51+
except Exception as e:
52+
logger.error(
53+
f"⚠️ Error running formatter '{command_str}': {e}\n"
54+
f"Please verify the 'formatter-cmds' in your pyproject.toml config."
55+
)
56+
return False
3257

3358

3459
@lru_cache(maxsize=1)

codeflash/code_utils/formatter.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,9 @@ def apply_formatter_cmds(
7676
logger.error(f"Failed to format code with {' '.join(formatter_cmd_list)}")
7777
except FileNotFoundError as e:
7878
from rich.panel import Panel
79-
from rich.text import Text
8079

81-
panel = Panel(
82-
Text.from_markup(f"⚠️ Formatter command not found: {' '.join(formatter_cmd_list)}", style="bold red"),
83-
expand=False,
84-
)
80+
command_str = " ".join(str(part) for part in formatter_cmd_list)
81+
panel = Panel(f"⚠️ Formatter command not found: {command_str}", expand=False, border_style="yellow")
8582
console.print(panel)
8683
if exit_on_failure:
8784
raise e from None

0 commit comments

Comments
 (0)