Skip to content

Commit aaafe47

Browse files
Merge pull request #670 from codeflash-ai/vsc/environment-validation
[LSP] validating python and git environment
2 parents 217ced2 + a0e2edb commit aaafe47

File tree

7 files changed

+101
-27
lines changed

7 files changed

+101
-27
lines changed

codeflash/cli_cmds/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from codeflash.code_utils import env_utils
1111
from codeflash.code_utils.code_utils import exit_with_message
1212
from codeflash.code_utils.config_parser import parse_config_file
13+
from codeflash.lsp.helpers import is_LSP_enabled
1314
from codeflash.version import __version__ as version
1415

1516

@@ -211,6 +212,9 @@ def process_pyproject_config(args: Namespace) -> Namespace:
211212
if args.benchmarks_root:
212213
args.benchmarks_root = Path(args.benchmarks_root).resolve()
213214
args.test_project_root = project_root_from_module_root(args.tests_root, pyproject_file_path)
215+
if is_LSP_enabled():
216+
args.all = None
217+
return args
214218
return handle_optimize_all_arg_parsing(args)
215219

216220

codeflash/cli_cmds/cmd_init.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ def ask_run_end_to_end_test(args: Namespace) -> None:
155155
run_end_to_end_test(args, bubble_sort_path, bubble_sort_test_path)
156156

157157

158+
def is_valid_pyproject_toml(pyproject_toml_path: Path) -> dict[str, Any] | None:
159+
if not pyproject_toml_path.exists():
160+
return None
161+
try:
162+
config, _ = parse_config_file(pyproject_toml_path)
163+
except Exception:
164+
return None
165+
166+
if "module_root" not in config or config["module_root"] is None or not Path(config["module_root"]).is_dir():
167+
return None
168+
if "tests_root" not in config or config["tests_root"] is None or not Path(config["tests_root"]).is_dir():
169+
return None
170+
171+
return config
172+
173+
158174
def should_modify_pyproject_toml() -> tuple[bool, dict[str, Any] | None]:
159175
"""Check if the current directory contains a valid pyproject.toml file with codeflash config.
160176
@@ -163,16 +179,9 @@ def should_modify_pyproject_toml() -> tuple[bool, dict[str, Any] | None]:
163179
from rich.prompt import Confirm
164180

165181
pyproject_toml_path = Path.cwd() / "pyproject.toml"
166-
if not pyproject_toml_path.exists():
167-
return True, None
168-
try:
169-
config, config_file_path = parse_config_file(pyproject_toml_path)
170-
except Exception:
171-
return True, None
172182

173-
if "module_root" not in config or config["module_root"] is None or not Path(config["module_root"]).is_dir():
174-
return True, None
175-
if "tests_root" not in config or config["tests_root"] is None or not Path(config["tests_root"]).is_dir():
183+
config = is_valid_pyproject_toml(pyproject_toml_path)
184+
if config is None:
176185
return True, None
177186

178187
return Confirm.ask(
@@ -968,6 +977,11 @@ def install_github_app(git_remote: str) -> None:
968977
except git.InvalidGitRepositoryError:
969978
click.echo("Skipping GitHub app installation because you're not in a git repository.")
970979
return
980+
981+
if git_remote not in get_git_remotes(git_repo):
982+
click.echo(f"Skipping GitHub app installation, remote ({git_remote}) does not exist in this repository.")
983+
return
984+
971985
owner, repo = get_repo_owner_and_name(git_repo, git_remote)
972986

973987
if is_github_app_installed_on_repo(owner, repo, suppress_errors=True):

codeflash/code_utils/git_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None:
201201

202202
def create_worktree_snapshot_commit(worktree_dir: Path, commit_message: str) -> None:
203203
repository = git.Repo(worktree_dir, search_parent_directories=True)
204-
repository.git.commit("-am", commit_message, "--no-verify")
204+
repository.git.add(".")
205+
repository.git.commit("-m", commit_message, "--no-verify")
205206

206207

207208
def create_detached_worktree(module_root: Path) -> Optional[Path]:
@@ -218,7 +219,10 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
218219

219220
# Get uncommitted diff from the original repo
220221
repository.git.add("-N", ".") # add the index for untracked files to be included in the diff
221-
uni_diff_text = repository.git.diff(None, "HEAD", ignore_blank_lines=True, ignore_space_at_eol=True)
222+
exclude_binary_files = [":!*.pyc", ":!*.pyo", ":!*.pyd", ":!*.so", ":!*.dll", ":!*.whl", ":!*.egg", ":!*.egg-info", ":!*.pyz", ":!*.pkl", ":!*.pickle", ":!*.joblib", ":!*.npy", ":!*.npz", ":!*.h5", ":!*.hdf5", ":!*.pth", ":!*.pt", ":!*.pb", ":!*.onnx", ":!*.db", ":!*.sqlite", ":!*.sqlite3", ":!*.feather", ":!*.parquet", ":!*.jpg", ":!*.jpeg", ":!*.png", ":!*.gif", ":!*.bmp", ":!*.tiff", ":!*.webp", ":!*.wav", ":!*.mp3", ":!*.ogg", ":!*.flac", ":!*.mp4", ":!*.avi", ":!*.mov", ":!*.mkv", ":!*.pdf", ":!*.doc", ":!*.docx", ":!*.xls", ":!*.xlsx", ":!*.ppt", ":!*.pptx", ":!*.zip", ":!*.rar", ":!*.tar", ":!*.tar.gz", ":!*.tgz", ":!*.bz2", ":!*.xz"] # fmt: off
223+
uni_diff_text = repository.git.diff(
224+
None, "HEAD", "--", *exclude_binary_files, ignore_blank_lines=True, ignore_space_at_eol=True
225+
)
222226

223227
if not uni_diff_text.strip():
224228
logger.info("No uncommitted changes to copy to worktree.")
@@ -234,7 +238,7 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
234238
# Apply the patch inside the worktree
235239
try:
236240
subprocess.run(
237-
["git", "apply", "--ignore-space-change", "--ignore-whitespace", patch_path],
241+
["git", "apply", "--ignore-space-change", "--ignore-whitespace", "--whitespace=nowarn", patch_path],
238242
cwd=worktree_dir,
239243
check=True,
240244
)

codeflash/discovery/functions_to_optimize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from codeflash.code_utils.git_utils import get_git_diff, get_repo_owner_and_name
2828
from codeflash.code_utils.time_utils import humanize_runtime
2929
from codeflash.discovery.discover_unit_tests import discover_unit_tests
30+
from codeflash.lsp.helpers import is_LSP_enabled
3031
from codeflash.models.models import FunctionParent
3132
from codeflash.telemetry.posthog_cf import ph
3233

@@ -168,6 +169,7 @@ def get_functions_to_optimize(
168169
)
169170
functions: dict[str, list[FunctionToOptimize]]
170171
trace_file_path: Path | None = None
172+
is_lsp = is_LSP_enabled()
171173
with warnings.catch_warnings():
172174
warnings.simplefilter(action="ignore", category=SyntaxWarning)
173175
if optimize_all:
@@ -185,6 +187,8 @@ def get_functions_to_optimize(
185187
if only_get_this_function is not None:
186188
split_function = only_get_this_function.split(".")
187189
if len(split_function) > 2:
190+
if is_lsp:
191+
return functions, 0, None
188192
exit_with_message(
189193
"Function name should be in the format 'function_name' or 'class_name.function_name'"
190194
)
@@ -200,6 +204,8 @@ def get_functions_to_optimize(
200204
):
201205
found_function = fn
202206
if found_function is None:
207+
if is_lsp:
208+
return functions, 0, None
203209
exit_with_message(
204210
f"Function {only_function_name} not found in file {file}\nor the function does not have a 'return' statement or is a property"
205211
)
@@ -470,6 +476,10 @@ def was_function_previously_optimized(
470476
Tuple of (filtered_functions_dict, remaining_count)
471477
472478
"""
479+
if is_LSP_enabled():
480+
# was_function_previously_optimized is for the checking the optimization duplicates in the github action, no need to do this in the LSP mode
481+
return False
482+
473483
# Check optimization status if repository info is provided
474484
# already_optimized_count = 0
475485
try:

