Skip to content

Commit a778cd5

Browse files
committed
feat(robot): add ROBOTCODE_CACHE_DIR env var for cache path override
Enable redirecting the analysis cache to a custom directory via the `ROBOTCODE_CACHE_DIR` environment variable. This allows all RobotCode tools — CLI commands, analyze runs, and the language server — to share the same cache regardless of where it is stored. The VS Code extension automatically sets this variable in the integrated terminal, so CLI commands find the correct cache without any manual setup. Cache clearing now operates on the database directly instead of removing the directory, preventing errors when multiple processes access the cache concurrently.
1 parent 8fc8687 commit a778cd5

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

packages/analyze/src/robotcode/analyze/cache/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
SqliteDataCache,
1414
build_cache_dir,
1515
exclusive_cache_lock,
16+
resolve_cache_base_path,
1617
)
1718

1819
from ..config import AnalyzeConfig
@@ -43,6 +44,8 @@ def _resolve_cache(
4344
if analyzer_config.cache is not None and analyzer_config.cache.cache_dir is not None:
4445
cache_base_path = Path(analyzer_config.cache.cache_dir)
4546

47+
cache_base_path = resolve_cache_base_path(cache_base_path)
48+
4649
cache_dir = build_cache_dir(cache_base_path)
4750

4851
if not cache_dir.exists() or not (cache_dir / "cache.db").exists():
@@ -374,6 +377,8 @@ def _resolve_cache_root(
374377
if analyzer_config.cache is not None and analyzer_config.cache.cache_dir is not None:
375378
cache_base_path = Path(analyzer_config.cache.cache_dir)
376379

380+
cache_base_path = resolve_cache_base_path(cache_base_path)
381+
377382
return cache_base_path / CACHE_DIR_NAME
378383

379384

packages/robot/src/robotcode/robot/diagnostics/data_cache.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ def exclusive_cache_lock(cache_dir: Path) -> Iterator[bool]:
152152
os.close(fd)
153153

154154

155+
def resolve_cache_base_path(base_path: Path) -> Path:
156+
"""Apply ROBOTCODE_CACHE_DIR env var override to a cache base path."""
157+
env_cache_dir = os.environ.get("ROBOTCODE_CACHE_DIR")
158+
if env_cache_dir:
159+
return Path(env_cache_dir)
160+
return base_path
161+
162+
155163
def build_cache_dir(base_path: Path) -> Path:
156164
return (
157165
base_path

packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ def get_imports_manager_for_workspace_folder(self, folder: Optional[WorkspaceFol
660660
return self._imports_managers[folder]
661661

662662
def calc_cache_path(self, folder_uri: Uri) -> Path:
663-
return folder_uri.to_path()
663+
from .data_cache import resolve_cache_base_path
664+
665+
return resolve_cache_base_path(folder_uri.to_path())
664666

665667
def get_diagnostic_modifier(self, document: TextDocument) -> DiagnosticsModifier:
666668
return document.get_cache(self.__get_diagnostic_modifier)

packages/robot/src/robotcode/robot/diagnostics/imports_manager.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ast
22
import multiprocessing as mp
33
import os
4-
import shutil
54
import threading
65
import weakref
76
from abc import ABC, abstractmethod
@@ -905,10 +904,8 @@ def get_resource_doc_from_document(self, document: TextDocument) -> ResourceDoc:
905904
return self.get_libdoc_from_model(model, source)
906905

907906
def clear_cache(self) -> None:
908-
if self.cache_path.exists():
909-
shutil.rmtree(self.cache_path, ignore_errors=True)
910-
911-
self._logger.debug(lambda: f"Cleared cache {self.cache_path}")
907+
count = self.data_cache.clear_all()
908+
self._logger.debug(lambda: f"Cleared {count} cache entries from {self.cache_path}")
912909

913910
@_logger.call
914911
def get_command_line_variables(self) -> List[VariableDefinition]:

vscode-client/extension/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ export async function activateAsync(context: vscode.ExtensionContext): Promise<v
187187
collection.replace("ROBOTCODE_DISABLE_ANSI_LINKS", "1");
188188
collection.replace("ROBOTCODE_BUNDLED_ROBOTCODE_MAIN", pythonManager.robotCodeMain);
189189

190+
if (context.storageUri) {
191+
collection.replace("ROBOTCODE_CACHE_DIR", context.storageUri.fsPath);
192+
}
193+
190194
languageClientManger.refresh();
191195
}
192196

0 commit comments

Comments
 (0)