Skip to content

Commit 7e3b842

Browse files
authored
Terminal suggest: Move storage from globalState to globalStorageUri (microsoft#255114)
1 parent 2506e28 commit 7e3b842

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

extensions/terminal-suggest/src/terminalSuggestMain.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type ShellGlobalsCacheEntryWithMeta = ShellGlobalsCacheEntry & { timestamp: numb
4646
const cachedGlobals: Map<string, ShellGlobalsCacheEntryWithMeta> = new Map();
4747
let pathExecutableCache: PathExecutableCache;
4848
const CACHE_KEY = 'terminalSuggestGlobalsCacheV2';
49-
let globalStorage: vscode.Memento;
49+
let globalStorageUri: vscode.Uri;
5050
const CACHE_MAX_AGE_MS = 1000 * 60 * 60 * 24 * 7; // 7 days
5151

5252
function getCacheKey(machineId: string, remoteAuthority: string | undefined, shellType: TerminalShellType): string {
@@ -150,7 +150,7 @@ async function fetchAndCacheShellGlobals(
150150

151151

152152
async function writeGlobalsCache(): Promise<void> {
153-
if (!globalStorage) {
153+
if (!globalStorageUri) {
154154
return;
155155
}
156156
// Remove old entries
@@ -165,25 +165,40 @@ async function writeGlobalsCache(): Promise<void> {
165165
obj[key] = value;
166166
}
167167
try {
168-
await globalStorage.update(CACHE_KEY, obj);
168+
// Ensure the directory exists
169+
const terminalSuggestDir = vscode.Uri.joinPath(globalStorageUri, 'terminal-suggest');
170+
await vscode.workspace.fs.createDirectory(terminalSuggestDir);
171+
const cacheFile = vscode.Uri.joinPath(terminalSuggestDir, `${CACHE_KEY}.json`);
172+
const data = Buffer.from(JSON.stringify(obj), 'utf8');
173+
await vscode.workspace.fs.writeFile(cacheFile, data);
169174
} catch (err) {
170175
console.error('Failed to write terminal suggest globals cache:', err);
171176
}
172177
}
173178

174179

175180
async function readGlobalsCache(): Promise<void> {
176-
if (!globalStorage) {
181+
if (!globalStorageUri) {
177182
return;
178183
}
179184
try {
180-
const obj = globalStorage.get<Record<string, ShellGlobalsCacheEntryWithMeta>>(CACHE_KEY);
185+
const terminalSuggestDir = vscode.Uri.joinPath(globalStorageUri, 'terminal-suggest');
186+
const cacheFile = vscode.Uri.joinPath(terminalSuggestDir, `${CACHE_KEY}.json`);
187+
const data = await vscode.workspace.fs.readFile(cacheFile);
188+
const obj = JSON.parse(data.toString()) as Record<string, ShellGlobalsCacheEntryWithMeta>;
181189
if (obj) {
182190
for (const key of Object.keys(obj)) {
183191
cachedGlobals.set(key, obj[key]);
184192
}
185193
}
186-
} catch { }
194+
} catch (err) {
195+
// File might not exist yet, which is expected on first run
196+
if (err instanceof vscode.FileSystemError && err.code === 'FileNotFound') {
197+
// This is expected on first run
198+
return;
199+
}
200+
console.error('Failed to read terminal suggest globals cache:', err);
201+
}
187202
}
188203

189204

@@ -193,7 +208,7 @@ export async function activate(context: vscode.ExtensionContext) {
193208
context.subscriptions.push(pathExecutableCache);
194209
let currentTerminalEnv: ITerminalEnvironment = process.env;
195210

196-
globalStorage = context.globalState;
211+
globalStorageUri = context.globalStorageUri;
197212
await readGlobalsCache();
198213

199214
// Get a machineId for this install (persisted per machine, not synced)

0 commit comments

Comments
 (0)