Skip to content

Commit 18a2bdf

Browse files
LSP: reply MethodNotFound to unknown requests
1 parent 03fbee9 commit 18a2bdf

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/lsp/lp_lsp.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,16 @@ let dispatch_message ofmt dict =
817817
| "initialized" ->
818818
()
819819
| msg ->
820-
LIO.log_error "no_handler" msg
820+
(* Requests carry an id; notifications don't. For requests we must
821+
reply with JSON-RPC MethodNotFound so the client doesn't wait
822+
forever. Notifications get logged and dropped, matching the
823+
spec's "no response" rule. [oint_field] defaults absent id to 0,
824+
which we treat as the notification sentinel. *)
825+
LIO.log_error "no_handler" msg;
826+
if id <> 0 then
827+
LIO.send_json ofmt
828+
(LSP.mk_error_reply ~id ~code:(-32601)
829+
~msg:("Method not found: " ^ msg))
821830

822831
let process_input ofmt (com : J.t) =
823832
try dispatch_message ofmt (U.to_assoc com)

src/lsp/lsp_base.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ let _parse_uri str =
3737
let mk_reply ~id ~result =
3838
`Assoc [ "jsonrpc", `String "2.0"; "id", `Int id; "result", result ]
3939

40+
(* JSON-RPC error reply. Codes follow the JSON-RPC 2.0 spec (see also
41+
LSP §error-codes): -32601 MethodNotFound is the one we reach for. *)
42+
let mk_error_reply ~id ~code ~msg =
43+
`Assoc [ "jsonrpc", `String "2.0";
44+
"id", `Int id;
45+
"error", `Assoc ["code", `Int code; "message", `String msg] ]
46+
4047
let mk_event m p =
4148
`Assoc [ "jsonrpc", `String "2.0"; "method", `String m; "params", `Assoc p ]
4249

src/lsp/lsp_base.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ val mk_range_of_interval : Range.t -> J.t
3535

3636
val mk_reply : id:int -> result:J.t -> J.t
3737

38+
val mk_error_reply : id:int -> code:int -> msg:string -> J.t
39+
(** JSON-RPC error reply. Codes follow the JSON-RPC 2.0 spec; [-32601]
40+
is [MethodNotFound]. *)
41+
3842
val mk_diagnostics
3943
: uri:string
4044
-> version: int

0 commit comments

Comments
 (0)