Skip to content

Commit 688213e

Browse files
author
Kartik Raj
authored
Resolve environments using cache if cache has complete info (microsoft#17474)
* If environments in cache have complete info, use it regardless of whether there is a refresh going on * News entry * Update comment
1 parent c79b6e3 commit 688213e

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

news/1 Enhancements/17474.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Resolve environments using cache if cache has complete env info.

src/client/pythonEnvironments/base/locators/composite/envsCollectionCache.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export interface IEnvsCollectionCache {
3636
addEnv(env: PythonEnvInfo): void;
3737

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

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

56+
type PythonEnvCompleteInfo = { hasCompleteInfo?: boolean } & PythonEnvInfo;
57+
5658
interface IPersistentStorage {
5759
load(): Promise<PythonEnvInfo[] | undefined>;
5860
store(envs: PythonEnvInfo[]): Promise<void>;
@@ -63,7 +65,7 @@ interface IPersistentStorage {
6365
*/
6466
export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionChangedEvent>
6567
implements IEnvsCollectionCache {
66-
private envs: PythonEnvInfo[] = [];
68+
private envs: PythonEnvCompleteInfo[] = [];
6769

6870
constructor(private readonly persistentStorage: IPersistentStorage) {
6971
super();
@@ -103,8 +105,9 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
103105
}
104106
}
105107

106-
public getEnv(executablePath: string): PythonEnvInfo | undefined {
107-
return this.envs.find((e) => areSameEnv(e, executablePath));
108+
public getCompleteInfo(executablePath: string): PythonEnvInfo | undefined {
109+
const env = this.envs.find((e) => areSameEnv(e, executablePath));
110+
return env?.hasCompleteInfo ? env : undefined;
108111
}
109112

110113
public async clearAndReloadFromStorage(): Promise<void> {
@@ -114,6 +117,9 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
114117
public async flush(): Promise<void> {
115118
if (this.envs.length) {
116119
traceInfo('Environments added to cache', JSON.stringify(this.envs));
120+
this.envs.forEach((e) => {
121+
e.hasCompleteInfo = true;
122+
});
117123
await this.persistentStorage.store(this.envs);
118124
}
119125
}

src/client/pythonEnvironments/base/locators/composite/envsCollectionService.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
5454
}
5555

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

0 commit comments

Comments
 (0)