Skip to content

Commit a7615cd

Browse files
author
Kartik Raj
committed
Remove uses of getAll where it's not needed
1 parent ef6354d commit a7615cd

File tree

8 files changed

+43
-42
lines changed

8 files changed

+43
-42
lines changed

src/client/activation/activationManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
7878

7979
// Get latest interpreter list in the background.
8080
addItemsToRunAfterActivation(() => {
81-
this.interpreterService.getAllInterpreters(resource).ignoreErrors();
81+
this.interpreterService.getInterpreters(resource).ignoreErrors();
8282
});
8383

8484
await sendActivationTelemetry(this.fileSystem, this.workspaceService, resource);

src/client/application/diagnostics/checks/macPythonInterpreter.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class InvalidMacPythonInterpreterService extends BaseDiagnosticsService {
8787
return [];
8888
}
8989

90-
const hasInterpreters = await this.interpreterService.hasInterpreters;
90+
const hasInterpreters = await this.interpreterService.hasInterpreters();
9191
if (!hasInterpreters) {
9292
return [];
9393
}
@@ -104,17 +104,20 @@ export class InvalidMacPythonInterpreterService extends BaseDiagnosticsService {
104104
return [];
105105
}
106106

107-
const interpreters = await this.interpreterService.getAllInterpreters(resource);
108-
for (const info of interpreters) {
109-
if (!(await this.helper.isMacDefaultPythonPath(info.path))) {
110-
return [
111-
new InvalidMacPythonInterpreterDiagnostic(
112-
DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic,
113-
resource,
114-
),
115-
];
116-
}
107+
if (
108+
await this.interpreterService.hasInterpreters((e) =>
109+
this.helper.isMacDefaultPythonPath(e.path).then((x) => !x),
110+
)
111+
) {
112+
// If non-mac default interpreters exist.
113+
return [
114+
new InvalidMacPythonInterpreterDiagnostic(
115+
DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic,
116+
resource,
117+
),
118+
];
117119
}
120+
118121
return [
119122
new InvalidMacPythonInterpreterDiagnostic(
120123
DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic,

src/client/application/diagnostics/checks/pythonInterpreter.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ export class InvalidPythonInterpreterService extends BaseDiagnosticsService {
6767
}
6868

6969
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
70-
// hasInterpreters being false can mean one of 2 things:
71-
// 1. getInterpreters hasn't returned any interpreters;
72-
// 2. getInterpreters hasn't run yet.
73-
// We want to make sure that false comes from 1, so we're adding this fix until we refactor interpreter discovery.
74-
// Also see https://github.com/microsoft/vscode-python/issues/3023.
75-
const hasInterpreters = await interpreterService.hasInterpreters;
70+
const hasInterpreters = await interpreterService.hasInterpreters();
7671

7772
if (!hasInterpreters) {
7873
return [new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.NoPythonInterpretersDiagnostic, resource)];

src/client/debugger/extension/adapter/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
114114
return interpreter.path;
115115
}
116116

117-
await this.interpreterService.hasInterpreters; // Wait until we know whether we have an interpreter
117+
await this.interpreterService.hasInterpreters(); // Wait until we know whether we have an interpreter
118118
const interpreters = await this.interpreterService.getInterpreters(resourceUri);
119119
if (interpreters.length === 0) {
120120
this.notifySelectInterpreter().ignoreErrors();

src/client/interpreter/contracts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface IComponentAdapter {
4343
// VirtualEnvPrompt
4444
onDidCreate(resource: Resource, callback: () => void): Disposable;
4545
// IInterpreterLocatorService
46-
hasInterpreters: Promise<boolean>;
46+
hasInterpreters(filter?: (e: PythonEnvironment) => Promise<boolean>): Promise<boolean>;
4747
getInterpreters(resource?: Uri, source?: PythonEnvSource[]): PythonEnvironment[];
4848

4949
// WorkspaceVirtualEnvInterpretersAutoSelectionRule
@@ -111,7 +111,7 @@ export interface IInterpreterService {
111111
onDidChangeInterpreterConfiguration: Event<Uri | undefined>;
112112
onDidChangeInterpreter: Event<void>;
113113
onDidChangeInterpreterInformation: Event<PythonEnvironment>;
114-
hasInterpreters: Promise<boolean>;
114+
hasInterpreters(filter?: (e: PythonEnvironment) => Promise<boolean>): Promise<boolean>;
115115
getInterpreters(resource?: Uri, options?: GetInterpreterOptions): Promise<PythonEnvironment[]>;
116116
getAllInterpreters(resource?: Uri, options?: GetInterpreterOptions): Promise<PythonEnvironment[]>;
117117
getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined>;

src/client/interpreter/interpreterService.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ type StoredPythonEnvironment = PythonEnvironment & { store?: boolean };
4242

4343
@injectable()
4444
export class InterpreterService implements Disposable, IInterpreterService {
45-
public get hasInterpreters(): Promise<boolean> {
45+
public async hasInterpreters(
46+
filter: (e: PythonEnvironment) => Promise<boolean> = async () => true,
47+
): Promise<boolean> {
4648
return inDiscoveryExperiment(this.experimentService).then((inExp) => {
4749
if (inExp) {
48-
return this.pyenvs.hasInterpreters;
50+
return this.pyenvs.hasInterpreters(filter);
4951
}
5052
const locator = this.serviceContainer.get<IInterpreterLocatorService>(
5153
IInterpreterLocatorService,

src/client/pythonEnvironments/legacyIOC.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { IExtensionSingleActivationService } from '../activation/types';
6868
import { EnvironmentInfoServiceQueuePriority, getEnvironmentInfoService } from './base/info/environmentInfoService';
6969
import { createDeferred } from '../common/utils/async';
7070
import { PythonEnvCollectionChangedEvent } from './base/watcher';
71+
import { asyncFilter } from '../common/utils/arrayUtils';
7172

7273
const convertedKinds = new Map(
7374
Object.entries({
@@ -138,18 +139,10 @@ class ComponentAdapter implements IComponentAdapter {
138139

139140
private readonly refreshed = new vscode.EventEmitter<void>();
140141

141-
private readonly onAddedToCollection = createDeferred();
142-
143142
constructor(
144143
// The adapter only wraps one thing: the component API.
145144
private readonly api: IDiscoveryAPI,
146-
) {
147-
this.api.onChanged((e: PythonEnvCollectionChangedEvent) => {
148-
if (e.update) {
149-
this.onAddedToCollection.resolve();
150-
}
151-
});
152-
}
145+
) {}
153146

154147
public triggerRefresh(query?: PythonLocatorQuery): Promise<void> {
155148
return this.api.triggerRefresh(query);
@@ -260,17 +253,27 @@ class ComponentAdapter implements IComponentAdapter {
260253
}
261254

262255
// Implements IInterpreterLocatorService
263-
public get hasInterpreters(): Promise<boolean> {
256+
public async hasInterpreters(
257+
filter: (e: PythonEnvironment) => Promise<boolean> = async () => true,
258+
): Promise<boolean> {
259+
const onAddedToCollection = createDeferred();
260+
// Watch for collection changed events.
261+
this.api.onChanged(async (e: PythonEnvCollectionChangedEvent) => {
262+
if (e.update) {
263+
if (await filter(convertEnvInfo(e.update))) {
264+
onAddedToCollection.resolve();
265+
}
266+
}
267+
});
264268
const initialEnvs = this.api.getEnvs();
265269
if (initialEnvs.length > 0) {
266-
return Promise.resolve(true);
270+
return true;
267271
}
268272
// We should already have initiated discovery. Wait for an env to be added
269273
// to the collection until the refresh has finished.
270-
return Promise.race([this.onAddedToCollection.promise, this.api.refreshPromise]).then(() => {
271-
const envs = this.api.getEnvs();
272-
return envs.length > 0;
273-
});
274+
await Promise.race([onAddedToCollection.promise, this.api.refreshPromise]);
275+
const envs = await asyncFilter(this.api.getEnvs(), (e) => filter(convertEnvInfo(e)));
276+
return envs.length > 0;
274277
}
275278

276279
public getInterpreters(resource?: vscode.Uri, source?: PythonEnvSource[]): PythonEnvironment[] {

src/client/startupTelemetry.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,19 @@ async function getActivationTelemetryProps(serviceContainer: IServiceContainer):
105105
? workspaceService.workspaceFolders![0].uri
106106
: undefined;
107107
const settings = configurationService.getSettings(mainWorkspaceUri);
108-
const [condaVersion, interpreter, interpreters] = await Promise.all([
108+
const [condaVersion, interpreter, hasPython3] = await Promise.all([
109109
condaLocator
110110
.getCondaVersion()
111111
.then((ver) => (ver ? ver.raw : ''))
112112
.catch<string>(() => ''),
113113
interpreterService.getActiveInterpreter().catch<PythonEnvironment | undefined>(() => undefined),
114-
interpreterService.getAllInterpreters(mainWorkspaceUri).catch<PythonEnvironment[]>(() => []),
114+
interpreterService.hasInterpreters(async (item) => item.version?.major === 3),
115115
]);
116116
const workspaceFolderCount = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.length : 0;
117117
const pythonVersion = interpreter && interpreter.version ? interpreter.version.raw : undefined;
118118
const interpreterType = interpreter ? interpreter.envType : undefined;
119119
const usingUserDefinedInterpreter = hasUserDefinedPythonPath(mainWorkspaceUri, serviceContainer);
120120
const usingGlobalInterpreter = isUsingGlobalInterpreterInWorkspace(settings.pythonPath, serviceContainer);
121-
const hasPython3 =
122-
interpreters!.filter((item) => (item && item.version ? item.version.major === 3 : false)).length > 0;
123121

124122
return {
125123
condaVersion,

0 commit comments

Comments
 (0)