ocamldune

OCaml: dune build error after running `dune init exec`


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


Solution

  • 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.