Skip to content

Commit

Permalink
[flow] Split src/server/protocols into two targets so that ServerProt…
Browse files Browse the repository at this point in the history
… doesn't depend on logger

Summary:
ServerProt should be used for LSP stuff, which should be able to run in the browser. Therefore, we shouldn't make it depend on any logging stuff.

With this cleanup, we can revert the duplication introduced in D55555436 and D55555440 (also done in this diff).

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D55645465

fbshipit-source-id: 8ab3b93311956d311b6a5481fa9767e20afc7831
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 2, 2024
1 parent dc9c47c commit 45043cd
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 174 deletions.
5 changes: 4 additions & 1 deletion src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,10 @@ let rec connect_and_make_request flowconfig_name =
(* Sends the command over the socket *)
let send_command ?timeout (oc : out_channel) (cmd : ServerProt.Request.command) : unit =
let command =
{ ServerProt.Request.client_logging_context = FlowEventLogger.get_context (); command = cmd }
{
ServerCommandWithContext.client_logging_context = FlowEventLogger.get_context ();
command = cmd;
}
in
Marshal_tools.to_fd_with_preamble ?timeout (Unix.descr_of_out_channel oc) command |> ignore;
flush oc
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
(modules commandConnect commandConnectSimple commandMeanKill)
(libraries
flow_exit_status
flow_protocols
flow_server_files
flow_server_protocol
flow_server_status
marshal_tools_lwt
socket ; hack
Expand Down
13 changes: 7 additions & 6 deletions src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ let mk_loc file line col =
}

let autocomplete filename content line col js_config_object :
(AutocompleteService_js.AcCompletion.t, string) result =
(ServerProt.Response.Completion.t, string) result =
let filename = File_key.SourceFile filename in
let root = File_path.dummy_path in
let cursor_loc = Loc.cursor (Some filename) line col in
Expand Down Expand Up @@ -413,7 +413,7 @@ let autocomplete filename content line col js_config_object :
loc
in
(match result with
| AcEmpty _ -> Ok { AcCompletion.items = []; is_incomplete = false }
| AcEmpty _ -> Ok { ServerProt.Response.Completion.items = []; is_incomplete = false }
| AcFatalError msg -> Error msg
| AcResult { result; errors_to_log = _ } -> Ok result)
in
Expand Down Expand Up @@ -553,12 +553,13 @@ let loc_as_range_to_json loc =

