Skip to content

Do not perform pipenv interpreter discovery on activation #11369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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/2 Fixes/11127.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not perform pipenv interpreter discovery on extension activation.
2 changes: 1 addition & 1 deletion src/client/activation/activationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
this.experiments.sendTelemetryIfInExperiment(DeprecatePythonPath.control);

// Get latest interpreter list in the background.
this.interpreterService.getInterpreters(resource).ignoreErrors();
this.interpreterService.getInterpreters(resource, { onActivation: true }).ignoreErrors();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class InvalidMacPythonInterpreterService extends BaseDiagnosticsService {
return [];
}

const interpreters = await this.interpreterService.getInterpreters(resource);
const interpreters = await this.interpreterService.getInterpreters(resource, { onActivation: true });
if (interpreters.filter((i) => !this.helper.isMacDefaultPythonPath(i.path)).length === 0) {
return [
new InvalidMacPythonInterpreterDiagnostic(
Expand Down
10 changes: 6 additions & 4 deletions src/client/datascience/kernel-launcher/kernelFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ export class KernelFinder implements IKernelFinder {
}

const diskSearch = this.findDiskPath(kernelName);
const interpreterSearch = this.interpreterLocator.getInterpreters(resource, false).then((interpreters) => {
const interpreterPaths = interpreters.map((interp) => interp.sysPrefix);
return this.findInterpreterPath(interpreterPaths, kernelName);
});
const interpreterSearch = this.interpreterLocator
.getInterpreters(resource, { ignoreCache: false })
.then((interpreters) => {
const interpreterPaths = interpreters.map((interp) => interp.sysPrefix);
return this.findInterpreterPath(interpreterPaths, kernelName);
});

let result = await Promise.race([diskSearch, interpreterSearch]);
if (!result) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/interpreter/autoSelection/rules/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SystemWideInterpretersAutoSelectionRule extends BaseRuleService {
resource: Resource,
manager?: IInterpreterAutoSelectionService
): Promise<NextAction> {
const interpreters = await this.interpreterService.getInterpreters(resource);
const interpreters = await this.interpreterService.getInterpreters(resource, { onActivation: true });
// Exclude non-local interpreters.
const filteredInterpreters = interpreters.filter(
(int) =>
Expand Down
29 changes: 9 additions & 20 deletions src/client/interpreter/autoSelection/rules/workspaceEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { OSType } from '../../../common/utils/platform';
import {
IInterpreterHelper,
IInterpreterLocatorService,
PIPENV_SERVICE,
PythonInterpreter,
WORKSPACE_VIRTUAL_ENV_SERVICE
} from '../../contracts';
Expand All @@ -31,9 +30,6 @@ export class WorkspaceVirtualEnvInterpretersAutoSelectionRule extends BaseRuleSe
@inject(IPlatformService) private readonly platform: IPlatformService,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IInterpreterLocatorService)
@named(PIPENV_SERVICE)
private readonly pipEnvInterpreterLocator: IInterpreterLocatorService,
@inject(IInterpreterLocatorService)
@named(WORKSPACE_VIRTUAL_ENV_SERVICE)
private readonly workspaceVirtualEnvInterpreterLocator: IInterpreterLocatorService,
@inject(IExperimentsManager) private readonly experiments: IExperimentsManager,
Expand All @@ -59,25 +55,16 @@ export class WorkspaceVirtualEnvInterpretersAutoSelectionRule extends BaseRuleSe
if (pythonPathInConfig.workspaceFolderValue || pythonPathInConfig.workspaceValue) {
return NextAction.runNextRule;
}
const pipEnvPromise = createDeferredFromPromise(
this.pipEnvInterpreterLocator.getInterpreters(workspacePath.folderUri, true)
);
const virtualEnvPromise = createDeferredFromPromise(
this.getWorkspaceVirtualEnvInterpreters(workspacePath.folderUri)
);

// Use only one, we currently do not have support for both pipenv and virtual env in same workspace.
// If users have this, then theu can specify which one is to be used.
const interpreters = await Promise.race([pipEnvPromise.promise, virtualEnvPromise.promise]);
let bestInterpreter: PythonInterpreter | undefined;
if (Array.isArray(interpreters) && interpreters.length > 0) {
bestInterpreter = this.helper.getBestInterpreter(interpreters);
} else {
const [pipEnv, virtualEnv] = await Promise.all([pipEnvPromise.promise, virtualEnvPromise.promise]);
const pipEnvList = Array.isArray(pipEnv) ? pipEnv : [];
const virtualEnvList = Array.isArray(virtualEnv) ? virtualEnv : [];
bestInterpreter = this.helper.getBestInterpreter(pipEnvList.concat(virtualEnvList));
}
const interpreters = await virtualEnvPromise.promise;
const bestInterpreter =
Array.isArray(interpreters) && interpreters.length > 0
? this.helper.getBestInterpreter(interpreters)
: undefined;

if (bestInterpreter && manager) {
await super.cacheSelectedInterpreter(workspacePath.folderUri, bestInterpreter);
await manager.setWorkspaceInterpreter(workspacePath.folderUri!, bestInterpreter);
Expand All @@ -99,7 +86,9 @@ export class WorkspaceVirtualEnvInterpretersAutoSelectionRule extends BaseRuleSe
return;
}
// Now check virtual environments under the workspace root
const interpreters = await this.workspaceVirtualEnvInterpreterLocator.getInterpreters(resource, true);
const interpreters = await this.workspaceVirtualEnvInterpreterLocator.getInterpreters(resource, {
ignoreCache: true
});
const workspacePath =
this.platform.osType === OSType.Windows
? workspaceFolder.uri.fsPath.toUpperCase()
Expand Down
11 changes: 9 additions & 2 deletions src/client/interpreter/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ export const IVirtualEnvironmentsSearchPathProvider = Symbol('IVirtualEnvironmen
export interface IVirtualEnvironmentsSearchPathProvider {
getSearchPaths(resource?: Uri): Promise<string[]>;
}

export type GetInterpreterOptions = {
onActivation?: boolean;
};

export type GetInterpreterLocatorOptions = GetInterpreterOptions & { ignoreCache?: boolean };

export const IInterpreterLocatorService = Symbol('IInterpreterLocatorService');

export interface IInterpreterLocatorService extends Disposable {
readonly onLocating: Event<Promise<PythonInterpreter[]>>;
readonly hasInterpreters: Promise<boolean>;
getInterpreters(resource?: Uri, ignoreCache?: boolean): Promise<PythonInterpreter[]>;
getInterpreters(resource?: Uri, options?: GetInterpreterLocatorOptions): Promise<PythonInterpreter[]>;
}

export type CondaInfo = {
Expand Down Expand Up @@ -91,7 +98,7 @@ export interface IInterpreterService {
onDidChangeInterpreter: Event<void>;
onDidChangeInterpreterInformation: Event<PythonInterpreter>;
hasInterpreters: Promise<boolean>;
getInterpreters(resource?: Uri): Promise<PythonInterpreter[]>;
getInterpreters(resource?: Uri, options?: GetInterpreterOptions): Promise<PythonInterpreter[]>;
getActiveInterpreter(resource?: Uri): Promise<PythonInterpreter | undefined>;
getInterpreterDetails(pythonPath: string, resoure?: Uri): Promise<undefined | PythonInterpreter>;
refresh(resource: Resource): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions src/client/interpreter/interpreterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IServiceContainer } from '../ioc/types';
import { captureTelemetry } from '../telemetry';
import { EventName } from '../telemetry/constants';
import {
GetInterpreterOptions,
IInterpreterDisplay,
IInterpreterHelper,
IInterpreterLocatorService,
Expand Down Expand Up @@ -112,8 +113,8 @@ export class InterpreterService implements Disposable, IInterpreterService {
}

@captureTelemetry(EventName.PYTHON_INTERPRETER_DISCOVERY, { locator: 'all' }, true)
public async getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> {
const interpreters = await this.locator.getInterpreters(resource);
public async getInterpreters(resource?: Uri, options?: GetInterpreterOptions): Promise<PythonInterpreter[]> {
const interpreters = await this.locator.getInterpreters(resource, options);
await Promise.all(
interpreters
.filter((item) => !item.displayName)
Expand Down
13 changes: 7 additions & 6 deletions src/client/interpreter/locators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CONDA_ENV_FILE_SERVICE,
CONDA_ENV_SERVICE,
CURRENT_PATH_SERVICE,
GetInterpreterLocatorOptions,
GLOBAL_VIRTUAL_ENV_SERVICE,
IInterpreterLocatorHelper,
IInterpreterLocatorService,
Expand Down Expand Up @@ -73,8 +74,8 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
* interpreters.
*/
@traceDecorators.verbose('Get Interpreters')
public async getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> {
const locators = this.getLocators();
public async getInterpreters(resource?: Uri, options?: GetInterpreterLocatorOptions): Promise<PythonInterpreter[]> {
const locators = this.getLocators(options);
const promises = locators.map(async (provider) => provider.getInterpreters(resource));
locators.forEach((locator) => {
locator.hasInterpreters
Expand All @@ -100,23 +101,23 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
*
* The locators are pulled from the registry.
*/
private getLocators(): IInterpreterLocatorService[] {
private getLocators(options?: GetInterpreterLocatorOptions): IInterpreterLocatorService[] {
// The order of the services is important.
// The order is important because the data sources at the bottom of the list do not contain all,
// the information about the interpreters (e.g. type, environment name, etc).
// This way, the items returned from the top of the list will win, when we combine the items returned.
const keys: [string, OSType | undefined][] = [
const keys: [string | undefined, OSType | undefined][] = [
[WINDOWS_REGISTRY_SERVICE, OSType.Windows],
[CONDA_ENV_SERVICE, undefined],
[CONDA_ENV_FILE_SERVICE, undefined],
[PIPENV_SERVICE, undefined],
options?.onActivation ? [undefined, undefined] : [PIPENV_SERVICE, undefined],
[GLOBAL_VIRTUAL_ENV_SERVICE, undefined],
[WORKSPACE_VIRTUAL_ENV_SERVICE, undefined],
[KNOWN_PATH_SERVICE, undefined],
[CURRENT_PATH_SERVICE, undefined]
];
return keys
.filter((item) => item[1] === undefined || item[1] === this.platform.osType)
.filter((item) => item[0] !== undefined && (item[1] === undefined || item[1] === this.platform.osType))
.map((item) => this.serviceContainer.get<IInterpreterLocatorService>(IInterpreterLocatorService, item[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { StopWatch } from '../../../common/utils/stopWatch';
import { IServiceContainer } from '../../../ioc/types';
import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../telemetry/constants';
import { IInterpreterLocatorService, IInterpreterWatcher, PythonInterpreter } from '../../contracts';
import {
GetInterpreterLocatorOptions,
IInterpreterLocatorService,
IInterpreterWatcher,
PythonInterpreter
} from '../../contracts';

/**
* This class exists so that the interpreter fetching can be cached in between tests. Normally
Expand Down Expand Up @@ -82,10 +87,10 @@ export abstract class CacheableLocatorService implements IInterpreterLocatorServ
}
public abstract dispose(): void;
@traceDecorators.verbose('Get Interpreters in CacheableLocatorService')
public async getInterpreters(resource?: Uri, ignoreCache?: boolean): Promise<PythonInterpreter[]> {
public async getInterpreters(resource?: Uri, options?: GetInterpreterLocatorOptions): Promise<PythonInterpreter[]> {
const cacheKey = this.getCacheKey(resource);
let deferred = this.promisesPerResource.get(cacheKey);
if (!deferred || ignoreCache) {
if (!deferred || options?.ignoreCache) {
deferred = createDeferred<PythonInterpreter[]>();
this.promisesPerResource.set(cacheKey, deferred);

Expand Down Expand Up @@ -125,7 +130,7 @@ export abstract class CacheableLocatorService implements IInterpreterLocatorServ
return deferred.promise;
}

const cachedInterpreters = ignoreCache ? undefined : this.getCachedInterpreters(resource);
const cachedInterpreters = options?.ignoreCache ? undefined : this.getCachedInterpreters(resource);
return Array.isArray(cachedInterpreters) ? cachedInterpreters : deferred.promise;
}
protected async addHandlersForInterpreterWatchers(cacheKey: string, resource: Uri | undefined): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion src/client/startupTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ async function getActivationTelemetryProps(serviceContainer: IServiceContainer):
.then((ver) => (ver ? ver.raw : ''))
.catch<string>(() => ''),
interpreterService.getActiveInterpreter().catch<PythonInterpreter | undefined>(() => undefined),
interpreterService.getInterpreters(mainWorkspaceUri).catch<PythonInterpreter[]>(() => [])
interpreterService
.getInterpreters(mainWorkspaceUri, { onActivation: true })
.catch<PythonInterpreter[]>(() => [])
]);
const workspaceFolderCount = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.length : 0;
const pythonVersion = interpreter && interpreter.version ? interpreter.version.raw : undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/test/activation/activationManager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ suite('Activation Manager', () => {
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(folder2);
when(activationService1.activate(resource)).thenResolve();
when(activationService2.activate(resource)).thenResolve();
when(interpreterService.getInterpreters(anything())).thenResolve();
when(interpreterService.getInterpreters(anything(), anything())).thenResolve();
autoSelection
.setup((a) => a.autoSelectInterpreter(resource))
.returns(() => Promise.resolve())
Expand Down Expand Up @@ -232,7 +232,7 @@ suite('Activation Manager', () => {
const resource = Uri.parse('two');
when(activationService1.activate(resource)).thenResolve();
when(activationService2.activate(resource)).thenResolve();
when(interpreterService.getInterpreters(anything())).thenResolve();
when(interpreterService.getInterpreters(anything(), anything())).thenResolve();
autoSelection
.setup((a) => a.autoSelectInterpreter(resource))
.returns(() => Promise.resolve())
Expand All @@ -252,7 +252,7 @@ suite('Activation Manager', () => {
const resource = Uri.parse('two');
when(activationService1.activate(resource)).thenResolve();
when(activationService2.activate(resource)).thenResolve();
when(interpreterService.getInterpreters(anything())).thenResolve();
when(interpreterService.getInterpreters(anything(), anything())).thenResolve();
when(experiments.inExperiment(DeprecatePythonPath.experiment)).thenReturn(true);
interpreterPathService
.setup((i) => i.copyOldInterpreterStorageValuesToNew(resource))
Expand All @@ -278,7 +278,7 @@ suite('Activation Manager', () => {
const resource = Uri.parse('two');
when(activationService1.activate(resource)).thenResolve();
when(activationService2.activate(resource)).thenResolve();
when(interpreterService.getInterpreters(anything())).thenResolve();
when(interpreterService.getInterpreters(anything(), anything())).thenResolve();
autoSelection
.setup((a) => a.autoSelectInterpreter(resource))
.returns(() => Promise.resolve())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ suite('Application Diagnostics - Checks Mac Python Interpreter', () => {
.returns(() => Promise.resolve(true))
.verifiable(typemoq.Times.once());
interpreterService
.setup((i) => i.getInterpreters(typemoq.It.isAny()))
.setup((i) => i.getInterpreters(typemoq.It.isAny(), { onActivation: true }))
.returns(() => Promise.resolve([{} as any]))
.verifiable(typemoq.Times.never());
interpreterService
Expand Down Expand Up @@ -204,7 +204,7 @@ suite('Application Diagnostics - Checks Mac Python Interpreter', () => {
.returns(() => Promise.resolve(true))
.verifiable(typemoq.Times.once());
interpreterService
.setup((i) => i.getInterpreters(typemoq.It.isAny()))
.setup((i) => i.getInterpreters(typemoq.It.isAny(), { onActivation: true }))
.returns(() => Promise.resolve([{} as any]))
.verifiable(typemoq.Times.never());
interpreterService
Expand Down Expand Up @@ -235,7 +235,7 @@ suite('Application Diagnostics - Checks Mac Python Interpreter', () => {
.returns(() => false)
.verifiable(typemoq.Times.once());
interpreterService
.setup((i) => i.getInterpreters(typemoq.It.isAny()))
.setup((i) => i.getInterpreters(typemoq.It.isAny(), { onActivation: true }))
.returns(() => Promise.resolve([{ path: pythonPath } as any, { path: pythonPath } as any]))
.verifiable(typemoq.Times.once());
interpreterService
Expand Down Expand Up @@ -275,7 +275,7 @@ suite('Application Diagnostics - Checks Mac Python Interpreter', () => {
.returns(() => false)
.verifiable(typemoq.Times.once());
interpreterService
.setup((i) => i.getInterpreters(typemoq.It.isAny()))
.setup((i) => i.getInterpreters(typemoq.It.isAny(), { onActivation: true }))
.returns(() =>
Promise.resolve([
{ path: pythonPath } as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ suite('Interpreters - Auto Selection - Current Path Rule', () => {
const manager = mock(InterpreterAutoSelectionService);
const resource = Uri.file('x');

when(locator.getInterpreters(resource)).thenResolve([]);
when(locator.getInterpreters(resource, anything())).thenResolve([]);

const nextAction = await rule.onAutoSelectInterpreter(resource, manager);

Expand Down
Loading