Skip to content

ModelCache crashes on construction in environments without a resolvable home director #59

Description

@sainath-seelam

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions