Skip to content

Commit 00b198a

Browse files
author
Kartik Raj
authored
Fix bugs related to discovery blocking other features (#22041)
For #21755
1 parent 42cdaf3 commit 00b198a

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

src/client/common/utils/multiStepInput.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface IQuickPickParameters<T extends QuickPickItem, E = any> {
4747
totalSteps?: number;
4848
canGoBack?: boolean;
4949
items: T[];
50-
activeItem?: T | Promise<T>;
50+
activeItem?: T | ((quickPick: QuickPick<T>) => Promise<T>);
5151
placeholder: string | undefined;
5252
customButtonSetups?: QuickInputButtonSetup[];
5353
matchOnDescription?: boolean;
@@ -156,7 +156,13 @@ export class MultiStepInput<S> implements IMultiStepInput<S> {
156156
initialize(input);
157157
}
158158
if (activeItem) {
159-
input.activeItems = [await activeItem];
159+
if (typeof activeItem === 'function') {
160+
activeItem(input).then((item) => {
161+
if (input.activeItems.length === 0) {
162+
input.activeItems = [item];
163+
}
164+
});
165+
}
160166
} else {
161167
input.activeItems = [];
162168
}

src/client/interpreter/autoSelection/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
181181
return this.stateFactory.createWorkspacePersistentState(key, undefined);
182182
}
183183

184+
private getAutoSelectionQueriedOnceState(): IPersistentState<boolean | undefined> {
185+
const key = `autoSelectionInterpretersQueriedOnce`;
186+
return this.stateFactory.createWorkspacePersistentState(key, undefined);
187+
}
188+
184189
/**
185190
* Auto-selection logic:
186191
* 1. If there are cached interpreters (not the first session in this workspace)
@@ -200,7 +205,12 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
200205
});
201206
}
202207

203-
await this.interpreterService.refreshPromise;
208+
const globalQueriedState = this.getAutoSelectionQueriedOnceState();
209+
if (!globalQueriedState.value) {
210+
// Global interpreters are loaded the first time an extension loads, after which we don't need to
211+
// wait on global interpreter promise refresh.
212+
await this.interpreterService.refreshPromise;
213+
}
204214
const interpreters = this.interpreterService.getInterpreters(resource);
205215
const workspaceUri = this.interpreterHelper.getActiveWorkspaceUri(resource);
206216

@@ -215,6 +225,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
215225
}
216226

217227
queriedState.updateValue(true);
228+
globalQueriedState.updateValue(true);
218229

219230
this.didAutoSelectedInterpreterEmitter.fire();
220231
}

src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { BaseInterpreterSelectorCommand } from './base';
5050
const untildify = require('untildify');
5151

5252
export type InterpreterStateArgs = { path?: string; workspace: Resource };
53-
type QuickPickType = IInterpreterQuickPickItem | ISpecialQuickPickItem | QuickPickItem;
53+
export type QuickPickType = IInterpreterQuickPickItem | ISpecialQuickPickItem | QuickPickItem;
5454

5555
function isInterpreterQuickPickItem(item: QuickPickType): item is IInterpreterQuickPickItem {
5656
return 'interpreter' in item;
@@ -177,7 +177,7 @@ export class SetInterpreterCommand extends BaseInterpreterSelectorCommand implem
177177
items: suggestions,
178178
sortByLabel: !preserveOrderWhenFiltering,
179179
keepScrollPosition: true,
180-
activeItem: this.getActiveItem(state.workspace, suggestions), // Use a promise here to ensure quickpick is initialized synchronously.
180+
activeItem: (quickPick) => this.getActiveItem(state.workspace, quickPick), // Use a promise here to ensure quickpick is initialized synchronously.
181181
matchOnDetail: true,
182182
matchOnDescription: true,
183183
title,
@@ -277,8 +277,9 @@ export class SetInterpreterCommand extends BaseInterpreterSelectorCommand implem
277277
return getGroupedQuickPickItems(items, recommended, workspaceFolder?.uri.fsPath);
278278
}
279279

280-
private async getActiveItem(resource: Resource, suggestions: QuickPickType[]) {
280+
private async getActiveItem(resource: Resource, quickPick: QuickPick<QuickPickType>) {
281281
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
282+
const suggestions = quickPick.items;
282283
const activeInterpreterItem = suggestions.find(
283284
(i) => isInterpreterQuickPickItem(i) && i.interpreter.id === interpreter?.id,
284285
);
@@ -339,7 +340,9 @@ export class SetInterpreterCommand extends BaseInterpreterSelectorCommand implem
339340
return false;
340341
})
341342
: undefined;
342-
quickPick.activeItems = activeItem ? [activeItem] : [];
343+
if (activeItem) {
344+
quickPick.activeItems = [activeItem];
345+
}
343346
}
344347

345348
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
115115
// Do not trigger another refresh if a refresh has previously finished.
116116
return Promise.resolve();
117117
}
118-
refreshPromise = this.startRefresh(query);
118+
refreshPromise = this.startRefresh(query).then(() => this.sendTelemetry(query, stopWatch));
119119
}
120-
return refreshPromise.then(() => this.sendTelemetry(query, stopWatch));
120+
return refreshPromise;
121121
}
122122

123123
private startRefresh(query: PythonLocatorQuery | undefined): Promise<void> {

src/test/configuration/interpreterSelector/commands/setInterpreter.unit.test.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import {
3232
EnvGroups,
3333
InterpreterStateArgs,
34+
QuickPickType,
3435
SetInterpreterCommand,
3536
} from '../../../../client/interpreter/configuration/interpreterSelector/commands/setInterpreter';
3637
import {
@@ -265,8 +266,14 @@ suite('Set Interpreter Command', () => {
265266
delete actualParameters!.initialize;
266267
delete actualParameters!.customButtonSetups;
267268
delete actualParameters!.onChangeItem;
268-
const activeItem = await actualParameters!.activeItem;
269-
assert.deepStrictEqual(activeItem, recommended);
269+
if (typeof actualParameters!.activeItem === 'function') {
270+
const activeItem = await actualParameters!.activeItem(({ items: suggestions } as unknown) as QuickPick<
271+
QuickPickType
272+
>);
273+
assert.deepStrictEqual(activeItem, recommended);
274+
} else {
275+
assert(false, 'Not a function');
276+
}
270277
delete actualParameters!.activeItem;
271278
assert.deepStrictEqual(actualParameters, expectedParameters, 'Params not equal');
272279
});
@@ -308,8 +315,14 @@ suite('Set Interpreter Command', () => {
308315
delete actualParameters!.initialize;
309316
delete actualParameters!.customButtonSetups;
310317
delete actualParameters!.onChangeItem;
311-
const activeItem = await actualParameters!.activeItem;
312-
assert.deepStrictEqual(activeItem, noPythonInstalled);
318+
if (typeof actualParameters!.activeItem === 'function') {
319+
const activeItem = await actualParameters!.activeItem(({ items: suggestions } as unknown) as QuickPick<
320+
QuickPickType
321+
>);
322+
assert.deepStrictEqual(activeItem, noPythonInstalled);
323+
} else {
324+
assert(false, 'Not a function');
325+
}
313326
delete actualParameters!.activeItem;
314327
assert.deepStrictEqual(actualParameters, expectedParameters, 'Params not equal');
315328
});
@@ -666,8 +679,14 @@ suite('Set Interpreter Command', () => {
666679
delete actualParameters!.initialize;
667680
delete actualParameters!.customButtonSetups;
668681
delete actualParameters!.onChangeItem;
669-
const activeItem = await actualParameters!.activeItem;
670-
assert.deepStrictEqual(activeItem, recommended);
682+
if (typeof actualParameters!.activeItem === 'function') {
683+
const activeItem = await actualParameters!.activeItem(({ items: suggestions } as unknown) as QuickPick<
684+
QuickPickType
685+
>);
686+
assert.deepStrictEqual(activeItem, recommended);
687+
} else {
688+
assert(false, 'Not a function');
689+
}
671690
delete actualParameters!.activeItem;
672691

673692
assert.deepStrictEqual(actualParameters, expectedParameters, 'Params not equal');

0 commit comments

Comments
 (0)