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 16, 2023
1 parent 0fb3b9b commit 6fe06e9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 38 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)
30 changes: 27 additions & 3 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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 Expand Up @@ -309,8 +333,8 @@ must be prefixed by the shortest one.

.. _dune-subst:

dune subst
==========
``dune subst``
==============

One of the features ``dune-release`` provides is watermarking; it replaces
various strings of the form ``%%ID%%`` in all your project files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ directory in the current directory.
test
$ dune init proj foo bar
Entering directory 'bar/foo'
Entering directory 'bar'
Success: initialized project component named foo
Leaving directory 'bar/foo'
Leaving directory 'bar'
$ ls
bar
foo
$ ls bar
foo
$ ls bar/foo
_build
bar
bin
Expand All @@ -35,16 +33,14 @@ directory in the current directory.
$ mkdir baz
$ dune init proj foo baz
Entering directory 'baz/foo'
Entering directory 'baz'
Success: initialized project component named foo
Leaving directory 'baz/foo'
Leaving directory 'baz'
$ ls
bar
baz
foo
$ ls baz
foo
$ ls baz/foo
_build
baz
bin
Expand Down
77 changes: 54 additions & 23 deletions test/blackbox-tests/test-cases/dune-init.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Setup mutable files
> (name test_lib))
> EOF
Adding a library
Adding a Library
----------------
Can init a public library
Expand Down Expand Up @@ -61,7 +61,7 @@ Clean up library with inlines tests
$ rm -rf ./_inline_tests_lib
Adding an executable
Adding an Executable
--------------------
Can init a public executable
Expand Down Expand Up @@ -90,7 +90,7 @@ Clean up the executable tests
$ rm -rf ./_test_bin_dir
Adding tests
Adding Tests
------------
Can init tests
Expand All @@ -109,7 +109,7 @@ Clean up the test tests
$ rm -rf ./_test_tests_dir
Adding components to default and non-standard places
Adding Components to Default and Non-Standard Places
---------------------------------------------------
Add a library in the current working directory
Expand All @@ -124,27 +124,37 @@ Clean the library creation
$ rm ./dune
Add a library to a dune file in a specified directory
Add a library to a `dune` file in a specified directory
$ dune init lib test_lib ./_test_dir
Success: initialized library component named test_lib
$ test -f ./_test_dir/dune
Clean up from the dune file created in ./_test_dir
Clean up from the `dune` file created in ./_test_dir
$ rm -rf ./_test_dir
Add a library to a dune file in a directory specified with an absolute path
Add a library to a `dune` file in a directory specified with an absolute path
$ dune init lib test_lib $PWD/_test_dir
Success: initialized library component named test_lib
$ test -f $PWD/_test_dir/dune
Clean up from the dune file created at an absolute path
Clean up from the `dune` file created at an absolute path
$ rm -rf $PWD/_test_dir
Adding a library and an executable dependent on that library
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
------------------------------------------------------------
Can init a library and dependent executable in a combo project
Expand Down Expand Up @@ -175,7 +185,7 @@ Clean up the combo project
$ rm -rf ./_test_lib_exe_dir
Adding libraries in a single directory
Adding Libraries in a Single Directory
--------------------------------------
Can add multiple libraries in the same directory
Expand Down Expand Up @@ -204,7 +214,7 @@ Clan up the multiple library project
$ rm -rf ./_test_lib
Multiple ppxs and library dependencies
Multiple PPXs and Library Dependencies
--------------------------------------
Can add multiple library dependencies in one command
Expand Down Expand Up @@ -234,7 +244,7 @@ Will not overwrite existing files
$ cat ./existing_project/bin/main.ml
() = print_endline "Goodbye"
Comments in dune files are preserved
Comments in `dune` files are preserved
$ dune init lib test_lib2 ./existing_project/src
Success: initialized library component named test_lib2
Expand All @@ -261,7 +271,7 @@ Will not create components with invalid names
$ test -f ./_test_lib
[1]
Adding fields to existing stanzas
Adding Fields to Existing Stanzas
---------------------------------
# TODO(shonfeder)
Expand All @@ -284,10 +294,10 @@ Adding fields to existing stanzas is currently not supported
(name test_bin)
(libraries test_lib1))
Creating projects
Creating Projects
-----------------
Initializing executable projects
Initializing Executable Projects
================================
We can init a new executable project:
Expand All @@ -297,7 +307,7 @@ We can init a new executable project:
Success: initialized project component named new_exec_proj
Leaving directory 'new_exec_proj'
The generated project contains all expected sub-components:
The generated project contains all expected subcomponents:
$ ls new_exec_proj/**
new_exec_proj/dune-project
Expand All @@ -317,7 +327,7 @@ The generated project contains all expected sub-components:
dune
test_new_exec_proj.ml
In particular, the dune-project file has the expected content:
In particular, the `dune-project` file has the expected content:
$ cat new_exec_proj/dune-project | sed 's/dune [0-9].[0-9]*/dune $version/g'
(lang dune $version)
Expand Down Expand Up @@ -401,7 +411,7 @@ We can build and run the project's tests:
Entering directory 'new_exec_proj'
Leaving directory 'new_exec_proj'
Initializing library projects
Initializing Library Projects
================================
We can init a new library project:
Expand All @@ -411,7 +421,7 @@ We can init a new library project:
Success: initialized project component named new_lib_proj
Leaving directory 'new_lib_proj'
The generated project contains all expected sub-components:
The generated project contains all expected subcomponents:
$ ls new_lib_proj/**
new_lib_proj/dune-project
Expand All @@ -427,7 +437,7 @@ The generated project contains all expected sub-components:
dune
test_new_lib_proj.ml
In particular, the dune-project file has the expected content:
In particular, the `dune-project` file has the expected content:
$ cat new_lib_proj/dune-project | sed 's/dune [0-9].[0-9]*/dune $version/g'
(lang dune $version)
Expand Down Expand Up @@ -508,7 +518,28 @@ And we we can run the tests:
test_new_lib_proj alias test/runtest
Leaving directory 'new_lib_proj'
Initializing projects using Esy
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
===============================
We can init a project using Esy:
Expand All @@ -518,7 +549,7 @@ We can init a project using Esy:
Success: initialized project component named new_esy_proj
Leaving directory 'new_esy_proj'
The esy project contains all expected sub-components:
The `esy` project contains all expected subcomponents:
$ ls new_esy_proj/**
new_esy_proj/dune-project
Expand All @@ -538,7 +569,7 @@ The esy project contains all expected sub-components:
dune
test_new_esy_proj.ml
And the dune-project file does NOT specify generation of an opam file:
And the `dune-project` file does NOT specify generation of an opam file:
$ cat new_esy_proj/dune-project | grep "generate_opam_files"
(generate_opam_files false)
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/github7806.t
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
$ 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",
{ t =
External
"$TESTCASE_ROOT"
})
Raised at Stdune__Code_error.raise in file

0 comments on commit 6fe06e9

Please sign in to comment.