From 465104a8f9e936f56a3d53482cc6311743b97217 Mon Sep 17 00:00:00 2001 From: Nathan Rebours Date: Tue, 16 Jul 2024 15:09:31 +0200 Subject: [PATCH 1/3] Fix extension removal bug in `Code_path.main_module_name` Signed-off-by: Nathan Rebours --- CHANGES.md | 4 ++++ src/code_path.ml | 7 ++++++- test/code_path/test.ml | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index dc017776e..4ddb0eb17 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,10 @@ details. ### Other changes +- Fix a bug where `Code_path.main_module_name` would not properly remove + extensions from the filename and therefore return an invalid module name. + (#, @NathanReb) + - Add `-unused-type-warnings` flag to the driver to allow users to disable only the generation of warning 34 silencing structure items when using `[@@deriving ...]` on type declarations. (#511, @mbarbin, @NathanReb) diff --git a/src/code_path.ml b/src/code_path.ml index f76713b28..772d3e77d 100644 --- a/src/code_path.ml +++ b/src/code_path.ml @@ -10,9 +10,14 @@ type t = { in_expr : bool; } +let remove_all_extensions basename = + match String.split_on_char ~sep:'.' basename with + | [] -> assert false + | name :: _ -> name + let top_level ~file_path = let main_module_name = - file_path |> Stdlib.Filename.basename |> Stdlib.Filename.remove_extension + file_path |> Stdlib.Filename.basename |> remove_all_extensions |> String.capitalize_ascii in { diff --git a/test/code_path/test.ml b/test/code_path/test.ml index c0aeff2b4..791c551c7 100644 --- a/test/code_path/test.ml +++ b/test/code_path/test.ml @@ -142,3 +142,14 @@ let _ = - : string = "(code_path(main_module_name Test)(submodule_path())(enclosing_module Test)(enclosing_value())(value())(fully_qualified_path Test))" |}] + + +let _ = + (* The main module name should properly remove all extensions *) + let code_path = + Code_path.top_level ~file_path:"some_dir/module_name.cppo.ml" + in + Code_path.main_module_name code_path +[%%expect{| +- : string = "Module_name" +|}] From 1d77918a24ca26713ac902bc203c9256ff2fd38b Mon Sep 17 00:00:00 2001 From: Nathan Rebours Date: Tue, 16 Jul 2024 15:14:31 +0200 Subject: [PATCH 2/3] Update CHANGES.md Signed-off-by: Nathan Rebours Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 4ddb0eb17..07221aaa6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ details. - Fix a bug where `Code_path.main_module_name` would not properly remove extensions from the filename and therefore return an invalid module name. - (#, @NathanReb) + (#512, @NathanReb) - Add `-unused-type-warnings` flag to the driver to allow users to disable only the generation of warning 34 silencing structure items when using From 9f65414f1ebae81c117f0635aefe98d3c0529652 Mon Sep 17 00:00:00 2001 From: Nathan Rebours Date: Wed, 17 Jul 2024 13:52:50 +0200 Subject: [PATCH 3/3] Comment on split_on_char return value assertion There is an assert false in the code there since the documentation states that one of the function invariants is that it never returns an empty list. Signed-off-by: Nathan Rebours --- src/code_path.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code_path.ml b/src/code_path.ml index 772d3e77d..d8490d957 100644 --- a/src/code_path.ml +++ b/src/code_path.ml @@ -12,7 +12,7 @@ type t = { let remove_all_extensions basename = match String.split_on_char ~sep:'.' basename with - | [] -> assert false + | [] -> assert false (* split_on_char never returns the empty list *) | name :: _ -> name let top_level ~file_path =