Skip to content

Expose currently selected interpreter path using API #11295

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 6 commits into from
Apr 22, 2020
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/1 Enhancements/11294.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose currently selected interpreter path using API.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"displayName": "Python",
"description": "Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more.",
"version": "2020.5.0-dev",
"featureFlags": {
"usingNewInterpreterStorage": true
},
"languageServerVersion": "0.5.30",
"publisher": "ms-python",
"enableProposedApi": false,
Expand Down
32 changes: 30 additions & 2 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { isTestExecution } from './common/constants';
import { DebugAdapterNewPtvsd } from './common/experimentGroups';
import { traceError } from './common/logger';
import { IExperimentsManager } from './common/types';
import { IConfigurationService, IExperimentsManager, Resource } from './common/types';
import { getDebugpyLauncherArgs, getPtvsdLauncherScriptArgs } from './debugger/extension/adapter/remoteLaunchers';
import { IServiceContainer, IServiceManager } from './ioc/types';

Expand Down Expand Up @@ -34,15 +34,36 @@ export interface IExtensionApi {
*/
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
};
/**
* Return internal settings within the extension which are stored in VSCode storage
*/
settings: {
/**
* Returns the Python execution command corresponding to the specified resource, taking into account
* any workspace-specific settings for the workspace to which this resource belongs.
* E.g of execution commands returned could be,
* * `['<path to the interpreter set in settings>']`
* * `['<path to the interpreter selected by the extension when setting is not set>']`
* * `['conda', 'run', 'python']` which is used to run from within Conda environments.
* or something similar for some other Python environments.
* @param {Resource} [resource] A resource for which the setting is asked for.
* * When no resource is provided, the setting scoped to the first workspace folder is returned.
* * If no folder is present, it returns the global setting.
* @returns {(string[] | undefined)} When return value is `undefined`, it means no interpreter is set.
* Otherwise, join the items returned using space to construct the full execution command.
*/
getExecutionCommand(resource?: Resource): string[] | undefined;
};
}

export function buildApi(
// tslint:disable-next-line:no-any
ready: Promise<any>,
serviceManager: IServiceManager,
serviceContainer: IServiceContainer
) {
): IExtensionApi {
const experimentsManager = serviceContainer.get<IExperimentsManager>(IExperimentsManager);
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const api = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
Expand Down Expand Up @@ -71,6 +92,13 @@ export function buildApi(
waitUntilDebuggerAttaches
});
}
},
settings: {
getExecutionCommand(resource?: Resource) {
const pythonPath = configurationService.getSettings(resource).pythonPath;
// If pythonPath equals an empty string, no interpreter is set.
return pythonPath === '' ? undefined : [pythonPath];
}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ export class PythonSettings implements IPythonSettings {
}
}
if (inExperiment && this.pythonPath === DEFAULT_INTERPRETER_SETTING) {
// If no interpreter is selected, set pythonPath to an empty string.
// This is to ensure that we ask users to select an interpreter in case auto selected interpreter is not safe to select
this.pythonPath = '';
}
Expand Down
39 changes: 36 additions & 3 deletions src/test/api.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@

// tslint:disable:no-any max-func-body-length

import { expect } from 'chai';
import { assert, expect } from 'chai';
import * as path from 'path';
import { anyString, instance, mock, when } from 'ts-mockito';
import { Uri } from 'vscode';
import { buildApi } from '../client/api';
import { ConfigurationService } from '../client/common/configuration/service';
import { EXTENSION_ROOT_DIR } from '../client/common/constants';
import { ExperimentsManager } from '../client/common/experiments';
import { IExperimentsManager } from '../client/common/types';
import { IConfigurationService, IExperimentsManager } from '../client/common/types';
import { ServiceContainer } from '../client/ioc/container';
import { ServiceManager } from '../client/ioc/serviceManager';
import { IServiceContainer, IServiceManager } from '../client/ioc/types';

suite('Extension API - Debugger', () => {
suite('Extension API', () => {
const expectedLauncherPath = `${EXTENSION_ROOT_DIR.fileToCommandArgument()}/pythonFiles/ptvsd_launcher.py`;
const ptvsdPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'lib', 'python', 'debugpy', 'no_wheels', 'debugpy');
const ptvsdHost = 'somehost';
Expand All @@ -25,15 +27,46 @@ suite('Extension API - Debugger', () => {
let serviceContainer: IServiceContainer;
let serviceManager: IServiceManager;
let experimentsManager: IExperimentsManager;
let configurationService: IConfigurationService;

setup(() => {
serviceContainer = mock(ServiceContainer);
serviceManager = mock(ServiceManager);
experimentsManager = mock(ExperimentsManager);
configurationService = mock(ConfigurationService);

when(serviceContainer.get<IConfigurationService>(IConfigurationService)).thenReturn(
instance(configurationService)
);
when(serviceContainer.get<IExperimentsManager>(IExperimentsManager)).thenReturn(instance(experimentsManager));
});

test('Execution command settings API returns expected array if interpreter is set', async () => {
const resource = Uri.parse('a');
when(configurationService.getSettings(resource)).thenReturn({ pythonPath: 'settingValue' } as any);

const interpreterPath = buildApi(
Promise.resolve(),
instance(serviceManager),
instance(serviceContainer)
).settings.getExecutionCommand(resource);

assert.deepEqual(interpreterPath, ['settingValue']);
});

test('Execution command settings API returns `undefined` if interpreter is set', async () => {
const resource = Uri.parse('a');
when(configurationService.getSettings(resource)).thenReturn({ pythonPath: '' } as any);

const interpreterPath = buildApi(
Promise.resolve(),
instance(serviceManager),
instance(serviceContainer)
).settings.getExecutionCommand(resource);

expect(interpreterPath).to.equal(undefined, '');
});

test('Test debug launcher args (no-wait and not in experiment)', async () => {
const waitForAttach = false;
when(experimentsManager.inExperiment(anyString())).thenReturn(false);
Expand Down