diff --git a/src/lean_interact/config.py b/src/lean_interact/config.py index 04806e8..edbf527 100644 --- a/src/lean_interact/config.py +++ b/src/lean_interact/config.py @@ -6,7 +6,6 @@ from typing import Literal from filelock import FileLock -from git import GitCommandError, Repo from lean_interact.utils import ( DEFAULT_CACHE_DIR, @@ -235,8 +234,7 @@ def _modify_lakefile(self, project_dir: str, lean_version: str) -> None: with open(os.path.join(project_dir, "lakefile.lean"), "a", encoding="utf-8") as f: for req in require: f.write(f'\n\nrequire {req.name} from git\n "{req.git}"' + (f' @ "{req.rev}"' if req.rev else "")) - - + class LeanREPLConfig: def __init__( self, @@ -247,6 +245,11 @@ def __init__( cache_dir: str = DEFAULT_CACHE_DIR, memory_hard_limit_mb: int | None = None, verbose: bool = False, + setup: bool = True, + lake_env_repl_path : str | None = None, + working_dir : str | None = None, + lake_path : str = "lake", + lake_command : str = "env", ): """ Initialize the Lean REPL configuration. @@ -303,11 +306,14 @@ def __init__( "Lean 4 build system (`lake`) is not installed. You can try to run `install-lean` or find installation instructions here: https://leanprover-community.github.io/get_started.html" ) - self._setup_repl() + if setup: + self._setup_repl() assert isinstance(self.lean_version, str) - if self.project is None: + if working_dir: + self._working_dir = working_dir + elif self.project is None: self._working_dir = self._cache_repl_dir else: self.project._instantiate( @@ -316,8 +322,14 @@ def __init__( verbose=self.verbose, ) self._working_dir = self.project._get_directory(cache_dir=self.cache_dir, lean_version=self.lean_version) + + self.lake_env_repl_path = lake_env_repl_path or os.path.join(self._cache_repl_dir, ".lake", "build", "bin", "repl") + self.lake_path = lake_path + self.lake_command = lake_command def _setup_repl(self) -> None: + from git import GitCommandError, Repo + assert isinstance(self.repl_rev, str) # Lock the clean REPL directory during setup to prevent race conditions @@ -385,6 +397,7 @@ def _get_available_lean_versions_sha(self) -> list[tuple[str, str]]: """ Get the available Lean versions for the selected REPL. """ + from git import Repo repo = Repo(self.cache_clean_repl_dir) return [ (str(commit.message.strip()), commit.hexsha) diff --git a/src/lean_interact/server.py b/src/lean_interact/server.py index 91b04be..47a0e91 100644 --- a/src/lean_interact/server.py +++ b/src/lean_interact/server.py @@ -63,7 +63,7 @@ def lean_version(self) -> str | None: def start(self) -> None: self._proc = subprocess.Popen( - ["lake", "env", os.path.join(self.config._cache_repl_dir, ".lake", "build", "bin", "repl")], + [self.config.lake_path, self.config.lake_command, self.config.lake_env_repl_path], cwd=self.config.working_dir, stdin=subprocess.PIPE, stdout=subprocess.PIPE, @@ -121,7 +121,7 @@ def _execute_cmd_in_repl(self, json_query: str, verbose: bool, timeout: float | with self._lock: if verbose: logger.info("Sending query: %s", json_query) - self._proc.stdin.write(json_query + "\n\n") + self._proc.stdin.write(json_query + "\n\n\n\n") self._proc.stdin.flush() output: str = ""