Skip to content

Commit

Permalink
fix(init): correct behavior of PATH argument
Browse files Browse the repository at this point in the history
Fixes ocaml#9209

Signed-off-by: Shon Feder <shon.feder@gmail.com>
  • Loading branch information
shonfeder committed Dec 10, 2023
1 parent addd24f commit 1d333fc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
12 changes: 9 additions & 3 deletions bin/init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).|}
Expand Down
4 changes: 4 additions & 0 deletions doc/changes/9447_fix_dune_init_proj_with_path.md
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 25 additions & 4 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
31 changes: 31 additions & 0 deletions test/blackbox-tests/test-cases/dune-init.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------------------------------------

Expand Down Expand Up @@ -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
===============================

Expand Down
1 change: 0 additions & 1 deletion test/blackbox-tests/test-cases/github7806.t
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 1d333fc

Please sign in to comment.