From 1d3142c664f1b775f95a49b5b5b03e29dc318662 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 16 May 2023 18:13:03 +0200 Subject: [PATCH 1/3] Add better python server logging --- bundled/tool/server.py | 48 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/bundled/tool/server.py b/bundled/tool/server.py index 35f7dec..eba7078 100755 --- a/bundled/tool/server.py +++ b/bundled/tool/server.py @@ -2,30 +2,68 @@ from __future__ import annotations +import logging +import logging.config import os import pathlib +import site import sys BUNDLE_DIR = pathlib.Path(__file__).parent.parent +logger = logging.getLogger(__name__) # Update sys.path before importing any bundled libraries. def update_sys_path(path_to_add: str) -> None: """Add given path to `sys.path`.""" if path_to_add not in sys.path and os.path.isdir(path_to_add): + # site adds the directory at the end if it is not yet present, we want it to + # be front sys.path.insert(0, path_to_add) + # Allow dev installs into bundled/libs + site.addsitedir(path_to_add) -# Ensure that we can import LSP libraries, and other bundled libraries. -update_sys_path(os.fspath(BUNDLE_DIR / "libs")) +def main(): + import ruff_lsp + from ruff_lsp import server + logging.config.dictConfig( + { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "simple": {"format": "%(asctime)s %(levelname)-4s %(message)s"} + }, + "handlers": { + "stderr": { + "class": "logging.StreamHandler", + "formatter": "simple", + }, + }, + "root": {"level": "INFO", "handlers": ["stderr"]}, + "loggers": { + # Don't repeat every message + "pygls.protocol": { + "level": "WARN", + "handlers": ["stderr"], + "propagate": False, + }, + }, + } + ) -# Start the server. -if __name__ == "__main__": - from ruff_lsp import server + logger.info(f"ruff path: {ruff_lsp.__file__}, sys.path: {sys.path}") if not hasattr(server, "set_bundle"): raise RuntimeError("ruff-vscode needs at least ruff-lsp v0.0.6") server.set_bundle(os.fspath(BUNDLE_DIR / "libs" / "bin" / server.TOOL_MODULE)) server.start() + + +# Start the server. +if __name__ == "__main__": + # Ensure that we can import LSP libraries, and other bundled libraries. + update_sys_path(os.fspath(BUNDLE_DIR / "libs")) + main() From 69cb269d3169826a9f91ba1850cbd164c80b0fc6 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 16 May 2023 18:13:12 +0200 Subject: [PATCH 2/3] Explain more about developing --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index c251603..b28af53 100644 --- a/README.md +++ b/README.md @@ -154,3 +154,12 @@ This extension is based on the [Template for VS Code Python tools extensions](ht - To automatically format the codebase, run: `just fmt`. - To run lint and type checks, run: `just check`. - To run tests, run: `just test`. + +To run the extension, navigate to `src/extension.ts` and run (`F5`). You should see the LSP output +and Python log messages in the debug console under "Python Server". + +### Modifying the LSP + +- Clone the [ruff-lsp](https://github.com/charliermarsh/ruff-lsp) to e.g. `../ruff-lsp` +- Go to `../ruff-lsp` and run `pip install -t ../ruff-vscode/bundled/libs/ -e .` +- If you want to use a custom ruff build, copy or link it to `bundles/libs/bin/ruff` From 34cfd757db7d03ae5874b2c03b6c05e2254cc992 Mon Sep 17 00:00:00 2001 From: konstin Date: Wed, 17 May 2023 23:27:05 +0200 Subject: [PATCH 3/3] Document custom ruff build --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b28af53..12ff0f5 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,11 @@ and Python log messages in the debug console under "Python Server". ### Modifying the LSP -- Clone the [ruff-lsp](https://github.com/charliermarsh/ruff-lsp) to e.g. `../ruff-lsp` +- Clone [ruff-lsp](https://github.com/charliermarsh/ruff-lsp) to e.g. `../ruff-lsp` - Go to `../ruff-lsp` and run `pip install -t ../ruff-vscode/bundled/libs/ -e .` -- If you want to use a custom ruff build, copy or link it to `bundles/libs/bin/ruff` + +### Custom ruff build + +- Clone [ruff](https://github.com/charliermarsh/ruff) to e.g. `/home/ferris/ruff` +- `cargo build` in ruff +- In the settings, add `/home/ferris/ruff/target/debug/ruff` to "Ruff: Path"