Skip to content

Commit 98fba5e

Browse files
small fix
1 parent 3f6e6c9 commit 98fba5e

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

codeflash/code_utils/formatter.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,13 @@ def apply_formatter_cmds(
4545
print_status: bool, # noqa
4646
exit_on_failure: bool = True, # noqa
4747
) -> tuple[Path, str, bool]:
48-
# TODO: Only allow a particular whitelist of formatters here to prevent arbitrary code execution
49-
formatter_name = cmds[0].lower()
5048
should_make_copy = False
5149
file_path = path
5250

5351
if test_dir_str:
5452
should_make_copy = True
5553
file_path = Path(test_dir_str) / "temp.py"
5654

57-
if not cmds or formatter_name == "disabled":
58-
return path, path.read_text(encoding="utf8"), False
59-
6055
if not path.exists():
6156
msg = f"File {path} does not exist. Cannot apply formatter commands."
6257
raise FileNotFoundError(msg)
@@ -114,6 +109,12 @@ def format_code(
114109
if console.quiet:
115110
# lsp mode
116111
exit_on_failure = False
112+
113+
# TODO: Only allow a particular whitelist of formatters here to prevent arbitrary code execution
114+
formatter_name = formatter_cmds[0].lower() if formatter_cmds else "disabled"
115+
if formatter_name == "disabled":
116+
return path.read_text(encoding="utf8")
117+
117118
with tempfile.TemporaryDirectory() as test_dir_str:
118119
if isinstance(path, str):
119120
path = Path(path)
@@ -152,16 +153,17 @@ def format_code(
152153
return original_code
153154

154155
# TODO : We can avoid formatting the whole file again and only formatting the optimized code standalone and replace in formatted file above.
155-
try:
156-
_, formatted_code, _ = apply_formatter_cmds(
157-
formatter_cmds, path, test_dir_str=None, print_status=print_status, exit_on_failure=exit_on_failure
156+
_, formatted_code, changed = apply_formatter_cmds(
157+
formatter_cmds, path, test_dir_str=None, print_status=print_status, exit_on_failure=exit_on_failure
158+
)
159+
if not changed:
160+
logger.warning(
161+
f"No changes detected in {path} after formatting, are you sure you have valid formatter commands?"
158162
)
159-
except FileNotFoundError as e:
160-
logger.warning(f"Formatter not found, returning original code: {e}")
161163
return original_code
162-
else:
163-
logger.debug(f"Formatted {path} with commands: {formatter_cmds}")
164-
return formatted_code
164+
165+
logger.debug(f"Formatted {path} with commands: {formatter_cmds}")
166+
return formatted_code
165167

166168

167169
def sort_imports(code: str) -> str:

0 commit comments

Comments
 (0)