Skip to content

Temporarily expose PYTHONPATH for Pylance #19899

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
merged 3 commits into from
Sep 27, 2022
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
21 changes: 20 additions & 1 deletion src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
'use strict';

import { noop } from 'lodash';
import { Uri, Event } from 'vscode';
import { IExtensionApi } from './apiTypes';
import { isTestExecution } from './common/constants';
import { IConfigurationService, Resource } from './common/types';
import { IEnvironmentVariablesProvider } from './common/variables/types';
import { getDebugpyLauncherArgs, getDebugpyPackagePath } from './debugger/extension/adapter/remoteLaunchers';
import { IInterpreterService } from './interpreter/contracts';
import { IServiceContainer, IServiceManager } from './ioc/types';
Expand All @@ -22,7 +24,17 @@ export function buildApi(
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
serviceManager.addSingleton<JupyterExtensionIntegration>(JupyterExtensionIntegration, JupyterExtensionIntegration);
const jupyterIntegration = serviceContainer.get<JupyterExtensionIntegration>(JupyterExtensionIntegration);
const api: IExtensionApi = {
const envService = serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider);
const api: IExtensionApi & {
/**
* @deprecated Temporarily exposed for Pylance until we expose this API generally. Will be removed in an
* iteration or two.
*/
pylance: {
getPythonPathVar: (resource?: Uri) => Promise<string | undefined>;
readonly onDidEnvironmentVariablesChange: Event<Uri | undefined>;
};
} = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
traceError('Failure during activation.', ex);
Expand Down Expand Up @@ -65,6 +77,13 @@ export function buildApi(
? jupyterIntegration.showDataViewer.bind(jupyterIntegration)
: (noop as any),
},
pylance: {
getPythonPathVar: async (resource?: Uri) => {
const envs = await envService.getEnvironmentVariables(resource);
return envs.PYTHONPATH;
},
onDidEnvironmentVariablesChange: envService.onDidEnvironmentVariablesChange,
},
};

// In test environment return the DI Container.
Expand Down
6 changes: 6 additions & 0 deletions src/test/api.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { buildApi } from '../client/api';
import { ConfigurationService } from '../client/common/configuration/service';
import { EXTENSION_ROOT_DIR } from '../client/common/constants';
import { IConfigurationService } from '../client/common/types';
import { IEnvironmentVariablesProvider } from '../client/common/variables/types';
import { IInterpreterService } from '../client/interpreter/contracts';
import { InterpreterService } from '../client/interpreter/interpreterService';
import { ServiceContainer } from '../client/ioc/container';
Expand All @@ -27,16 +28,21 @@ suite('Extension API', () => {
let serviceManager: IServiceManager;
let configurationService: IConfigurationService;
let interpreterService: IInterpreterService;
let environmentVariablesProvider: IEnvironmentVariablesProvider;

setup(() => {
serviceContainer = mock(ServiceContainer);
serviceManager = mock(ServiceManager);
configurationService = mock(ConfigurationService);
interpreterService = mock(InterpreterService);
environmentVariablesProvider = mock<IEnvironmentVariablesProvider>();

when(serviceContainer.get<IConfigurationService>(IConfigurationService)).thenReturn(
instance(configurationService),
);
when(serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider)).thenReturn(
instance(environmentVariablesProvider),
);
when(serviceContainer.get<IInterpreterService>(IInterpreterService)).thenReturn(instance(interpreterService));
});

Expand Down