let completion_item_to_json
{
AutocompleteService_js.AcCompletion.name;
ServerProt.Response.Completion.name;
kind;
description = _;
itemDetail;
labelDetail = _;
documentation_and_tags = (lazy (documentation, _));
documentation;
tags = _;
preselect = _;
sort_text = _;
text_edit;
Expand Down Expand Up @@ -586,7 +587,7 @@ let completion_item_to_json
let props =
match text_edit with
| None -> props
| Some { AutocompleteService_js.AcCompletion.newText; insert; replace } ->
| Some { ServerProt.Response.newText; insert; replace } ->
("insertText", JSON_String newText)
:: ( "range",
JSON_Object
Expand All @@ -612,7 +613,7 @@ let autocomplete js_file js_content js_line js_col js_config_object =
let line = Js.parseInt js_line in
let col = Js.parseInt js_col in
match autocomplete filename content line col js_config_object with
| Ok { AutocompleteService_js.AcCompletion.items; is_incomplete } ->
| Ok { ServerProt.Response.Completion.items; is_incomplete } ->
let open Hh_json in
JSON_Object
[
Expand Down
2 changes: 1 addition & 1 deletion src/monitor/connections/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(libraries
flow_common_lwt
flow_monitor_logger
flow_server_protocol
flow_protocols
lwt.unix
utils_core
flow_service_inference)
Expand Down
2 changes: 1 addition & 1 deletion src/monitor/connections/ephemeralConnection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* lived clients. *)

include FlowServerMonitorConnection.Make (struct
type in_message = ServerProt.Request.command_with_context
type in_message = ServerCommandWithContext.t

type out_message = MonitorProt.monitor_to_client_message
end)
2 changes: 1 addition & 1 deletion src/monitor/connections/ephemeralConnection.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

include
FlowServerMonitorConnection.CONNECTION
with type in_message := ServerProt.Request.command_with_context
with type in_message := ServerCommandWithContext.t
and type out_message := MonitorProt.monitor_to_client_message
4 changes: 2 additions & 2 deletions src/monitor/flowServerMonitorServer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Logger = FlowServerMonitorLogger

type command =
| Write_ephemeral_request of {
request: ServerProt.Request.command_with_context;
request: ServerCommandWithContext.t;
client: EphemeralConnection.t;
}
| Write_persistent_request of {
Expand Down Expand Up @@ -734,7 +734,7 @@ let start monitor_options =
let send_request ~client ~request =
Logger.debug
"Adding request (%s) to the command stream"
(ServerProt.Request.to_string request.ServerProt.Request.command);
(ServerProt.Request.to_string request.ServerCommandWithContext.command);
push_to_command_stream (Some (Write_ephemeral_request { request; client }))

let send_persistent_request ~client_id ~request =
Expand Down
3 changes: 1 addition & 2 deletions src/monitor/flowServerMonitorServer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* LICENSE file in the root directory of this source tree.
*)

val send_request :
client:EphemeralConnection.t -> request:ServerProt.Request.command_with_context -> unit
val send_request : client:EphemeralConnection.t -> request:ServerCommandWithContext.t -> unit

val send_persistent_request :
client_id:LspProt.client_id -> request:LspProt.request_with_metadata -> unit
Expand Down
9 changes: 3 additions & 6 deletions src/monitor/requestMap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
*)

val add :
request:ServerProt.Request.command_with_context ->
client:EphemeralConnection.t ->
MonitorProt.request_id Lwt.t
request:ServerCommandWithContext.t -> client:EphemeralConnection.t -> MonitorProt.request_id Lwt.t

val remove :
request_id:MonitorProt.request_id ->
(ServerProt.Request.command_with_context * EphemeralConnection.t) option Lwt.t
(ServerCommandWithContext.t * EphemeralConnection.t) option Lwt.t

val remove_all :
unit -> (ServerProt.Request.command_with_context * EphemeralConnection.t) list Lwt.t
val remove_all : unit -> (ServerCommandWithContext.t * EphemeralConnection.t) list Lwt.t

val cardinal : unit -> int
2 changes: 1 addition & 1 deletion src/monitor/rpc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(name flow_monitor_rpc)
(wrapped false)
(libraries
flow_server_protocol
flow_protocols
marshal_tools_lwt
sys_utils ; hack
))
75 changes: 4 additions & 71 deletions src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -435,63 +435,18 @@ let autocomplete
let open Hh_json in
match results_res with
| AcResult { result; errors_to_log } ->
let { AcCompletion.items; is_incomplete } = result in
let { ServerProt.Response.Completion.items; is_incomplete = _ } = result in
let result_string =
match (items, errors_to_log) with
| (_, []) -> "SUCCESS"
| ([], _ :: _) -> "FAILURE"
| (_ :: _, _ :: _) -> "PARTIAL"
in
let at_least_one_result_has_documentation =
Base.List.exists
items
~f:(fun AcCompletion.{ documentation_and_tags = (lazy (docs, _)); _ } ->
Base.Option.is_some docs
Base.List.exists items ~f:(fun ServerProt.Response.Completion.{ documentation; _ } ->
Base.Option.is_some documentation
)
in
let result =
let open ServerProt.Response.Completion in
let items =
Base.List.map
items
~f:(fun
{
AcCompletion.kind;
name;
labelDetail;
description;
itemDetail;
text_edit;
additional_text_edits;
sort_text;
preselect;
documentation_and_tags = (lazy (documentation, tags));
log_info;
insert_text_format;
}
->
{
ServerProt.Response.Completion.kind;
name;
labelDetail;
description;
itemDetail;
text_edit =
Base.Option.map text_edit ~f:(fun { AcCompletion.newText; insert; replace } ->
{ ServerProt.Response.newText; insert; replace }
);
additional_text_edits;
sort_text;
preselect;
documentation;
tags;
log_info;
insert_text_format;
}
)
in
{ items; is_incomplete }
in
( Ok (token_opt, result, ac_loc, ac_type_string),
("result", JSON_String result_string)
:: ("count", JSON_Number (items |> List.length |> string_of_int))
Expand Down Expand Up @@ -1832,7 +1787,7 @@ let handle_nonparallelizable_ephemeral ~genv ~request_id ~client_context ~worklo
{ WorkloadStream.workload_should_be_cancelled; workload_handler }

let enqueue_or_handle_ephemeral genv (request_id, command_with_context) =
let { ServerProt.Request.client_logging_context = client_context; command } =
let { ServerCommandWithContext.client_logging_context = client_context; command } =
command_with_context
in
let cmd_str = spf "%s: %s" request_id (ServerProt.Request.to_string command) in
Expand Down Expand Up @@ -2445,28 +2400,6 @@ let handle_persistent_signaturehelp_lsp
in
(match func_details with
| Ok details ->
let details =
match details with
| None -> None
| Some (details, n) ->
Some
( Base.List.map
details
~f:(fun { Signature_help.func_documentation; param_tys; return_ty } ->
{
ServerProt.Response.func_documentation;
param_tys =
Base.List.map
param_tys
~f:(fun { Signature_help.param_documentation; param_name; param_ty } ->
{ ServerProt.Response.param_documentation; param_name; param_ty }
);
return_ty;
}
),
n
)
in
let r = SignatureHelpResult (Flow_lsp_conversions.flow_signature_help_to_lsp details) in
let response = ResponseMessage (id, r) in
let has_any_documentation =
Expand Down
2 changes: 1 addition & 1 deletion src/server/command_handler/commandHandler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*)

val enqueue_or_handle_ephemeral :
ServerEnv.genv -> MonitorProt.request_id * ServerProt.Request.command_with_context -> unit
ServerEnv.genv -> MonitorProt.request_id * ServerCommandWithContext.t -> unit

val enqueue_persistent :
ServerEnv.genv -> LspProt.client_id -> LspProt.request_with_metadata -> unit
2 changes: 1 addition & 1 deletion src/server/persistent_connection/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
flow_common_lsp_conversions
flow_common_utils_filename_cache
flow_monitor_rpc
flow_server_protocol
flow_protocols
flow_types))
14 changes: 13 additions & 1 deletion src/server/protocol/dune
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
(library
(name flow_server_protocol)
(wrapped false)
(modules serverProt)
(libraries
flow_autofix_options
flow_common
flow_common_profiling
flow_coverage
flow_parser_utils_replacement_printer
flow_server_status
flow_typing
lsp ; hack
)
(preprocess
(pps ppx_deriving.eq ppx_deriving.show)))

