diff --git a/package.json b/package.json index 57144a2..19322bc 100644 --- a/package.json +++ b/package.json @@ -243,7 +243,7 @@ "ruff.interpreter": { "default": [], "markdownDescription": "Path to a Python interpreter to use to run the LSP server.", - "scope": "window", + "scope": "resource", "items": { "type": "string" }, diff --git a/src/common/python.ts b/src/common/python.ts index 4686b76..aa6d84c 100644 --- a/src/common/python.ts +++ b/src/common/python.ts @@ -67,13 +67,13 @@ export async function runPythonExtensionCommand(command: string, ...rest: any[]) return await commands.executeCommand(command, ...rest); } -export function checkVersion(resolved: ResolvedEnvironment | undefined): boolean { - const version = resolved?.version; +export function checkVersion(resolved: ResolvedEnvironment): boolean { + const version = resolved.version; if (version?.major === 3 && version?.minor >= 7) { return true; } traceError(`Python version ${version?.major}.${version?.minor} is not supported.`); - traceError(`Selected python path: ${resolved?.executable.uri?.fsPath}`); + traceError(`Selected python path: ${resolved.executable.uri?.fsPath}`); traceError("Supported versions are 3.7 and above."); return false; } diff --git a/src/common/server.ts b/src/common/server.ts index b8158ea..3369b7d 100644 --- a/src/common/server.ts +++ b/src/common/server.ts @@ -24,7 +24,6 @@ import { getGlobalSettings, getUserSetLegacyServerSettings, getUserSetNativeServerSettings, - getWorkspaceSettings, ISettings, } from "./settings"; import { @@ -36,7 +35,6 @@ import { NATIVE_SERVER_STABLE_VERSION, } from "./version"; import { updateServerKind, updateStatus } from "./status"; -import { getProjectRoot } from "./utilities"; import { isVirtualWorkspace } from "./vscodeapi"; import { execFile } from "child_process"; import which = require("which"); @@ -412,6 +410,8 @@ async function createServer( let _disposables: Disposable[] = []; export async function restartServer( + projectRoot: vscode.WorkspaceFolder, + workspaceSettings: ISettings, serverId: string, serverName: string, outputChannel: LogOutputChannel, @@ -426,9 +426,6 @@ export async function restartServer( updateStatus(undefined, LanguageStatusSeverity.Information, true); - const projectRoot = await getProjectRoot(); - const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot); - const extensionSettings = await getExtensionSettings(serverId); const globalSettings = await getGlobalSettings(serverId); diff --git a/src/common/settings.ts b/src/common/settings.ts index 43585dd..c01f688 100644 --- a/src/common/settings.ts +++ b/src/common/settings.ts @@ -121,8 +121,12 @@ export async function getWorkspaceSettings( const config = getConfiguration(namespace, workspace.uri); let interpreter: string[] = getInterpreterFromSetting(namespace, workspace) ?? []; - if (interpreter.length === 0 && vscode.workspace.isTrusted) { - interpreter = (await getInterpreterDetails(workspace.uri)).path ?? []; + if (interpreter.length === 0) { + if (vscode.workspace.isTrusted) { + interpreter = (await getInterpreterDetails(workspace.uri)).path ?? []; + } + } else { + interpreter = resolveVariables(interpreter, workspace); } let configuration = config.get("configuration") ?? null; @@ -136,7 +140,7 @@ export async function getWorkspaceSettings( workspace: workspace.uri.toString(), path: resolveVariables(config.get("path") ?? [], workspace), ignoreStandardLibrary: config.get("ignoreStandardLibrary") ?? true, - interpreter: resolveVariables(interpreter, workspace), + interpreter, configuration, importStrategy: config.get("importStrategy") ?? "fromEnvironment", codeAction: config.get("codeAction") ?? {}, diff --git a/src/extension.ts b/src/extension.ts index 7fb3d9b..eba981b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,7 +3,6 @@ import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/nod import { registerLogger, traceError, traceLog, traceVerbose } from "./common/log/logging"; import { checkVersion, - getInterpreterDetails, initializePython, onDidChangePythonInterpreter, resolveInterpreter, @@ -12,6 +11,7 @@ import { restartServer } from "./common/server"; import { checkIfConfigurationChanged, getInterpreterFromSetting, + getWorkspaceSettings, ISettings, } from "./common/settings"; import { loadServerDefaults } from "./common/setup"; @@ -23,6 +23,7 @@ import { onDidGrantWorkspaceTrust, registerCommand, } from "./common/vscodeapi"; +import { getProjectRoot } from "./common/utilities"; const issueTracker = "https://github.com/astral-sh/ruff/issues"; @@ -87,63 +88,59 @@ export async function activate(context: vscode.ExtensionContext): Promise restartInProgress = true; - if (!vscode.workspace.isTrusted) { - lsClient = await restartServer(serverId, serverName, outputChannel, lsClient); + const projectRoot = await getProjectRoot(); + const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot); - restartInProgress = false; - if (restartQueued) { - restartQueued = false; - await runServer(); - } - - return; - } - - const interpreter = getInterpreterFromSetting(serverId); - if (interpreter !== undefined && interpreter.length > 0) { - if (checkVersion(await resolveInterpreter(interpreter))) { - traceVerbose( - `Using interpreter from ${serverInfo.module}.interpreter: ${interpreter.join(" ")}`, + if (vscode.workspace.isTrusted) { + if (workspaceSettings.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.", ); - lsClient = await restartServer(serverId, serverName, outputChannel, lsClient); restartInProgress = false; - if (restartQueued) { - restartQueued = false; - await runServer(); - } + return; } - restartInProgress = false; - return; - } - - const interpreterDetails = await getInterpreterDetails(); - if (interpreterDetails.path) { - traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(" ")}`); - lsClient = await restartServer(serverId, serverName, outputChannel, lsClient); - - restartInProgress = false; - if (restartQueued) { - restartQueued = false; - await runServer(); + traceLog(`Using interpreter: ${workspaceSettings.interpreter.join(" ")}`); + const resolvedEnvironment = await resolveInterpreter(workspaceSettings.interpreter); + if (resolvedEnvironment === undefined) { + updateStatus( + vscode.l10n.t("Python interpreter not found."), + vscode.LanguageStatusSeverity.Error, + ); + traceError( + "Unable to find any Python environment for the interpreter path:", + workspaceSettings.interpreter.join(" "), + ); + restartInProgress = false; + return; + } else if (!checkVersion(resolvedEnvironment)) { + restartInProgress = false; + return; } - - return; } - restartInProgress = false; - - 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.", + lsClient = await restartServer( + projectRoot, + workspaceSettings, + serverId, + serverName, + outputChannel, + lsClient, ); + + restartInProgress = false; + if (restartQueued) { + restartQueued = false; + await runServer(); + } }; context.subscriptions.push( @@ -263,10 +260,10 @@ export async function activate(context: vscode.ExtensionContext): Promise traceLog(`Python extension loading`); await initializePython(context.subscriptions); traceLog(`Python extension loaded`); + return; // The `onDidChangePythonInterpreter` event will trigger the server start. } - } else { - await runServer(); } + await runServer(); }); }