Skip to content

Commit 8cb749c

Browse files
committed
Provide options support for refreshInterpreters API.
1 parent 262019b commit 8cb749c

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

src/client/apiTypes.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export interface ActiveInterpreterChangedParams {
108108
resource?: Uri;
109109
}
110110

111+
export interface RefreshInterpretersOptions {
112+
clearCache?: boolean;
113+
}
114+
111115
export interface IProposedExtensionAPI {
112116
environment: {
113117
/**
@@ -152,7 +156,7 @@ export interface IProposedExtensionAPI {
152156
* promise to get the updated interpreters list. If there is a refresh already going on
153157
* then it returns the promise for that refresh.
154158
*/
155-
refreshInterpreters(): Promise<string[] | undefined>;
159+
refreshInterpreters(options?: RefreshInterpretersOptions): Promise<string[] | undefined>;
156160
/**
157161
* Returns a promise for the ongoing refresh. Returns `undefined` if there are no active
158162
* refreshes going on.

src/client/proposedApi.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
InterpreterDetailsOptions,
99
InterpretersChangedParams,
1010
IProposedExtensionAPI,
11+
RefreshInterpretersOptions,
1112
} from './apiTypes';
1213
import { IConfigurationService, IInterpreterPathService, Resource } from './common/types';
1314
import { IComponentAdapter } from './interpreter/contracts';
@@ -97,8 +98,8 @@ export function buildProposedApi(
9798
setActiveInterpreter(interpreterPath: string, resource?: Resource): Promise<void> {
9899
return interpreterPathService.update(resource, ConfigurationTarget.Workspace, interpreterPath);
99100
},
100-
async refreshInterpreters(): Promise<string[] | undefined> {
101-
await discoveryApi.triggerRefresh(undefined);
101+
async refreshInterpreters(options?: RefreshInterpretersOptions): Promise<string[] | undefined> {
102+
await discoveryApi.triggerRefresh(options ? { clearCache: options.clearCache } : undefined);
102103
const paths = discoveryApi.getEnvs().map((e) => e.executable.filename);
103104
return Promise.resolve(paths);
104105
},

src/client/pythonEnvironments/base/locator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export interface IDiscoveryAPI {
182182
/**
183183
* Triggers a new refresh for query if there isn't any already running.
184184
*/
185-
triggerRefresh(query?: PythonLocatorQuery): Promise<void>;
185+
triggerRefresh(query?: PythonLocatorQuery & { clearCache?: boolean }): Promise<void>;
186186
/**
187187
* Get current list of known environments.
188188
*/

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

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export interface IEnvsCollectionCache {
5151
* validating cache.
5252
*/
5353
validateCache(): Promise<void>;
54+
55+
/**
56+
* Clears the in-memory cache. This can be used for hard refresh.
57+
*/
58+
clearCache(): Promise<void>;
5459
}
5560

5661
export type PythonEnvCompleteInfo = { hasCompleteInfo?: boolean } & PythonEnvInfo;
@@ -134,6 +139,11 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
134139
await this.persistentStorage.store(this.envs);
135140
}
136141
}
142+
143+
public clearCache(): Promise<void> {
144+
this.envs = [];
145+
return Promise.resolve();
146+
}
137147
}
138148

139149
/**

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,22 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
8181
return query ? cachedEnvs.filter(getQueryFilter(query)) : cachedEnvs;
8282
}
8383

84-
public triggerRefresh(query?: PythonLocatorQuery): Promise<void> {
84+
public triggerRefresh(query?: PythonLocatorQuery & { clearCache?: boolean }): Promise<void> {
8585
let refreshPromise = this.getRefreshPromiseForQuery(query);
8686
if (!refreshPromise) {
8787
refreshPromise = this.startRefresh(query);
8888
}
8989
return refreshPromise;
9090
}
9191

92-
private startRefresh(query: PythonLocatorQuery | undefined): Promise<void> {
92+
private startRefresh(query: (PythonLocatorQuery & { clearCache?: boolean }) | undefined): Promise<void> {
9393
const stopWatch = new StopWatch();
9494
const deferred = createDeferred<void>();
9595

96-
if (!query) {
97-
// `undefined` query means this is full refresh of environments.
96+
if (query?.clearCache) {
9897
// Trigger an event indicating that we need to clear everything and start
9998
// fresh.
99+
this.cache.clearCache();
100100
reportInterpretersChanged([{ path: undefined, type: 'clear-all' }]);
101101
}
102102
// Ensure we set this before we trigger the promise to accurately track when a refresh has started.

src/test/pythonEnvironments/base/locators/composite/envsCollectionService.unit.test.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ suite('Python envs locator - Environments Collection', async () => {
158158
}),
159159
);
160160

161-
sinon.assert.calledWithExactly(reportInterpretersChangedStub, [{ path: undefined, type: 'clear-all' }]);
162161
envs.forEach((e) => {
163162
sinon.assert.calledWithExactly(reportInterpretersChangedStub, [
164163
{
@@ -167,9 +166,7 @@ suite('Python envs locator - Environments Collection', async () => {
167166
},
168167
]);
169168
});
170-
171-
// One for each environment and one for global 'clear-all' event.
172-
sinon.assert.callCount(reportInterpretersChangedStub, envs.length + 1);
169+
sinon.assert.callCount(reportInterpretersChangedStub, envs.length);
173170
});
174171

175172
test('triggerRefresh() refreshes the collection and storage with any new environments', async () => {
@@ -204,7 +201,6 @@ suite('Python envs locator - Environments Collection', async () => {
204201
assertEnvsEqual(storage, expected);
205202

206203
const eventData = [
207-
{ path: undefined, type: 'clear-all' },
208204
{
209205
path: path.join(TEST_LAYOUT_ROOT, 'doesNotExist'),
210206
type: 'remove',
@@ -307,7 +303,6 @@ suite('Python envs locator - Environments Collection', async () => {
307303
assertEnvsEqual(envs, expected);
308304

309305
const eventData = [
310-
{ path: undefined, type: 'clear-all' },
311306
{
312307
path: path.join(TEST_LAYOUT_ROOT, 'doesNotExist'),
313308
type: 'remove',
@@ -404,7 +399,6 @@ suite('Python envs locator - Environments Collection', async () => {
404399
);
405400

406401
const eventData = [
407-
{ path: undefined, type: 'clear-all' },
408402
{
409403
path: path.join(TEST_LAYOUT_ROOT, 'doesNotExist'),
410404
type: 'remove',
@@ -447,7 +441,6 @@ suite('Python envs locator - Environments Collection', async () => {
447441
expect(isFired).to.equal(true);
448442

449443
const eventData = [
450-
{ path: undefined, type: 'clear-all' },
451444
{
452445
path: path.join(TEST_LAYOUT_ROOT, 'conda1', 'python.exe'),
453446
type: 'add',
@@ -614,12 +607,6 @@ suite('Python envs locator - Environments Collection', async () => {
614607
downstreamEvents.sort((a, b) => (a.type && b.type ? a.type?.localeCompare(b.type) : 0)),
615608
);
616609

617-
[
618-
{ path: undefined, type: 'clear-all' },
619-
{ path: undefined, type: 'clear-all' },
620-
].forEach((d) => {
621-
sinon.assert.calledWithExactly(reportInterpretersChangedStub, [d]);
622-
});
623-
sinon.assert.calledTwice(reportInterpretersChangedStub);
610+
sinon.assert.notCalled(reportInterpretersChangedStub);
624611
});
625612
});

0 commit comments

Comments
 (0)