(library
(name flow_protocols)
(wrapped false)
(modules
(:standard \ serverProt))
(libraries
flow_server_protocol
flow_common_profiling
flow_exit)
(preprocess
(pps ppx_deriving.eq ppx_deriving.show)))
2 changes: 1 addition & 1 deletion src/server/protocol/monitorProt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type please_die_reason = MonitorExiting of (Exit.t * string)
(* These are the messages that the monitor sends to the server *)
type monitor_to_server_message =
(* A request from an ephemeral socket connection. It expects a response *)
| Request of request_id * ServerProt.Request.command_with_context
| Request of request_id * ServerCommandWithContext.t
(* A notification that there is a new persistent socket connection *)
| NewPersistentConnection of LspProt.client_id * Lsp.Initialize.params
(* A request from a persistent socket connection. It does not expect a response *)
Expand Down
11 changes: 11 additions & 0 deletions src/server/protocol/serverCommandWithContext.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

type t = {
client_logging_context: FlowEventLogger.logging_context;
command: ServerProt.Request.command;
}
5 changes: 0 additions & 5 deletions src/server/protocol/serverProt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ module Request = struct
| `File file -> File_path.to_string file
in
Printf.sprintf "save-state %s" out

type command_with_context = {
client_logging_context: FlowEventLogger.logging_context;
command: command;
}
end

module Response = struct
Expand Down
Loading

0 comments on commit 45043cd

Please sign in to comment.