From d23c06b8fd84baa74a02c45f3337339086be30e8 Mon Sep 17 00:00:00 2001 From: James Westman Date: Mon, 6 Dec 2021 11:43:10 -0600 Subject: [PATCH] lsp: Fix text encoding issue Content-Length is the number of bytes in the message, but the JSONRPC code was interpreting it as a number of characters (when both reading and writing), which caused it to fail on multibyte UTF-8 characters. --- blueprintcompiler/lsp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blueprintcompiler/lsp.py b/blueprintcompiler/lsp.py index 23fe63f..2297211 100644 --- a/blueprintcompiler/lsp.py +++ b/blueprintcompiler/lsp.py @@ -103,12 +103,12 @@ class LanguageServer: line = "" content_len = -1 while content_len == -1 or (line != "\n" and line != "\r\n"): - line = sys.stdin.readline() + line = sys.stdin.buffer.readline().decode() if line == "": return if line.startswith("Content-Length:"): content_len = int(line.split("Content-Length:")[1].strip()) - line = sys.stdin.read(content_len) + line = sys.stdin.buffer.read(content_len).decode() self._log("input: " + line) data = json.loads(line) @@ -126,7 +126,7 @@ class LanguageServer: data["jsonrpc"] = "2.0" line = json.dumps(data, separators=(",", ":")) + "\r\n" self._log("output: " + line) - sys.stdout.write(f"Content-Length: {len(line)}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}") + sys.stdout.write(f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}") sys.stdout.flush() def _log(self, msg):