-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlsp_runner.py
71 lines (58 loc) · 2.26 KB
/
lsp_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
Runner to use when running under a different interpreter.
"""
import os
import pathlib
import sys
import traceback
# **********************************************************
# Update sys.path before importing any bundled libraries.
# **********************************************************
def update_sys_path(path_to_add: str, strategy: str) -> None:
"""Add given path to `sys.path`."""
if path_to_add not in sys.path and os.path.isdir(path_to_add):
if strategy == "useBundled":
sys.path.insert(0, path_to_add)
else:
sys.path.append(path_to_add)
# Ensure that we can import LSP libraries, and other bundled libraries.
update_sys_path(
os.fspath(pathlib.Path(__file__).parent.parent / "libs"),
os.getenv("LS_IMPORT_STRATEGY", "useBundled"),
)
# pylint: disable=wrong-import-position,import-error
import lsp_jsonrpc as jsonrpc
import lsp_utils as utils
RPC = jsonrpc.create_json_rpc(sys.stdin.buffer, sys.stdout.buffer)
EXIT_NOW = False
while not EXIT_NOW:
msg = RPC.receive_data()
method = msg["method"]
if method == "exit":
EXIT_NOW = True
continue
if method == "run":
is_exception = False # pylint: disable=invalid-name
# This is needed to preserve sys.path, pylint modifies
# sys.path and that might not work for this scenario
# next time around.
with utils.substitute_attr(sys, "path", [""] + sys.path[:]):
try:
result = utils.run_module(
module=msg["module"],
argv=msg["argv"],
use_stdin=msg["useStdin"],
cwd=msg["cwd"],
source=msg["source"] if "source" in msg else None,
)
except Exception: # pylint: disable=broad-except
result = utils.RunResult("", traceback.format_exc(chain=True))
is_exception = True # pylint: disable=invalid-name
response = {"id": msg["id"], "error": result.stderr}
if is_exception:
response["exception"] = is_exception
elif result.stdout:
response["result"] = result.stdout
RPC.send_data(response)