From 25f1a7fd8f365c7bc91d4f891315f3bd2c1094a2 Mon Sep 17 00:00:00 2001 From: Liam Stevenson Date: Fri, 1 Nov 2024 14:37:53 +0000 Subject: [PATCH] Create command for printing magic numbers (#117) * Create command for printing magic numbers * Fix ci failures --- .github/workflows/flambda-backend.yml | 2 +- src/commands/dune | 4 ++- src/commands/new_commands.ml | 5 ++++ src/commands/query_json.ml | 6 ++++- src/frontend/dune | 4 +-- src/frontend/query_commands.ml | 8 ++++-- src/frontend/query_protocol.ml | 2 +- src/ocaml-index/bin/dune | 2 +- src/ocaml-index/bin/ocaml_index.ml | 15 ++++++++++-- src/ocaml/utils/config.ml | 35 +++++++++++++++++++++++++++ src/ocaml/utils/config.mli | 15 ++++++++++++ src/ocaml/utils/dune | 2 +- tests/test-dirs/version.t | 27 +++++++++++++++++++++ 13 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 tests/test-dirs/version.t diff --git a/.github/workflows/flambda-backend.yml b/.github/workflows/flambda-backend.yml index a66909ab0..81b347b8f 100644 --- a/.github/workflows/flambda-backend.yml +++ b/.github/workflows/flambda-backend.yml @@ -102,4 +102,4 @@ jobs: working-directory: merlin-jst run: | export MERLIN_TEST_OCAML_PATH=$GITHUB_WORKSPACE/flambda-backend/_install - opam exec -- dune runtest -p merlin-lib,dot-merlin-reader,merlin + opam exec -- dune runtest -p merlin-lib,dot-merlin-reader,merlin,ocaml-index diff --git a/src/commands/dune b/src/commands/dune index 016727ba0..0d3673c6b 100644 --- a/src/commands/dune +++ b/src/commands/dune @@ -4,6 +4,7 @@ (flags :standard -open Ocaml_parsing + -open Ocaml_utils -open Merlin_utils -open Merlin_kernel) (libraries @@ -11,4 +12,5 @@ merlin-lib.utils merlin-lib.kernel merlin-lib.query_protocol - merlin-lib.query_commands)) + merlin-lib.query_commands + merlin-lib.ocaml_utils)) diff --git a/src/commands/new_commands.ml b/src/commands/new_commands.ml index 30ff6fe39..a0569e5ab 100644 --- a/src/commands/new_commands.ml +++ b/src/commands/new_commands.ml @@ -867,6 +867,11 @@ let all_commands = run buffer (Query_protocol.Signature_help sh) end; (* Used only for testing *) + command "version" ~spec:[] ~default:() ~doc:"Print version information" + begin + fun buffer () -> run buffer Query_protocol.Version + end; + (* Used only for testing *) command "dump" ~spec: [ arg "-what" diff --git a/src/commands/query_json.ml b/src/commands/query_json.ml index d4b556507..856450d7e 100644 --- a/src/commands/query_json.ml +++ b/src/commands/query_json.ml @@ -514,4 +514,8 @@ let json_of_response (type a) (query : a t) (response : a) : json = let with_file = scope = `Project in `List (List.map locations ~f:(fun loc -> with_location ~with_file loc [])) | Signature_help _, s -> json_of_signature_help s - | Version, version -> `String version + | Version, (version, magic_numbers) -> + `Assoc + [ ("version", `String version); + ("magicNumbers", Config.Magic_numbers.to_json magic_numbers) + ] diff --git a/src/frontend/dune b/src/frontend/dune index f04d9329d..7fb6e9866 100644 --- a/src/frontend/dune +++ b/src/frontend/dune @@ -2,8 +2,8 @@ (name query_protocol) (public_name merlin-lib.query_protocol) (modules query_protocol) - (flags :standard -open Merlin_utils -open Merlin_kernel -open Ocaml_parsing -open Merlin_kernel) - (libraries merlin_kernel merlin_utils ocaml_parsing)) + (flags :standard -open Merlin_utils -open Merlin_kernel -open Ocaml_parsing -open Ocaml_utils) + (libraries merlin_kernel merlin_utils ocaml_parsing ocaml_utils)) (library (name query_commands) diff --git a/src/frontend/query_commands.ml b/src/frontend/query_commands.ml index 1e3422080..48ece9bca 100644 --- a/src/frontend/query_commands.ml +++ b/src/frontend/query_commands.ml @@ -978,5 +978,9 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a = function } | None -> None) | Version -> - Printf.sprintf "The Merlin toolkit version %s, for Ocaml %s\n" - Merlin_config.version Sys.ocaml_version + let version = + Printf.sprintf "The Merlin toolkit version %s, for Ocaml %s\n" + Merlin_config.version Sys.ocaml_version + in + let magic_numbers = Config.Magic_numbers.current in + (version, magic_numbers) diff --git a/src/frontend/query_protocol.ml b/src/frontend/query_protocol.ml index aa728e0b8..4dbea186c 100644 --- a/src/frontend/query_protocol.ml +++ b/src/frontend/query_protocol.ml @@ -272,4 +272,4 @@ type _ t = (** In current version, Merlin only uses the parameter [position] to answer signature_help queries. The additionnal parameters are described in the LSP protocol and might enable finer behaviour in the future. *) - | Version : string t + | Version : (string * Config.Magic_numbers.t) t diff --git a/src/ocaml-index/bin/dune b/src/ocaml-index/bin/dune index 1b42c4782..f55b17c52 100644 --- a/src/ocaml-index/bin/dune +++ b/src/ocaml-index/bin/dune @@ -2,7 +2,7 @@ (name ocaml_index) (public_name ocaml-index) (package ocaml-index) - (libraries lib ocaml_typing ocaml_utils merlin_index_format) + (libraries lib ocaml_typing ocaml_utils merlin_index_format merlin_utils yojson) (flags :standard -open Ocaml_typing diff --git a/src/ocaml-index/bin/ocaml_index.ml b/src/ocaml-index/bin/ocaml_index.ml index 98041bf3f..dace271bc 100644 --- a/src/ocaml-index/bin/ocaml_index.ml +++ b/src/ocaml-index/bin/ocaml_index.ml @@ -15,7 +15,13 @@ let rewrite_root = ref false let store_shapes = ref false let do_not_use_cmt_loadpath = ref false -type command = Aggregate | Dump | Dump_file_stats | Stats | Gather_shapes +type command = + | Aggregate + | Dump + | Dump_file_stats + | Stats + | Gather_shapes + | Magic_numbers let parse_command = function | "aggregate" -> Some Aggregate @@ -23,6 +29,7 @@ let parse_command = function | "dump-file-stats" -> Some Dump_file_stats | "stats" -> Some Stats | "gather-shapes" -> Some Gather_shapes + | "magic-numbers" -> Some Magic_numbers | _ -> None let command = ref None @@ -128,5 +135,9 @@ let () = (Hashtbl.length cu_shape) (Option.value ~default:"none" root_directory)) (List.rev !input_files_rev) - | _ -> Printf.printf "Nothing to do.\n%!"); + | Some Magic_numbers -> + let json = Config.Magic_numbers.(to_json current) in + Yojson.Basic.to_channel stdout json; + print_newline () + | None -> Printf.printf "Nothing to do.\n%!"); exit 0 diff --git a/src/ocaml/utils/config.ml b/src/ocaml/utils/config.ml index 8cb70ebee..068d637da 100644 --- a/src/ocaml/utils/config.ml +++ b/src/ocaml/utils/config.ml @@ -48,3 +48,38 @@ let reserved_header_bits = 8 let runtime5 = true let merlin = true + +module Magic_numbers = struct + type t = + { cmi_magic_number : string; + ast_intf_magic_number : string; + ast_impl_magic_number : string; + cmt_magic_number : string; + cms_magic_number : string; + index_magic_number : string + } + + let current = + { cmi_magic_number; + ast_intf_magic_number; + ast_impl_magic_number; + cmt_magic_number; + cms_magic_number; + index_magic_number + } + + let to_json t = + let nums = + [ ("cmi_magic_number", t.cmi_magic_number); + ("ast_intf_magic_number", t.ast_intf_magic_number); + ("ast_impl_magic_number", t.ast_impl_magic_number); + ("cmt_magic_number", t.cmt_magic_number); + ("cms_magic_number", t.cms_magic_number); + ("index_magic_number", t.index_magic_number) + ] + in + `Assoc + (List.map + (fun (key, value) -> (key, Merlin_utils.Std.Json.string value)) + nums) +end diff --git a/src/ocaml/utils/config.mli b/src/ocaml/utils/config.mli index bfca68f37..4d8888ef5 100644 --- a/src/ocaml/utils/config.mli +++ b/src/ocaml/utils/config.mli @@ -46,4 +46,19 @@ val runtime5 : bool val merlin : bool +module Magic_numbers : sig + type t = + { cmi_magic_number : string; + ast_intf_magic_number : string; + ast_impl_magic_number : string; + cmt_magic_number : string; + cms_magic_number : string; + index_magic_number : string + } + + val current : t + + val to_json : t -> Std.json +end + (**/**) diff --git a/src/ocaml/utils/dune b/src/ocaml/utils/dune index 445250c9e..5819bead1 100644 --- a/src/ocaml/utils/dune +++ b/src/ocaml/utils/dune @@ -1,5 +1,5 @@ (library (name ocaml_utils) (public_name merlin-lib.ocaml_utils) - (libraries merlin_config merlin_utils) + (libraries merlin_utils) (flags :standard -open Merlin_utils)) diff --git a/tests/test-dirs/version.t b/tests/test-dirs/version.t new file mode 100644 index 000000000..c3544cf4e --- /dev/null +++ b/tests/test-dirs/version.t @@ -0,0 +1,27 @@ + $ $MERLIN single version | revert-newlines | jq .value + { + "version": "The Merlin toolkit version %VERSION%, for Ocaml 5.2.0\n", + "magicNumbers": { + "cmi_magic_number": "Caml1999I552", + "ast_intf_magic_number": "Caml1999N552", + "ast_impl_magic_number": "Caml1999M552", + "cmt_magic_number": "Caml1999T552", + "cms_magic_number": "Caml1999S552", + "index_magic_number": "Merl2023I552" + } + } + + $ ocaml-index magic-numbers | jq + { + "cmi_magic_number": "Caml1999I552", + "ast_intf_magic_number": "Caml1999N552", + "ast_impl_magic_number": "Caml1999M552", + "cmt_magic_number": "Caml1999T552", + "cms_magic_number": "Caml1999S552", + "index_magic_number": "Merl2023I552" + } + +Verify there is no difference between Merlin and Ocaml-index + $ $MERLIN single version | revert-newlines | jq --sort-keys .value.magicNumbers > merlin-magic-numbers.json + $ ocaml-index magic-numbers | jq --sort-keys > ocaml-index-magic-numbers.json + $ diff merlin-magic-numbers.json ocaml-index-magic-numbers.json