codeflash/lsp/beta.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
from pathlib import Path
77
from typing import TYPE_CHECKING
88

9+
import git
910
from pygls import uris
1011

1112
from codeflash.api.cfapi import get_codeflash_api_key, get_user_id
13+
from codeflash.cli_cmds.cli import process_pyproject_config
1214
from codeflash.code_utils.git_utils import create_diff_patch_from_worktree
1315
from codeflash.code_utils.shell_utils import save_api_key_to_rc
1416
from codeflash.discovery.functions_to_optimize import filter_functions, get_functions_within_git_diff
1517
from codeflash.either import is_successful
1618
from codeflash.lsp.server import CodeflashLanguageServer, CodeflashLanguageServerProtocol
1719

1820
if TYPE_CHECKING:
21+
from argparse import Namespace
22+
1923
from lsprotocol import types
2024

2125

@@ -85,9 +89,12 @@ def initialize_function_optimization(
8589
) -> dict[str, str]:
8690
file_path = Path(uris.to_fs_path(params.textDocument.uri))
8791
server.show_message_log(f"Initializing optimization for function: {params.functionName} in {file_path}", "Info")
92+
8893
if server.optimizer is None:
89-
_initialize_optimizer_if_valid(server)
94+
_initialize_optimizer_if_api_key_is_valid(server)
95+
9096
server.optimizer.worktree_mode()
97+
9198
original_args, _ = server.optimizer.original_args_and_test_cfg
9299

93100
server.optimizer.args.function = params.functionName
@@ -99,15 +106,12 @@ def initialize_function_optimization(
99106
f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info"
100107
)
101108

