Skip to content

Commit

Permalink
Use ruff.interpreter from workspace settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 24, 2024
1 parent a8f3a92 commit 7e25f69
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 3 additions & 3 deletions src/common/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 2 additions & 5 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
getGlobalSettings,
getUserSetLegacyServerSettings,
getUserSetNativeServerSettings,
getWorkspaceSettings,
ISettings,
} from "./settings";
import {
Expand All @@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "vscode";
import { getInterpreterDetails } from "./python";
import { getConfiguration, getWorkspaceFolders } from "./vscodeapi";
import path = require("path");

type ImportStrategy = "fromEnvironment" | "useBundled";

Expand Down Expand Up @@ -121,8 +122,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<string>("configuration") ?? null;
Expand All @@ -136,7 +141,7 @@ export async function getWorkspaceSettings(
workspace: workspace.uri.toString(),
path: resolveVariables(config.get<string[]>("path") ?? [], workspace),
ignoreStandardLibrary: config.get<boolean>("ignoreStandardLibrary") ?? true,
interpreter: resolveVariables(interpreter, workspace),
interpreter,
configuration,
importStrategy: config.get<ImportStrategy>("importStrategy") ?? "fromEnvironment",
codeAction: config.get<CodeAction>("codeAction") ?? {},
Expand Down
105 changes: 54 additions & 51 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as vscode from "vscode";
import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/node";
import { registerLogger, traceError, traceLog, traceVerbose } from "./common/log/logging";
import {
registerLogger,
traceError,
traceLog,
traceVerbose,
traceWarn,
} from "./common/log/logging";
import {
checkVersion,
getInterpreterDetails,
initializePython,
onDidChangePythonInterpreter,
resolveInterpreter,
Expand All @@ -12,6 +17,7 @@ import { restartServer } from "./common/server";
import {
checkIfConfigurationChanged,
getInterpreterFromSetting,
getWorkspaceSettings,
ISettings,
} from "./common/settings";
import { loadServerDefaults } from "./common/setup";
Expand All @@ -23,6 +29,7 @@ import {
onDidGrantWorkspaceTrust,
registerCommand,
} from "./common/vscodeapi";
import { getProjectRoot } from "./common/utilities";

const issueTracker = "https://github.com/astral-sh/ruff/issues";

Expand Down Expand Up @@ -87,63 +94,59 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

restartInProgress = true;

if (!vscode.workspace.isTrusted) {
lsClient = await restartServer(serverId, serverName, outputChannel, lsClient);

restartInProgress = false;
if (restartQueued) {
restartQueued = false;
await runServer();
}

return;
}
const projectRoot = await getProjectRoot();
const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot);

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(
Expand Down Expand Up @@ -263,10 +266,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
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();
});
}

Expand Down

0 comments on commit 7e25f69

Please sign in to comment.