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]