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
1 change: 1 addition & 0 deletions news/1 Enhancements/17474.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resolve environments using cache if cache has complete env info.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export interface IEnvsCollectionCache {
addEnv(env: PythonEnvInfo): void;

/**
* Return cached environment information for a given interpreter path if it exists,
* otherwise return `undefined`.
* Return cached environment information for a given interpreter path if it exists and
* has complete info, otherwise return `undefined`.
*/
getEnv(path: string): PythonEnvInfo | undefined;
getCompleteInfo(path: string): PythonEnvInfo | undefined;

/**
* Writes the content of the in-memory cache to persistent storage.
Expand All @@ -53,6 +53,8 @@ export interface IEnvsCollectionCache {
validateCache(): Promise<void>;
}

type PythonEnvCompleteInfo = { hasCompleteInfo?: boolean } & PythonEnvInfo;

interface IPersistentStorage {
load(): Promise<PythonEnvInfo[] | undefined>;
store(envs: PythonEnvInfo[]): Promise<void>;
Expand All @@ -63,7 +65,7 @@ interface IPersistentStorage {
*/
export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionChangedEvent>
implements IEnvsCollectionCache {
private envs: PythonEnvInfo[] = [];
private envs: PythonEnvCompleteInfo[] = [];

constructor(private readonly persistentStorage: IPersistentStorage) {
super();
Expand Down Expand Up @@ -103,8 +105,9 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
}
}

public getEnv(executablePath: string): PythonEnvInfo | undefined {
return this.envs.find((e) => areSameEnv(e, executablePath));
public getCompleteInfo(executablePath: string): PythonEnvInfo | undefined {
const env = this.envs.find((e) => areSameEnv(e, executablePath));
return env?.hasCompleteInfo ? env : undefined;
}

public async clearAndReloadFromStorage(): Promise<void> {
Expand All @@ -114,6 +117,9 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
public async flush(): Promise<void> {
if (this.envs.length) {
traceInfo('Environments added to cache', JSON.stringify(this.envs));
this.envs.forEach((e) => {
e.hasCompleteInfo = true;
});
await this.persistentStorage.store(this.envs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
}

public async resolveEnv(executablePath: string): Promise<PythonEnvInfo | undefined> {
const cachedEnv = this.cache.getEnv(executablePath);
// Envs in cache may have incomplete info when a refresh is happening, so
// do not rely on cache in those cases.
if (cachedEnv && this.refreshPromises.size === 0) {
// Note cache may have incomplete info when a refresh is happening.
// This API is supposed to return complete info by definition, so
// only use cache if it has complete info on an environment.
const cachedEnv = this.cache.getCompleteInfo(executablePath);
if (cachedEnv) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Now that 3rd party extensions can also trigger a refresh, refresh can be going on any moment, we want to ensure we still use the cache in that case.

return cachedEnv;
}
const resolved = await this.locator.resolveEnv(executablePath);
Expand Down