Skip to content

Commit 4e19d99

Browse files
authored
Merge branch 'main' into release/v0.12.4
2 parents 22c7d3f + 8f60ace commit 4e19d99

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
721721
)
722722
elif formatter == "don't use a formatter":
723723
formatter_cmds.append("disabled")
724-
check_formatter_installed(formatter_cmds)
724+
check_formatter_installed(formatter_cmds, exit_on_failure=False)
725725
codeflash_section["formatter-cmds"] = formatter_cmds
726726
# Add the 'codeflash' section, ensuring 'tool' section exists
727727
tool_section = pyproject_data.get("tool", tomlkit.table())

codeflash/code_utils/env_utils.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
from __future__ import annotations
22

33
import os
4-
import shlex
5-
import subprocess
4+
import sys
65
import tempfile
76
from functools import lru_cache
87
from pathlib import Path
98
from typing import Optional
109

1110
from codeflash.cli_cmds.console import logger
11+
from codeflash.code_utils.formatter import format_code
1212
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config
1313

1414

15-
class FormatterNotFoundError(Exception):
16-
"""Exception raised when a formatter is not found."""
17-
18-
def __init__(self, formatter_cmd: str) -> None:
19-
super().__init__(f"Formatter command not found: {formatter_cmd}")
20-
21-
22-
def check_formatter_installed(formatter_cmds: list[str]) -> bool:
15+
def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool = True) -> bool: # noqa
2316
return_code = True
2417
if formatter_cmds[0] == "disabled":
2518
return return_code
@@ -28,22 +21,14 @@ def check_formatter_installed(formatter_cmds: list[str]) -> bool:
2821
f.write(tmp_code)
2922
f.flush()
3023
tmp_file = Path(f.name)
31-
file_token = "$file" # noqa: S105
32-
for command in set(formatter_cmds):
33-
formatter_cmd_list = shlex.split(command, posix=os.name != "nt")
34-
formatter_cmd_list = [tmp_file.as_posix() if chunk == file_token else chunk for chunk in formatter_cmd_list]
35-
try:
36-
result = subprocess.run(formatter_cmd_list, capture_output=True, check=False)
37-
except (FileNotFoundError, NotADirectoryError):
38-
return_code = False
39-
break
40-
if result.returncode:
41-
return_code = False
42-
break
43-
tmp_file.unlink(missing_ok=True)
44-
if not return_code:
45-
msg = f"Error running formatter command: {command}"
46-
raise FormatterNotFoundError(msg)
24+
try:
25+
format_code(formatter_cmds, tmp_file)
26+
except Exception:
27+
print(
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+
)
30+
if exit_on_failure:
31+
sys.exit(1)
4732
return return_code
4833

4934

codeflash/code_utils/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def format_code(formatter_cmds: list[str], path: Path) -> str:
2222
if formatter_name == "disabled":
2323
return path.read_text(encoding="utf8")
2424
file_token = "$file" # noqa: S105
25-
for command in set(formatter_cmds):
25+
for command in formatter_cmds:
2626
formatter_cmd_list = shlex.split(command, posix=os.name != "nt")
2727
formatter_cmd_list = [path.as_posix() if chunk == file_token else chunk for chunk in formatter_cmd_list]
2828
try:

0 commit comments

Comments
 (0)