Skip to content

Commit

Permalink
Separate Pyright language server as one micro service (#3150)
Browse files Browse the repository at this point in the history
This PR separates the Pyright language server as an independent
microservice. To provide support for both Pyright and PyLSP, we have
created a python-language-server.sh script to run the Python language
server.

To run the script:
`./python-language-server.sh`
By default, it will use PyLSP and run on port 3000. To run Pyright, use
the following command:
`./python-language-server.sh --server=pyright`
For example, to run PyLSP on a different port (e.g., 4000):
`./python-language-server.sh --server=pylsp --port=4000`
  • Loading branch information
yunyad authored Dec 13, 2024
1 parent 7c8ab8c commit 8b35e8c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import edu.uci.ics.texera.web.resource.dashboard.user.workflow.{
WorkflowResource,
WorkflowVersionResource
}
import edu.uci.ics.texera.web.resource.languageserver.PythonLanguageServerManager
import io.dropwizard.auth.AuthValueFactoryProvider
import io.dropwizard.setup.{Bootstrap, Environment}
import io.dropwizard.websockets.WebsocketBundle
Expand Down Expand Up @@ -75,7 +74,6 @@ object TexeraWebApplication {
.resolve("web-config.yml")
.toString
)
PythonLanguageServerManager.startLanguageServer()
}
}

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion core/pyright-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"ts-node": "10.9.2"
},
"scripts": {
"start": "node --loader ts-node/esm src/main.ts"
"start": "node --loader ts-node/esm src/main.ts --port"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"languageServerDir": "../node_modules/pyright/dist",
"amberConfigFilePath": "../../amber/src/main/resources/application.conf",
"clientPathName": "/python-language-server"
}
46 changes: 29 additions & 17 deletions core/pyright-language-server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
//The source file can be referred to: https://github.com/TypeFox/monaco-languageclient/blob/main/packages/examples/src/python/server/main.ts

import {dirname, resolve} from "node:path";
import { dirname, resolve } from "node:path";
import { runLanguageServer } from "./language-server-runner.ts";
import { getLocalDirectory, LanguageName } from "./server-commons.ts";
import fs from "fs";
import hoconParser from "hocon-parser";
import {fileURLToPath} from "url";
import { fileURLToPath } from "url";

const runPythonServer = (baseDir: string, relativeDir: string, serverPort: number) => {
const runPythonServer = (
baseDir: string,
relativeDir: string,
serverPort: number,
) => {
const processRunPath = resolve(baseDir, relativeDir);
runLanguageServer({
serverName: "PYRIGHT",
pathName: clientPathName,
serverPort: serverPort,
runCommand: LanguageName.node,
runCommandArgs: [
processRunPath,
"--stdio",
],
runCommandArgs: [processRunPath, "--stdio"],
wsServerOptions: {
noServer: true,
perMessageDeflate: false,
Expand All @@ -26,19 +27,30 @@ const runPythonServer = (baseDir: string, relativeDir: string, serverPort: numbe
});
};


const baseDir = getLocalDirectory(import.meta.url);
const relativeDir = "./node_modules/pyright/dist/pyright-langserver.js";

const configFilePath = resolve(baseDir, "pythonLanguageServerConfig.json");
const config = JSON.parse(fs.readFileSync(configFilePath, "utf-8"));

const amberConfigFilePath = resolve(baseDir, config.amberConfigFilePath);
const amberConfigContent = fs.readFileSync(amberConfigFilePath, "utf-8");
const applicationConfig = hoconParser(amberConfigContent) as Record<string, any>;
const configFilePath = resolve(baseDir, "config.json");
const configContent = fs.readFileSync(configFilePath, "utf-8");
const config = JSON.parse(configContent) as Record<string, any>;

const languageServerDir = resolve(baseDir, config.languageServerDir);
const clientPathName = config.clientPathName;

const parseArgs = (): Record<string, string> => {
const args = process.argv.slice(2);
const options: Record<string, string> = {};
args.forEach((arg) => {
if (arg.startsWith("--") && arg.includes("=")) {
const [key, value] = arg.substring(2).split("=");
options[key] = value;
}
});
return options;
};

const pythonLanguageServerPort = applicationConfig["python-language-server"].port;
const clientPathName = config.clientPathName
const args = parseArgs();
const pythonLanguageServerPort = args["port"] ? parseInt(args["port"]) : 3000;

const runDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
runPythonServer(runDir, relativeDir, pythonLanguageServerPort);
runPythonServer(runDir, relativeDir, pythonLanguageServerPort);
67 changes: 67 additions & 0 deletions core/scripts/python-language-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

set -e

DEFAULT_PROVIDER="pylsp"
DEFAULT_PORT=3000

PROVIDER=""
PORT=""

BASE_DIR=$(dirname "$0")
PYRIGHT_DIR="$BASE_DIR/../pyright-language-server"

while [ $# -gt 0 ]; do
case "$1" in
--server=*|--provider=*)
PROVIDER="${1#*=}"
;;
--port=*)
PORT="${1#*=}"
;;
*)
echo "Unknown argument: $1"
echo "Usage: $0 [--server=<pyright|pylsp>] [--port=<port_number>]"
exit 1
;;
esac
shift
done

PROVIDER="${PROVIDER:-$DEFAULT_PROVIDER}"
PORT="${PORT:-$DEFAULT_PORT}"

# Validate port value
if ! [[ "$PORT" =~ ^[0-9]+$ ]]; then
echo "Invalid port: $PORT. Must be a number."
exit 1
fi

start_pyright() {
echo "Starting Pyright Language Server on port $PORT..."
cd "$PYRIGHT_DIR"
yarn install --silent
yarn start --port="$PORT"
}

start_pylsp() {
echo "Starting Pylsp Language Server on port $PORT..."
if ! command -v pylsp &>/dev/null; then
echo "Error: pylsp is not installed. Install it with 'pip install python-lsp-server'."
exit 1
fi
pylsp --port "$PORT" --ws
}

case $PROVIDER in
pyright)
start_pyright
;;
pylsp)
start_pylsp
;;
*)
echo "Invalid provider: $PROVIDER. Valid options are: pyright, pylsp."
exit 1
;;
esac

0 comments on commit 8b35e8c

Please sign in to comment.