Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: add expandvars() to 4 expanduser().resolve() locations (#2681)

LearningCircuit8bdb2f9d4ea7
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index 32ba444e94..31d5da561b 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -8,6 +8,8 @@ Provides endpoints for managing RAG indexing of library documents:
- Bulk operations with progress tracking
"""
+import os
+
from flask import (
Blueprint,
jsonify,
@@ -1506,7 +1508,11 @@ def upload_to_collection(collection_id):
"research_library.storage_path",
str(get_library_directory()),
)
- library_root = str(Path(library_root).expanduser().resolve())
+ library_root = str(
+ Path(os.path.expandvars(library_root))
+ .expanduser()
+ .resolve()
+ )
pdf_storage_manager = PDFStorageManager(
library_root=Path(library_root), storage_mode="database"
)
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index 97a2b489b1..5b633e8077 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -9,6 +9,7 @@ Handles downloading PDFs from various academic sources with:
"""
import hashlib
+import os
import re
import time
import uuid
@@ -100,7 +101,9 @@ class DownloadService:
raise ValueError("Storage path setting cannot be None")
self.library_root = str(
- Path(storage_path_setting).expanduser().resolve()
+ Path(os.path.expandvars(storage_path_setting))
+ .expanduser()
+ .resolve()
)
logger.warning(
f"[DOWNLOAD_SERVICE_INIT] Library root resolved to: {self.library_root}"
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_collection.py b/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
index d10f771a19..58bde0c159 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
@@ -4,6 +4,7 @@ Collection-specific RAG Search Engine
Provides semantic search within a specific document collection using RAG.
"""
+import os
from typing import List, Dict, Any, Optional
from loguru import logger
@@ -264,7 +265,11 @@ class CollectionSearchEngine(LibraryRAGSearchEngine):
self.settings_snapshot,
str(get_library_directory()),
)
- library_root = Path(library_root).expanduser().resolve()
+ library_root = (
+ Path(os.path.expandvars(library_root))
+ .expanduser()
+ .resolve()
+ )
pdf_manager = PDFStorageManager(library_root, "auto")
if pdf_manager.has_pdf(document, session):
document_url = f"/library/document/{doc_id}/pdf"
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_library.py b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
index 995ab44906..e548be4f6a 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_library.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
@@ -4,6 +4,7 @@ Library RAG Search Engine
Provides semantic search over the user's personal research library using RAG.
"""
+import os
from typing import List, Dict, Any, Optional
from loguru import logger
@@ -251,7 +252,9 @@ class LibraryRAGSearchEngine(BaseSearchEngine):
str(get_library_directory()),
)
library_root = (
- Path(library_root).expanduser().resolve()
+ Path(os.path.expandvars(library_root))
+ .expanduser()
+ .resolve()
)
pdf_manager = PDFStorageManager(
library_root, "auto"
diff --git a/tests/research_library/services/test_expandvars_path.py b/tests/research_library/services/test_expandvars_path.py
new file mode 100644
index 0000000000..0df27e93bd
--- /dev/null
+++ b/tests/research_library/services/test_expandvars_path.py
@@ -0,0 +1,107 @@
+"""
+Tests that $HOME-style environment variables in research_library.storage_path
+are correctly expanded via os.path.expandvars() in all code paths.
+"""
+
+import os
+from pathlib import Path
+from unittest.mock import Mock
+
+import pytest
+
+
+class TestDownloadServiceExpandvars:
+ """DownloadService should expand env vars in storage_path."""
+
+ def test_dollar_home_in_storage_path_is_expanded(self, mocker, tmp_path):
+ """$HOME in storage_path should resolve to actual home directory."""
+ real_home = str(tmp_path / "fakehome")
+ os.makedirs(real_home, exist_ok=True)
+
+ mock_settings = Mock()
+ mock_settings.get_setting.return_value = "$HOME/mylib"
+ mocker.patch(
+ "local_deep_research.research_library.services.download_service.get_settings_manager",
+ return_value=mock_settings,
+ )
+ mocker.patch("pathlib.Path.mkdir")
+ mocker.patch(
+ "local_deep_research.research_library.services.download_service.RetryManager"
+ )
+ mocker.patch.dict(os.environ, {"HOME": real_home})
+
+ from local_deep_research.research_library.services.download_service import (
+ DownloadService,
+ )
+
+ service = DownloadService(username="test_user")
+
+ expected = str(Path(real_home) / "mylib")
+ assert service.library_root == expected
+ assert "$" not in service.library_root
+
+ def test_custom_env_var_in_storage_path_is_expanded(self, mocker, tmp_path):
+ """Custom env vars like $MY_DATA_DIR should be expanded."""
+ data_dir = str(tmp_path / "data")
+ os.makedirs(data_dir, exist_ok=True)
+
+ mock_settings = Mock()
+ mock_settings.get_setting.return_value = "$MY_DATA_DIR/library"
+ mocker.patch(
+ "local_deep_research.research_library.services.download_service.get_settings_manager",
+ return_value=mock_settings,
+ )
+ mocker.patch("pathlib.Path.mkdir")
+ mocker.patch(
+ "local_deep_research.research_library.services.download_service.RetryManager"
+ )
+ mocker.patch.dict(os.environ, {"MY_DATA_DIR": data_dir})
+
+ from local_deep_research.research_library.services.download_service import (
+ DownloadService,
+ )
+
+ service = DownloadService(username="test_user")
+
+ expected = str(Path(data_dir) / "library")
+ assert service.library_root == expected
+ assert "$" not in service.library_root
+
+
+class TestExpandvarsUnit:
+ """Unit tests verifying the expandvars pattern works correctly."""
+
+ def test_expandvars_before_resolve(self, tmp_path):
+ """Verify the expandvars + expanduser + resolve pattern works."""
+ fake_home = str(tmp_path / "home")
+ os.makedirs(fake_home, exist_ok=True)
+
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setenv("HOME", fake_home)
+
+ raw = "$HOME/research_library"
+ result = Path(os.path.expandvars(raw)).expanduser().resolve()
+
+ assert str(result) == str(Path(fake_home) / "research_library")
+ assert "$" not in str(result)
+
+ def test_tilde_and_envvar_both_expanded(self, tmp_path):
+ """Both ~ and $VAR should be expanded in the same path."""
+ custom_dir = str(tmp_path / "custom")
+ os.makedirs(custom_dir, exist_ok=True)
+
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setenv("MY_SUBDIR", "custom")
+
+ raw = str(tmp_path / "$MY_SUBDIR/docs")
+ result = Path(os.path.expandvars(raw)).expanduser().resolve()
+
+ expected = str(Path(custom_dir) / "docs")
+ assert str(result) == expected
+
+ def test_no_envvar_path_unchanged(self):
+ """Paths without env vars should pass through unchanged."""
+ raw = "/absolute/path/to/library"
+ result = str(Path(os.path.expandvars(raw)).expanduser().resolve())
+
+ assert result == raw