From 1668b2a645beda2f7fb3d352a6e803b417eaf1c6 Mon Sep 17 00:00:00 2001 From: langxm Date: Wed, 14 Oct 2026 03:21:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(P4):=20LSP=20=E8=AF=8A=E6=96=AD=E6=94=B9?= =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E6=8B=89=E5=8F=96(textDocument/diagnostic=20?= =?UTF-8?q?3.17)=E2=80=94=E2=80=94=E5=BC=82=E6=AD=A5=20publishDiagnostics?= =?UTF-8?q?=20=E7=AE=A1=E9=81=93=E5=B8=A7=E9=94=99=E4=BD=8D(read(n)?= =?UTF-8?q?=E4=B8=8D=E6=BB=A1)=E5=AF=BC=E8=87=B4=E9=9D=9E=E7=A1=AE?= =?UTF-8?q?=E5=AE=9A=E4=B8=A2=E5=B8=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/lsp_service.py | 50 +++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/backend/app/services/lsp_service.py b/backend/app/services/lsp_service.py index 380b5b0..6ebee8a 100644 --- a/backend/app/services/lsp_service.py +++ b/backend/app/services/lsp_service.py @@ -49,6 +49,7 @@ class LspProcess: self._msg_id = 0 self._ready = threading.Event() self._closed = False + self._warmed = False def start(self) -> None: try: @@ -71,6 +72,16 @@ class LspProcess: self.proc.stdin.write(frame) self.proc.stdin.flush() + def _read_exact(self, n: int) -> bytes: + """管道 read(n) 不保证读满 n 字节(帧错位根因),循环读满。""" + buf = b"" + while len(buf) < n and self.proc and self.proc.stdout: + chunk = self.proc.stdout.read(n - len(buf)) + if not chunk: + break + buf += chunk + return buf + def _read_loop(self) -> None: try: while not self._closed and self.proc and self.proc.stdout: @@ -81,8 +92,8 @@ class LspProcess: continue n = int(line.split(b":")[1]) self.proc.stdout.readline() # 空行 - raw = self.proc.stdout.read(n) - if not raw: + raw = self._read_exact(n) + if len(raw) < n: break try: msg = json.loads(raw) @@ -93,12 +104,14 @@ class LspProcess: logger.warning("LSP %s 读线程退出: %s", self.lang, e) def _dispatch(self, msg: dict) -> None: + logger.debug("LSP %s 收到帧: method=%s id=%s", self.lang, msg.get("method"), msg.get("id")) if "id" in msg and "method" not in msg: q = self._pending.pop(msg.get("id"), None) if q: q.put(msg) elif msg.get("method") == "publishDiagnostics": uri = msg["params"].get("uri", "") + logger.debug("LSP %s publishDiagnostics uri=%s (期望在 _diags=%s)", self.lang, uri, bool(self._diag_event.get(uri))) self._diags[uri] = msg["params"].get("diagnostics", []) ev = self._diag_event.get(uri) if ev: @@ -156,15 +169,34 @@ class LspProcess: "version": 1, "text": code}, }) - def diagnostics(self, code: str, timeout: float = 12.0) -> list[dict]: + def diagnostics(self, code: str, timeout: float = 15.0) -> list[dict]: + """主动拉取诊断(LSP 3.17 textDocument/diagnostic;不依赖异步 publishDiagnostics)。 + 首帧前留初始化间隔,拉取为空时小间隔重试(服务器异步分析)。 + """ + if not self._warmed: + time.sleep(2) + self._warmed = True uri = self._uri() - self._diag_event[uri] = threading.Event() self.open_doc(uri, code) - if not self._diag_event[uri].wait(timeout): - self._diag_event.pop(uri, None) - raise LspError(f"{self.lang} 诊断超时(服务器无响应)") - self._diag_event.pop(uri, None) - return self._diags.pop(uri, []) + t0 = time.time() + while time.time() - t0 < timeout: + try: + result = self.request("textDocument/diagnostic", {"textDocument": {"uri": uri}}, timeout=min(10, timeout)) + except LspError as e: + # 服务器不支持拉取诊断 → 回退等 publishDiagnostics + logger.warning("LSP %s 拉取诊断失败,回退异步推送: %s", self.lang, e) + ev = threading.Event() + self._diag_event[uri] = ev + if ev.wait(timeout): + self._diag_event.pop(uri, None) + return self._diags.pop(uri, []) + self._diag_event.pop(uri, None) + raise + items = result.get("items", []) + if items: + return items + time.sleep(1.0) + raise LspError(f"{self.lang} 诊断超时(服务器无响应)") def completion(self, code: str, line: int, character: int) -> list[dict]: uri = self._uri()