Description:
Constructing SSolutionsRepositoryBuilder() raises RuntimeError: Could not determine home directory. in any environment where the user's home directory cannot be resolved, because ModelCache.__init__ eagerly calls Path.home() regardless of whether caching will actually be used.
Traceback
File "SSolutionsRepositoryBuilder.py", line 34, in __init__
self._cache = ModelCache()
File "ModelCache.py", line 34, in __init__
self._dir = cache_dir or self._default_dir()
File "ModelCache.py", line 26, in _default_dir
d = Path.home() / ".mps_cli_cache"
File "pathlib.py", line 1406, in expanduser
raise RuntimeError("Could not determine home directory.")
Expected Behaviour and Fix:
ModelCache.__init__ eagerly calls Path.home() to resolve the default cache directory at construction time, before any file is ever read or written. In environments where HOME (Linux/macOS) or USERPROFILE (Windows) is unset, Path.home() raises RuntimeError: Could not determine home directory. — crashing the entire program on startup.
The fix is to defer _default_dir() out of __init__ and call it lazily the first time load() or save() is actually used:
def __init__(self, cache_dir: Path | None = None):
self._dir = cache_dir # None = resolve on first access
def _get_dir(self) -> Path:
if self._dir is None:
self._dir = self._default_dir()
return self._dir
def load(self, path: Path):
try:
cache_file = self._get_dir() / self._key(path)
...
def save(self, path: Path, model) -> None:
try:
cache_file = self._get_dir() / self._key(path)
...
This way constructing ModelCache() or SSolutionsRepositoryBuilder() is always safe, and Path.home() is only called if caching is actually exercised.
Description:
Constructing
SSolutionsRepositoryBuilder()raises RuntimeError: Could not determine home directory. in any environment where the user's home directory cannot be resolved, becauseModelCache.__init__eagerly callsPath.home()regardless of whether caching will actually be used.Traceback
Expected Behaviour and Fix:
ModelCache.__init__eagerly callsPath.home()to resolve the default cache directory at construction time, before any file is ever read or written. In environments where HOME (Linux/macOS) or USERPROFILE (Windows) is unset,Path.home()raises RuntimeError: Could not determine home directory. — crashing the entire program on startup.The fix is to defer _default_dir() out of
__init__and call it lazily the first timeload()orsave()is actually used:This way constructing
ModelCache()orSSolutionsRepositoryBuilder()is always safe, andPath.home()is only called if caching is actually exercised.