Skip to content

Commit

Permalink
Validate Python interpreter version for native server (#516)
Browse files Browse the repository at this point in the history
## Summary

I'm not sure why this check was removed looking back at
#431, but this validation
should be done for both native server and `ruff-lsp`. Both uses a Python
script which requires Python 3.7 or later and `ruff` in general also
supports 3.7 and later.

Without this change, if someone were to use Python 3.6, the extension
would give the following error:
```
2024-07-05 09:26:21.378 [error] Error while trying to find the Ruff binary: Error: Command failed: /Users/dhruv/.pyenv/versions/3.6.15/bin/python /Users/dhruv/work/astral/ruff-vscode/bundled/tool/find_ruff_binary_path.py
Traceback (most recent call last):
  File "/Users/dhruv/work/astral/ruff-vscode/bundled/tool/find_ruff_binary_path.py", line 33, in <module>
    ruff_binary_path = find_ruff_binary_path()
  File "/Users/dhruv/work/astral/ruff-vscode/bundled/tool/find_ruff_binary_path.py", line 20, in find_ruff_binary_path
    elif sys.platform == "darwin" and sys._framework:
AttributeError: module 'sys' has no attribute '_framework'
```

## Test Plan

The above error still occurs and the fix for that is in a follow-up PR
(#517).

Using the below settings:
```json
{
  "ruff.nativeServer":true,
  "ruff.interpreter": ["/Users/dhruv/.pyenv/versions/3.6.15/bin/python"]
}
```

The logs shows that the version isn't supported:
```
2024-07-05 09:27:38.086 [error] Python version 3.6 is not supported.
2024-07-05 09:27:38.086 [error] Selected python path: /Users/dhruv/.pyenv/versions/3.6.15/bin/python
2024-07-05 09:27:38.086 [error] Supported versions are 3.7 and above.
```
  • Loading branch information
dhruvmanila authored Jul 5, 2024
1 parent fb6ec2f commit 50e9053
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}),
);

const { enable, nativeServer } = getConfiguration(serverId) as unknown as ISettings;
const { enable } = getConfiguration(serverId) as unknown as ISettings;
if (!enable) {
traceLog(
"Extension is disabled. To enable, change `ruff.enable` to `true` and restart VS Code.",
Expand Down Expand Up @@ -86,9 +86,15 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

restartInProgress = true;

if (nativeServer) {
traceVerbose("Using experimental server with bundled Ruff binary");

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

restartInProgress = false;
Expand All @@ -98,42 +104,20 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}

return;
} else {
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);
}

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

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

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();
}

return;
}
return;
}

restartInProgress = false;
Expand Down

0 comments on commit 50e9053

Please sign in to comment.