From ead16cd988f13ddfef7132493251de4555e44348 Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Fri, 16 Dec 2022 02:39:40 +1100 Subject: [PATCH] Error on "dune init" if dir exists named "dune" (#6705) When running `dune init (exe|lib|test)` in a directory which contains a directory named "dune", prior to this change, the error would be: Error: Is a directory Following this change, the error is now: Error: "/path/to/dune" already exists and is a directory Signed-off-by: Stephen Sherratt --- CHANGES.md | 3 +++ bin/dune_init.ml | 5 +++++ .../init-error-if-dune-is-directory.t | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/blackbox-tests/test-cases/init-error-if-dune-is-directory.t diff --git a/CHANGES.md b/CHANGES.md index f9824eb5c1b..baf94b73f95 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,9 @@ Unreleased ---------- +- Report an error if `dune init ...` would create a "dune" file in a location + which already contains a "dune" directory (#6705, @gridbugs) + - Fix the parsing of alerts. They will now show up in diagnostics correctly. (#6678, @rginberg) diff --git a/bin/dune_init.ml b/bin/dune_init.ml index 37efa0cf14c..c4af2391011 100644 --- a/bin/dune_init.ml +++ b/bin/dune_init.ml @@ -118,6 +118,11 @@ module File = struct let full_path = Path.relative path name in let content = if not (Path.exists full_path) then [] + else if Path.is_directory full_path then + User_error.raise + [ Pp.textf "\"%s\" already exists and is a directory" + (Path.to_absolute_filename full_path) + ] else match Io.with_lexbuf_from_file ~f:Dune_lang.Format.parse full_path with | Dune_lang.Format.Sexps content -> content diff --git a/test/blackbox-tests/test-cases/init-error-if-dune-is-directory.t b/test/blackbox-tests/test-cases/init-error-if-dune-is-directory.t new file mode 100644 index 00000000000..36fb32faf47 --- /dev/null +++ b/test/blackbox-tests/test-cases/init-error-if-dune-is-directory.t @@ -0,0 +1,22 @@ +Report an error if `dune init ...` would create a "dune" file in a location +which already has a "dune" directory. + + $ mkdir dune + + $ dune init exe foo + Error: + "$TESTCASE_ROOT/dune" + already exists and is a directory + [1] + + $ dune init lib foo + Error: + "$TESTCASE_ROOT/dune" + already exists and is a directory + [1] + + $ dune init test foo + Error: + "$TESTCASE_ROOT/dune" + already exists and is a directory + [1]