From 0f1c50ba54b085d5a106c5348c73fcf4148fc602 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Wed, 13 Sep 2023 17:01:47 +0200 Subject: [PATCH] fix(init): check that module names are valid Fixes #8252 When creating a library (directly or indirectly, through a project), the component name should be a valid module name. This restriction does not apply to executable names. Signed-off-by: Etienne Millon --- bin/dune_init.ml | 9 +++++++++ doc/changes/init-module-names.md | 1 + test/blackbox-tests/test-cases/dune-init/github8252.t | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 doc/changes/init-module-names.md diff --git a/bin/dune_init.ml b/bin/dune_init.ml index 33f7d8a0a57..bc8a4bb9a91 100644 --- a/bin/dune_init.ml +++ b/bin/dune_init.ml @@ -195,6 +195,14 @@ module Init_context = struct ;; end +let check_module_name name = + let s = Dune_lang.Atom.to_string name in + let (_ : Dune_rules.Module_name.t) = + Dune_rules.Module_name.of_string_user_error (Loc.none, s) |> User_error.ok_exn + in + () +;; + module Public_name = struct include Lib_name module Pkg = Dune_lang.Package_name.Opam_compatible @@ -346,6 +354,7 @@ module Component = struct ;; let library (common : Options.Common.t) { Options.Library.inline_tests; public } = + check_module_name common.name; let common = if inline_tests then ( diff --git a/doc/changes/init-module-names.md b/doc/changes/init-module-names.md new file mode 100644 index 00000000000..9762086ae6a --- /dev/null +++ b/doc/changes/init-module-names.md @@ -0,0 +1 @@ +- init: check that module names are valid (#8644, fixes #8252, @emillon) diff --git a/test/blackbox-tests/test-cases/dune-init/github8252.t b/test/blackbox-tests/test-cases/dune-init/github8252.t index 685c8c64e8b..01d8bca5436 100644 --- a/test/blackbox-tests/test-cases/dune-init/github8252.t +++ b/test/blackbox-tests/test-cases/dune-init/github8252.t @@ -2,5 +2,9 @@ $ dune init project 01_module Entering directory '01_module' - Success: initialized project component named 01_module + Error: "01_module" is an invalid module name. + Module names must be non-empty, start with a letter, and composed only of the + following characters: 'A'..'Z', 'a'..'z', '_', ''' or '0'..'9'. + Hint: M01_module would be a correct module name Leaving directory '01_module' + [1]