Skip to content

Commit 1361dde

Browse files
Merge branch 'main' into lsp/task-execution-context
2 parents 773b228 + 44e46b4 commit 1361dde

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 36 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,41 @@
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).replace(" $file", "")
30+
31+
if shutil.which(exe_name) is None:
32+
logger.error(
33+
f"Could not find formatter: {command_str}\n"
34+
f"Please install it or update 'formatter-cmds' in your codeflash configuration"
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"Could not find formatter: {command_str}\n"
48+
f"Please install it or update 'formatter-cmds' in your codeflash configuration"
49+
)
50+
return False
51+
except Exception as e:
52+
logger.error(f"Formatter failed to run: {command_str}\nError: {e}")
53+
return False
3254

3355

3456
@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

codeflash/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from codeflash.cli_cmds.cli import parse_args, process_pyproject_config
1010
from codeflash.cli_cmds.cmd_init import CODEFLASH_LOGO, ask_run_end_to_end_test
1111
from codeflash.cli_cmds.console import paneled_text
12+
from codeflash.code_utils import env_utils
1213
from codeflash.code_utils.checkpoint import ask_should_use_checkpoint_get_functions
1314
from codeflash.code_utils.config_parser import parse_config_file
1415
from codeflash.code_utils.version_check import check_for_newer_minor_version
@@ -39,6 +40,8 @@ def main() -> None:
3940
ask_run_end_to_end_test(args)
4041
else:
4142
args = process_pyproject_config(args)
43+
if not env_utils.check_formatter_installed(args.formatter_cmds):
44+
return
4245
args.previous_checkpoint_functions = ask_should_use_checkpoint_get_functions(args)
4346
init_sentry(not args.disable_telemetry, exclude_errors=True)
4447
posthog_cf.initialize_posthog(not args.disable_telemetry)

codeflash/optimization/optimizer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ def run(self) -> None:
260260
console.rule()
261261
if not env_utils.ensure_codeflash_api_key():
262262
return
263-
if not env_utils.check_formatter_installed(self.args.formatter_cmds):
264-
return
265263
if self.args.no_draft and is_pr_draft():
266264
logger.warning("PR is in draft mode, skipping optimization")
267265
return

docs/crisp.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/docs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
},
1010
"favicon": "/favicon.ico",
1111
"integrations": {
12-
"intercom": {
13-
"appId": "ljxo1nzr"
12+
"posthog": {
13+
"apiKey": "phc_aUO790jHd7z1SXwsYCz8dRApxueplZlZWeDSpKc5hol"
1414
}
1515
},
1616
"navigation": {

0 commit comments

Comments
 (0)