Skip to content

Commit 87d10e3

Browse files
committed
fix(vscode): vscode state database path detection to support shared storage
Extract VSCode state database path resolution into a dedicated utility function to support both standard and shared storage configurations. Add automatic detection of VSCode installation via product.json to determine the correct sharedDataFolderName, falling back to the default APPDATA location when shared storage is not configured or detection fails.
1 parent 0cdb7ac commit 87d10e3

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
import logging
3+
import os
4+
5+
6+
def get_state_db_path() -> str:
7+
product_json_path = find_vscode_product_json()
8+
try:
9+
if os.path.exists(product_json_path):
10+
with open(product_json_path, encoding="utf-8") as f:
11+
product = json.load(f)
12+
13+
shared_folder = product.get("sharedDataFolderName")
14+
if shared_folder:
15+
shared_path = os.path.join(os.path.expanduser("~"), shared_folder, "sharedStorage", "state.vscdb")
16+
17+
if os.path.exists(shared_path):
18+
return shared_path
19+
except Exception as e:
20+
logging.error("Shared storage detection failed: %s", e)
21+
22+
return os.path.expandvars(r"%APPDATA%\Code\User\globalStorage\state.vscdb")
23+
24+
25+
def find_vscode_product_json() -> str | None:
26+
base_path = os.path.expandvars(r"%LOCALAPPDATA%\Programs\Microsoft VS Code")
27+
28+
if not os.path.exists(base_path):
29+
return None
30+
31+
try:
32+
candidates = []
33+
for name in os.listdir(base_path):
34+
full_path = os.path.join(base_path, name)
35+
product_path = os.path.join(full_path, "resources", "app", "product.json")
36+
37+
if os.path.isfile(product_path):
38+
candidates.append((os.path.getmtime(full_path), product_path))
39+
40+
if not candidates:
41+
return None
42+
43+
candidates.sort(reverse=True)
44+
return candidates[0][1]
45+
46+
except Exception as e:
47+
logging.error("Failed to locate VSCode product.json: %s", e)
48+
return None

src/core/widgets/services/quick_launch/providers/vscode.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77

88
from core.utils.shell_utils import shell_open
9+
from core.utils.widgets.vscode.get_vscode_state_db_path import get_state_db_path
910
from core.utils.win32.constants import SW_HIDE
1011
from core.widgets.services.quick_launch.base_provider import (
1112
BaseProvider,
@@ -101,7 +102,7 @@ class VSCodeProvider(BaseProvider):
101102

102103
def __init__(self, config: dict | None = None):
103104
super().__init__(config)
104-
self._db_path = os.path.expandvars(r"%APPDATA%\Code\User\globalStorage\state.vscdb")
105+
self._db_path = get_state_db_path()
105106

106107
def _get_recents(self) -> list[dict]:
107108
if not os.path.exists(self._db_path):

src/core/widgets/yasb/vscode.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from PyQt6.QtWidgets import QHBoxLayout, QLabel, QScrollArea, QVBoxLayout, QWidget
1212

1313
from core.utils.utilities import PopupWidget
14+
from core.utils.widgets.vscode.get_vscode_state_db_path import get_state_db_path
1415
from core.validation.widgets.yasb.vscode import VSCodeConfig
1516
from core.widgets.base import BaseWidget
1617

@@ -22,9 +23,10 @@ def __init__(self, config: VSCodeConfig):
2223
super().__init__(class_name="vscode-widget")
2324
self.config = config
2425
self._show_alt_label = False
26+
2527
state_storage_path = self.config.state_storage_path
2628
if not state_storage_path:
27-
state_storage_path = os.path.expandvars(r"%APPDATA%\Code\User\globalStorage\state.vscdb")
29+
state_storage_path = get_state_db_path()
2830
self._state_file_path = state_storage_path
2931

3032
self._init_container()
@@ -70,7 +72,7 @@ def _load_recent_workspaces(self) -> list[dict]:
7072
else:
7173
logging.error("Unexpected entry type: %s", type(path))
7274
else:
73-
logging.error("No data found in %s", file_path)
75+
logging.error("No data found in %s", self._state_file_path)
7476
conn.close()
7577
return result_list
7678
except Exception as e:

0 commit comments

Comments
 (0)