Skip to content

Commit

Permalink
Ensure python is always requested using uri. (microsoft#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Jun 1, 2023
1 parent c29bfb0 commit 4a7339f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
17 changes: 4 additions & 13 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
import { DEBUG_SERVER_SCRIPT_PATH, SERVER_SCRIPT_PATH } from './constants';
import { traceError, traceInfo, traceVerbose } from './logging';
import { getDebuggerPath } from './python';
import { getExtensionSettings, getGlobalSettings, getWorkspaceSettings, ISettings } from './settings';
import { getLSClientTraceLevel, getProjectRoot } from './utilities';
import { getExtensionSettings, getGlobalSettings, ISettings } from './settings';
import { getLSClientTraceLevel } from './utilities';
import { isVirtualWorkspace } from './vscodeapi';
import { updateStatus } from './status';
import { unregisterEmptyFormatter } from './nullFormatter';
Expand Down Expand Up @@ -81,6 +81,7 @@ async function createServer(

let _disposables: Disposable[] = [];
export async function restartServer(
workspaceSetting: ISettings,
serverId: string,
serverName: string,
outputChannel: LogOutputChannel,
Expand All @@ -97,22 +98,12 @@ export async function restartServer(
_disposables = [];
}
updateStatus(undefined, LanguageStatusSeverity.Information, true);
const projectRoot = await getProjectRoot();
const workspaceSetting = await getWorkspaceSettings(serverId, projectRoot, true);
if (workspaceSetting.interpreter.length === 0) {
updateStatus(l10n.t('Please select a Python interpreter.'), LanguageStatusSeverity.Error);
traceError(
'Python interpreter missing:\r\n' +
'[Option 1] Select python interpreter using the ms-python.python.\r\n' +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n`,
);
return undefined;
}

const newLSClient = await createServer(workspaceSetting, serverId, serverName, outputChannel, {
settings: await getExtensionSettings(serverId, true),
globalSettings: await getGlobalSettings(serverId, false),
});

traceInfo(`Server: Start requested.`);
_disposables.push(
newLSClient.onDidChangeState((e) => {
Expand Down
15 changes: 15 additions & 0 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { ConfigurationChangeEvent, ConfigurationScope, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
import { traceLog } from './logging';

export interface ISettings {
cwd: string;
Expand Down Expand Up @@ -85,7 +86,21 @@ export async function getWorkspaceSettings(
if (includeInterpreter) {
interpreter = getInterpreterFromSetting(namespace, workspace) ?? [];
if (interpreter.length === 0) {
traceLog(`No interpreter found from setting ${namespace}.interpreter`);
traceLog(`Getting interpreter from ms-python.python extension for workspace ${workspace.uri.fsPath}`);
interpreter = (await getInterpreterDetails(workspace.uri)).path ?? [];
if (interpreter.length > 0) {
traceLog(
`Interpreter from ms-python.python extension for ${workspace.uri.fsPath}:`,
`${interpreter.join(' ')}`,
);
}
} else {
traceLog(`Interpreter from setting ${namespace}.interpreter: ${interpreter.join(' ')}`);
}

if (interpreter.length === 0) {
traceLog(`No interpreter found for ${workspace.uri.fsPath} in settings or from ms-python.python extension`);
}
}

Expand Down
42 changes: 14 additions & 28 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import * as vscode from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
import { restartServer } from './common/server';
import { registerLogger, traceError, traceLog, traceVerbose } from './common/logging';
import {
checkVersion,
getInterpreterDetails,
initializePython,
onDidChangePythonInterpreter,
resolveInterpreter,
runPythonExtensionCommand,
} from './common/python';
import { initializePython, onDidChangePythonInterpreter } from './common/python';
import {
checkIfConfigurationChanged,
getExtensionSettings,
getInterpreterFromSetting,
getWorkspaceSettings,
ISettings,
} from './common/settings';
import { loadServerDefaults } from './common/setup';
Expand Down Expand Up @@ -44,27 +38,19 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
traceVerbose(`Configuration: ${JSON.stringify(serverInfo)}`);

const runServer = async () => {
const interpreter = getInterpreterFromSetting(serverId);
if (interpreter && interpreter.length > 0 && checkVersion(await resolveInterpreter(interpreter))) {
traceVerbose(`Using interpreter from ${serverInfo.module}.interpreter: ${interpreter.join(' ')}`);
lsClient = await restartServer(serverId, serverName, outputChannel, lsClient);
return;
}

const interpreterDetails = await getInterpreterDetails();
if (interpreterDetails.path) {
traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(' ')}`);
lsClient = await restartServer(serverId, serverName, outputChannel, lsClient);
return;
}

updateStatus(vscode.l10n.t('Please select a Python interpreter.'), vscode.LanguageStatusSeverity.Error);
traceError(
'Python interpreter missing:\r\n' +
'[Option 1] Select python interpreter using the ms-python.python.\r\n' +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` +
const projectRoot = await getProjectRoot();
const workspaceSetting = await getWorkspaceSettings(serverId, projectRoot, true);
if (workspaceSetting.interpreter.length === 0) {
updateStatus(vscode.l10n.t('Please select a Python interpreter.'), vscode.LanguageStatusSeverity.Error);
traceError(
'Python interpreter missing:\r\n' +
'[Option 1] Select python interpreter using the ms-python.python.\r\n' +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n`,
'Please use Python 3.7 or greater.',
);
);
} else {
lsClient = await restartServer(workspaceSetting, serverId, serverName, outputChannel, lsClient);
}
};

context.subscriptions.push(
Expand Down

0 comments on commit 4a7339f

Please sign in to comment.