102-
optimizable_funcs, _, _ = server.optimizer.get_optimizable_functions()
103-
if not optimizable_funcs:
109+
optimizable_funcs, count, _ = server.optimizer.get_optimizable_functions()
110+
111+
if count == 0:
104112
server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning")
105-
return {
106-
"functionName": params.functionName,
107-
"status": "error",
108-
"message": "function is no found or not optimizable",
109-
"args": None,
110-
}
113+
cleanup_the_optimizer(server)
114+
return {"functionName": params.functionName, "status": "error", "message": "not found", "args": None}
111115

112116
fto = optimizable_funcs.popitem()[1][0]
113117
server.optimizer.current_function_being_optimized = fto
@@ -129,7 +133,33 @@ def discover_function_tests(server: CodeflashLanguageServer, params: FunctionOpt
129133
return {"functionName": params.functionName, "status": "success", "discovered_tests": num_discovered_tests}
130134

131135

132-
def _initialize_optimizer_if_valid(server: CodeflashLanguageServer) -> dict[str, str]:
136+
@server.feature("validateProject")
137+
def validate_project(server: CodeflashLanguageServer, _params: FunctionOptimizationParams) -> dict[str, str]:
138+
from codeflash.cli_cmds.cmd_init import is_valid_pyproject_toml
139+
140+
server.show_message_log("Validating project...", "Info")
141+
config = is_valid_pyproject_toml(server.args.config_file)
142+
if config is None:
143+
server.show_message_log("pyproject.toml is not valid", "Error")
144+
return {
145+
"status": "error",
146+
"message": "pyproject.toml is not valid", # keep the error message the same, the extension is matching "pyproject.toml" in the error message to show the codeflash init instructions
147+
}
148+
149+
args = process_args(server)
150+
repo = git.Repo(args.module_root, search_parent_directories=True)
151+
if repo.bare:
152+
return {"status": "error", "message": "Repository is in bare state"}
153+
154+
try:
155+
_ = repo.head.commit
156+
except Exception:
157+
return {"status": "error", "message": "Repository has no commits (unborn HEAD)"}
158+
159+
return {"status": "success"}
160+
161+
162+
def _initialize_optimizer_if_api_key_is_valid(server: CodeflashLanguageServer) -> dict[str, str]:
133163
user_id = get_user_id()
134164
if user_id is None:
135165
return {"status": "error", "message": "api key not found or invalid"}
@@ -140,14 +170,24 @@ def _initialize_optimizer_if_valid(server: CodeflashLanguageServer) -> dict[str,
140170

141171
from codeflash.optimization.optimizer import Optimizer
142172

143-
server.optimizer = Optimizer(server.args)
173+
new_args = process_args(server)
174+
server.optimizer = Optimizer(new_args)
144175
return {"status": "success", "user_id": user_id}
145176

146177

178+
def process_args(server: CodeflashLanguageServer) -> Namespace:
179+
if server.args_processed_before:
180+
return server.args
181+
new_args = process_pyproject_config(server.args)
182+
server.args = new_args
183+
server.args_processed_before = True
184+
return new_args
185+
186+
147187
@server.feature("apiKeyExistsAndValid")
148188
def check_api_key(server: CodeflashLanguageServer, _params: any) -> dict[str, str]:
149189
try:
150-
return _initialize_optimizer_if_valid(server)
190+
return _initialize_optimizer_if_api_key_is_valid(server)
151191
except Exception:
152192
return {"status": "error", "message": "something went wrong while validating the api key"}
153193

@@ -167,7 +207,7 @@ def provide_api_key(server: CodeflashLanguageServer, params: ProvideApiKeyParams
167207
get_codeflash_api_key.cache_clear()
168208
get_user_id.cache_clear()
169209

170-
init_result = _initialize_optimizer_if_valid(server)
210+
init_result = _initialize_optimizer_if_api_key_is_valid(server)
171211
if init_result["status"] == "error":
172212
return {"status": "error", "message": "Api key is not valid"}
173213

codeflash/lsp/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ class CodeflashLanguageServer(LanguageServer):
4646
def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401
4747
super().__init__(*args, **kwargs)
4848
self.optimizer: Optimizer | None = None
49+
self.args_processed_before: bool = False
4950
self.args = None
5051

5152
def prepare_optimizer_arguments(self, config_file: Path) -> None:
52-
from codeflash.cli_cmds.cli import parse_args, process_pyproject_config
53+
from codeflash.cli_cmds.cli import parse_args
5354

5455
args = parse_args()
5556
args.config_file = config_file
5657
args.no_pr = True # LSP server should not create PRs
5758
args.worktree = True
58-
args = process_pyproject_config(args)
5959
self.args = args
6060
# avoid initializing the optimizer during initialization, because it can cause an error if the api key is invalid
6161

codeflash/optimization/optimizer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ def worktree_mode(self) -> None:
440440
return
441441
self.current_worktree = worktree_dir
442442
self.mutate_args_for_worktree_mode(worktree_dir)
443+
# make sure the tests dir is created in the worktree, this can happen if the original tests dir is empty
444+
Path(self.args.tests_root).mkdir(parents=True, exist_ok=True)
443445

444446
def mutate_args_for_worktree_mode(self, worktree_dir: Path) -> None:
445447
saved_args = copy.deepcopy(self.args)

0 commit comments

Comments
 (0)