From 1d333fc4783a9350cd818868a736807a9569e941 Mon Sep 17 00:00:00 2001 From: Shon Feder Date: Sat, 9 Dec 2023 23:41:01 -0500 Subject: [PATCH] fix(init): correct behavior of PATH argument Fixes #9209 Signed-off-by: Shon Feder --- bin/init.ml | 12 +++++-- .../9447_fix_dune_init_proj_with_path.md | 4 +++ doc/usage.rst | 29 ++++++++++++++--- .../test-cases/dune-init.t/run.t | 31 +++++++++++++++++++ test/blackbox-tests/test-cases/github7806.t | 1 - 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 doc/changes/9447_fix_dune_init_proj_with_path.md diff --git a/bin/init.ml b/bin/init.ml index ea744eb9c833..0c4493085a9d 100644 --- a/bin/init.ml +++ b/bin/init.ml @@ -218,8 +218,12 @@ let project = let name = Dune_lang.Atom.to_string common.name in let root = match path with + (* If a path is given, we use that for the root during project + initialization, creating the path to it if needed. *) + | Some path -> path + (* Otherwise we will use the project's given name, and create a + directory accordingly. *) | None -> name - | Some path -> Filename.concat path name in let builder = Builder.set_root common_builder root in let (_ : Fpath.mkdir_p_result) = Fpath.mkdir_p root in @@ -252,8 +256,10 @@ let group = ; `P {|Run a subcommand with $(b, --help) for for details on it's supported arguments|} ; `P - {|If the optional $(b,PATH) is provided, the component will be created - there. Otherwise, it is created in the current working directory.|} + {|If the optional $(b,PATH) is provided, it must be a path to a directory, and + the component will be created there. Otherwise, it is created in a child of the + current working directory, called $(b, NAME). To initialize a component in the + current working directory, use `.` as the $(b,PATH).|} ; `P {|Any prefix of a $(b,COMMAND)'s name can be supplied in place of full name (as illustrated in the synopsis).|} diff --git a/doc/changes/9447_fix_dune_init_proj_with_path.md b/doc/changes/9447_fix_dune_init_proj_with_path.md new file mode 100644 index 000000000000..b7a0499873d9 --- /dev/null +++ b/doc/changes/9447_fix_dune_init_proj_with_path.md @@ -0,0 +1,4 @@ +- Fix handling of the `PATH` argument to `dune init proj NAME PATH`. An + intermediate directory called `NAME` is no longer created if `PATH` is + supplied, so `dune init proj my_project .` will now initialize a project in + the current working directory. (#9447, fixes #9209, @shonfeder) diff --git a/doc/usage.rst b/doc/usage.rst index 8a1c3d80a796..32f5b689ae31 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -9,9 +9,6 @@ This section describes using ``dune`` from the shell. Initializing Components ======================= -NOTE: The ``dune init`` command is still under development and subject to -change. - Dune's ``init`` subcommand provides limited support for generating Dune file stanzas and folder structures to define components. The ``dune init`` command can be used to quickly add new projects, libraries, tests, and executables without having to @@ -77,7 +74,31 @@ the file and directory, if necessary): (preprocess (pps ppx_inline_tests))) -Consult the manual page using the ``dune init --help`` command for more details. +Initializing components in a specified directory +------------------------------------------------ + +All ``init`` subcommands take an optional ``PATH`` argument, which should be a +path to a directory. When supplied, the component will be created in the +specified directory. E.g., to initialize a project in the current working +directory, run + +.. code:: console + + $ dune init proj my_proj . + +To initialize a project in a directory in some nested path, run + +.. code:: console + + $ dune init proj my_proj path/to/my/project + +If the specified directory does not already exist, it will be created. + +Learning more about the ``init`` commands +----------------------------------------- + +Consult the manual page using the ```dune init --help`` command for more +details. .. _finding-root: diff --git a/test/blackbox-tests/test-cases/dune-init.t/run.t b/test/blackbox-tests/test-cases/dune-init.t/run.t index 4d2bdfaa672a..684e57ff876c 100644 --- a/test/blackbox-tests/test-cases/dune-init.t/run.t +++ b/test/blackbox-tests/test-cases/dune-init.t/run.t @@ -144,6 +144,16 @@ Clean up from the dune file created at an absolute path $ rm -rf $PWD/_test_dir +Add a library in the current working directory + + $ dune init lib test_cwd_lib . + Success: initialized library component named test_cwd_lib + $ test -f dune + +Clean up from the dune file created in the current workding dir + + $ rm dune + Adding a library and an executable dependent on that library ------------------------------------------------------------ @@ -508,6 +518,27 @@ And we we can run the tests: test_new_lib_proj alias test/runtest Leaving directory 'new_lib_proj' + +Initializing projects using the PATH argument +============================================= + +We can init a project in a specified directory: + + $ dune init proj proj_at_path path/to/project + Entering directory 'path/to/project' + Success: initialized project component named proj_at_path + Leaving directory 'path/to/project' + $ test -f path/to/project/dune-project + +A project can be initialized in the current directory (addressing +https://github.com/ocaml/dune/issues/9209): + + $ mkdir a-new-dir + $ cd a-new-dir + $ dune init proj project_in_cwd . + Success: initialized project component named project_in_cwd + $ test -f dune-project + Initializing projects using Esy =============================== diff --git a/test/blackbox-tests/test-cases/github7806.t b/test/blackbox-tests/test-cases/github7806.t index 40defe853916..8e348989b9a3 100644 --- a/test/blackbox-tests/test-cases/github7806.t +++ b/test/blackbox-tests/test-cases/github7806.t @@ -1,5 +1,4 @@ $ dune init project name $PWD 2>&1 | head -n 8 - Entering directory 'name' Internal error, please report upstream including the contents of _build/log. Description: ("[as_in_source_tree_exn] called on something not in source tree",