Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/common/persistentState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi
// a simpler, alternate API
// for components to use

interface IPersistentStorage<T> {
export interface IPersistentStorage<T> {
get(): T;
set(value: T): Promise<void>;
}
Expand Down
34 changes: 31 additions & 3 deletions src/client/pythonEnvironments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -184,11 +185,38 @@ function createWorkspaceLocator(ext: ExtensionState): WorkspaceLocators {
return locators;
}

function getFromStorage(storage: IPersistentStorage<PythonEnvInfo[]>): 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<PythonEnvInfo[]>, envs: PythonEnvInfo[]): Promise<void> {
storage.set(
envs.map((e) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to clone envs otherwise it'll overwrite e.searchLocation of the existing environment object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I agree.

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<IEnvsCollectionCache> {
const storage = getGlobalStorage<PythonEnvInfo[]>(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;
}