Skip to content

Commit 89aa22f

Browse files
committed
feat(pygls-playground): change pythonPath to pythonCommand
This allows the user to specify an arbitrary command to use when launching the python interpreter. When combined with tools like `uv`, this makes it trivial to switch between different versions of pygls! For example ```json { "pygls.server.pythonCommand": ["uv", "run", "--no-project", "--prerelease=allow", "--with", "pygls==2.0a2", "python"] } ```
1 parent 805ae6f commit 89aa22f

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

.vscode/extensions/pygls-playground/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"author": "Open Law Library",
55
"repository": "https://github.com/openlawlibrary/pygls",
66
"license": "Apache-2.0",
7-
"version": "1.3.1",
7+
"version": "2.0.0",
88
"publisher": "openlawlibrary",
99
"engines": {
1010
"node": ">=16.17.1",
@@ -79,11 +79,14 @@
7979
"description": "The python script to run when launching the server.",
8080
"markdownDescription": "The python script to run when launching the server.\n Relative to #pygls.server.cwd#"
8181
},
82-
"pygls.server.pythonPath": {
82+
"pygls.server.pythonCommand": {
8383
"scope": "resource",
84-
"type": "string",
85-
"default": "",
86-
"description": "The python interpreter to use to run the server.\nBy default, this extension will attempt to use the Python interpreter configured via the Python extension, setting this setting will override this behavior."
84+
"type": "array",
85+
"items": {
86+
"type": "string"
87+
},
88+
"default": null,
89+
"description": "The command to use when launching the python interpreter used to run the server.\nBy default, this extension will attempt to use the Python interpreter configured via the Python extension, setting this setting will override this behavior."
8790
},
8891
"pygls.trace.server": {
8992
"scope": "resource",

.vscode/extensions/pygls-playground/src/extension.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import * as semver from "semver";
2626
import { PythonExtension } from "@vscode/python-extension";
2727
import { LanguageClient, LanguageClientOptions, ServerOptions, State, integer } from "vscode-languageclient/node";
2828

29-
const MIN_PYTHON = semver.parse("3.7.9")
29+
const MIN_PYTHON = semver.parse("3.9.0")
3030

3131
// Some other nice to haves.
3232
// TODO: Check selected env satisfies pygls' requirements - if not offer to run the select env command.
@@ -317,45 +317,44 @@ function getServerPath(): string {
317317
*/
318318
async function getPythonCommand(resource?: vscode.Uri): Promise<string[] | undefined> {
319319
const config = vscode.workspace.getConfiguration("pygls.server", resource)
320-
const pythonPath = await getPythonInterpreter(resource)
321-
if (!pythonPath) {
320+
const pythonCommand = await getPythonInterpreterCmd(resource)
321+
if (!pythonCommand) {
322322
return
323323
}
324-
const command = [pythonPath]
325324
const enableDebugger = config.get<boolean>('debug')
326325

327326
if (!enableDebugger) {
328-
return command
327+
return pythonCommand
329328
}
330329

331330
const debugHost = config.get<string>('debugHost')
332331
const debugPort = config.get<integer>('debugPort')
333332
try {
334333
const debugArgs = await python.debug.getRemoteLauncherCommand(debugHost, debugPort, true)
335334
// Debugpy recommends we disable frozen modules
336-
command.push("-Xfrozen_modules=off", ...debugArgs)
335+
pythonCommand.push("-Xfrozen_modules=off", ...debugArgs)
337336
} catch (err) {
338337
logger.error(`Unable to get debugger command: ${err}`)
339338
logger.error("Debugger will not be available.")
340339
}
341340

342-
return command
341+
return pythonCommand
343342
}
344343

345344
/**
346-
* Return the python interpreter to use when starting the server.
345+
* Return the command to use to start the python interpreter
347346
*
348-
* This uses the official python extension to grab the user's currently
349-
* configured environment.
347+
* If no command is configured, this uses the official python extension to
348+
* grab the user's currently configured environment.
350349
*
351-
* @returns The python interpreter to use to launch the server
350+
* @returns The command needed to invoke the python interpreter
352351
*/
353-
async function getPythonInterpreter(resource?: vscode.Uri): Promise<string | undefined> {
352+
async function getPythonInterpreterCmd(resource?: vscode.Uri): Promise<string[] | undefined> {
354353
const config = vscode.workspace.getConfiguration("pygls.server", resource)
355-
const pythonPath = config.get<string>('pythonPath')
356-
if (pythonPath) {
357-
logger.info(`Using user configured python environment: '${pythonPath}'`)
358-
return pythonPath
354+
const pythonCommand = config.get<string[]>('pythonCommand')
355+
if (pythonCommand) {
356+
logger.info(`Using user configured python command: '${pythonCommand}'`)
357+
return pythonCommand
359358
}
360359

361360
if (!python) {
@@ -400,7 +399,7 @@ async function getPythonInterpreter(resource?: vscode.Uri): Promise<string | und
400399
return
401400
}
402401

403-
return pythonUri.fsPath
402+
return [pythonUri.fsPath]
404403
}
405404

406405
async function getPythonExtension() {

.vscode/settings.json

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,35 @@
33
// Uncomment to enable `textDocument/onTypeFormatting` requests
44
// "editor.formatOnType": true
55
},
6-
// Uncomment to override Python interpreter used.
7-
// "pygls.server.pythonPath": "/path/to/python",
8-
"pygls.server.debug": false,
6+
// Uncomment to override Python environment used.
7+
// "pygls.server.pythonCommand": ["uv", "run", "--no-project", "--prerelease=allow", "--with", "pygls==2.0a2", "python"],
8+
//
9+
// Uncomment to debug the server process in VSCode
10+
// "pygls.server.debug": true,
11+
//
12+
// Uncomment to change where the debugger should be hosted
913
// "pygls.server.debugHost": "localhost",
1014
// "pygls.server.debugPort": 5678,
15+
//
16+
// Controls which LSP server is launched
1117
"pygls.server.launchScript": "code_actions.py", // This is relative to `pygls.server.cwd`
1218
"pygls.server.cwd": "${workspaceFolder}/examples/servers",
13-
"pygls.trace.server": "off",
19+
//
20+
// Uncomment to view all LSP messages between the client and server
21+
// "pygls.trace.server": "verbose",
22+
//
23+
// Controls which documents VSCode will ask your LSP server about
1424
"pygls.client.documentSelector": [
1525
{
16-
"scheme": "file",
1726
"language": "plaintext"
1827
}
1928
],
29+
// "pygls.jsonServer.exampleConfiguration": "some value here",
30+
//
31+
// Below are standard VSCode project settings.
2032
"python.testing.pytestArgs": [
2133
"."
2234
],
2335
"python.testing.unittestEnabled": false,
2436
"python.testing.pytestEnabled": true,
25-
// "pygls.jsonServer.exampleConfiguration": "some value here",
2637
}

0 commit comments

Comments
 (0)