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 6b70a82
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 59 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
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
10 changes: 7 additions & 3 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("configuration") ?? null;
Expand All @@ -136,7 +140,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
86 changes: 36 additions & 50 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +11,7 @@ import { restartServer } from "./common/server";
import {
checkIfConfigurationChanged,
getInterpreterFromSetting,
getWorkspaceSettings,
ISettings,
} from "./common/settings";
import { loadServerDefaults } from "./common/setup";
Expand All @@ -23,6 +23,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 +88,48 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

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 (!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 +249,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 6b70a82

Please sign in to comment.