0

I am new to OCaml, and I followed the tutorial to set up a developpement environment. I then tried the helloworld example with dune and OCaml but encoutered the following error:

$ dune init exe helloworld
Success: initialized executable component named helloworld
$ dune build
Error: I cannot find the root of the current workspace/project.
If you would like to create a new dune project, you can type:

    dune init project NAME

Otherwise, please make sure to run dune inside an existing project or
workspace. For more information about how dune identifies the root of the
current workspace/project, please refer to
https://dune.readthedocs.io/en/stable/usage.html#finding-the-root

Directory structure:

.
├── _build
│   └── log
├── dune
└── helloworld.ml

dune: 3.0.2
ocaml: 4.11.1

Shon
  • 3,989
  • 1
  • 22
  • 35
douyu
  • 2,377
  • 2
  • 14
  • 27

1 Answers1

7

You can fix this situation by adding a dune-project file at the root of your project filled with this line:

(lang dune 3.0)

However, as per the documentation, dune init exec is not intended for creating projects: it only creates the boilerplate for an executable component within an existing project (see dune init --help or the dune cli manual for details).

To initialize a project, you should run

dune init proj helloworld

This will also generate a template dune-project file along with the recommended project file structure.


NOTE: Prior to dune 3.0 dune build would have created the file for you if it was absent:

❯ dune init exe helloworld
Success: initialized executable component named helloworld
❯ ls
_build  dune  helloworld.ml
❯ dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.9)
❯ ls
_build  dune  dune-project  helloworld.ml

However, since dune 3.0, dune-project is not added automatically when not found in the project.

Lhooq
  • 4,281
  • 1
  • 18
  • 37
  • 3
    Solved, I find [Remove support for creating and editing the dune-project file automatically](https://github.com/ocaml/dune/issues/4108) – douyu Feb 21 '22 at 08:49
  • I added a few additions to clarify the intended use of `dune init exec` vs. `dune init proj`. Besides the breaking change in dune around `dune build` behavior, this problem is due to an incorrect tutorial. I've submitted a fix for the tutorial here: https://github.com/ocaml/ocaml.org/pull/325 – Shon Feb 21 '22 at 12:55
  • Thanks for the edits. From what I understand, `dune init` has always been a "side-project" but a lot of newcomers use it so it's good if we can finally have a good documentation for it. – Lhooq Feb 21 '22 at 14:15