From 5cb064226ed8a6318f39f1df5390ca9e49bc1fd3 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Thu, 27 Oct 2022 16:47:39 -0700 Subject: [PATCH] Ensure that search location is stored as a string. --- src/client/common/persistentState.ts | 2 +- src/client/pythonEnvironments/index.ts | 34 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/client/common/persistentState.ts b/src/client/common/persistentState.ts index a858a8d8cf80..0d397665d96d 100644 --- a/src/client/common/persistentState.ts +++ b/src/client/common/persistentState.ts @@ -152,7 +152,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi // a simpler, alternate API // for components to use -interface IPersistentStorage { +export interface IPersistentStorage { get(): T; set(value: T): Promise; } diff --git a/src/client/pythonEnvironments/index.ts b/src/client/pythonEnvironments/index.ts index 3b518fab0ddb..fddd647c93b1 100644 --- a/src/client/pythonEnvironments/index.ts +++ b/src/client/pythonEnvironments/index.ts @@ -2,7 +2,8 @@ // Licensed under the MIT License. import * as vscode from 'vscode'; -import { getGlobalStorage } from '../common/persistentState'; +import { Uri } from 'vscode'; +import { getGlobalStorage, IPersistentStorage } from '../common/persistentState'; import { getOSType, OSType } from '../common/utils/platform'; import { ActivationResult, ExtensionState } from '../components'; import { PythonEnvInfo } from './base/info'; @@ -184,11 +185,38 @@ function createWorkspaceLocator(ext: ExtensionState): WorkspaceLocators { return locators; } +function getFromStorage(storage: IPersistentStorage): PythonEnvInfo[] { + return storage.get().map((e) => { + if (e.searchLocation) { + if (typeof e.searchLocation === 'string') { + e.searchLocation = Uri.parse(e.searchLocation); + } else if ('scheme' in e.searchLocation && 'path' in e.searchLocation) { + e.searchLocation = Uri.parse(`${e.searchLocation.scheme}://${e.searchLocation.path}`); + } + } + return e; + }); +} + +function putIntoStorage(storage: IPersistentStorage, envs: PythonEnvInfo[]): Promise { + storage.set( + envs.map((e) => { + if (e.searchLocation) { + // Make TS believe it is string. This is temporary. We need to serialize this in + // a custom way. + e.searchLocation = (e.searchLocation.toString() as unknown) as Uri; + } + return e; + }), + ); + return Promise.resolve(); +} + async function createCollectionCache(ext: ExtensionState): Promise { const storage = getGlobalStorage(ext.context, 'PYTHON_ENV_INFO_CACHE', []); const cache = await createCache({ - get: () => storage.get(), - store: async (e) => storage.set(e), + get: () => getFromStorage(storage), + store: async (e) => putIntoStorage(storage, e), }); return cache; }