1

I expected dune to automatically download dependencies when running dune build, but that doesn't happen.

I've tried multiple variants of this, and I tried with ocaml versions 4.14 and 5.0, and with different dune versions written in the dune-project file (although I didn't try to manually download specific dune versions, I wasn't sure that would happen automatically), my globally installed dune version is the latest, 3.10.

This specific set of instructions below was tried in an empty directory, and following the instructions of dune's official documentation.

~> echo '(lang dune 3.10)' > dune-project
~> echo '(executable
          (name hello_world)
          (libraries core)
          (preprocess (pps ppx_jane)))' > dune
~> echo 'open Core

         let () =
           Sexp.to_string_hum [%sexp ([3;4;5] : int list)]
           |> print_endline' > hello_world.ml'
~> dune build
File "dune", line 3, characters 12-16:
3 |  (libraries core)
                ^^^^
Error: Library "core" not found.
-> required by _build/default/hello_world.exe
-> required by alias all
-> required by alias default
File "dune", line 4, characters 18-26:
4 |  (preprocess (pps ppx_jane)))
                      ^^^^^^^^
Error: Library "ppx_jane" not found.
-> required by _build/default/hello_world.pp.ml
-> required by alias all
-> required by alias default

The error I get is always related to dependencies not ever being found. I tried copying other projects, running other tutorials with different sets of dependencies and whatnot, it's always the same error. I tried to manually meddle with an .opam file, but it looks like dune just ignores that and rewrites it.

Running a project without any dependencies works fine.

fiatjaf
  • 11,479
  • 5
  • 56
  • 72

1 Answers1

1

Opam is the package manager for OCaml and its role is installing dependencies.

Dune on the other hand is a build system. The two have distinct roles.

Running dune build will give you errors about missing dependencies, but it should also update your project's opam file, allowing you to install the needed dependencies with opam install . --deps-only. Once this is done, dune build should succeed as long as the missing dependencies are the only source of errors.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • I don't get it. All the documentation and tutorials I could find (including the one I linked above) say that you only need to do `dune build`, never `opam install`, and `dune build` doesn't work. – fiatjaf Sep 01 '23 at 22:21
  • In other projects I did without any dependencies I also noticed that `dune build` **does** create or update the opam file (after it downloads the dependencies, I assume), but in my case it doesn't update or create the opam file. In fact it _empties_ an opam file if one already exists. I assume that is because of the errors. So there is nothing to call `opam install` on. – fiatjaf Sep 01 '23 at 22:21
  • @fiatjaf you need to add `(generate_opam_files true) (package (name hello_world) (depends core ppx_jane)) ` to your dune-project file to generate opam files. A example can be found here, https://github.com/anmonteiro/ocaml-h2/blob/master/dune-project – MaxPlankton Sep 02 '23 at 15:51
  • Thank you, that worked for generating the opam file, but the opam file is missing the dependencies. I thought they would be parsed directly from the `dune` file. – fiatjaf Sep 02 '23 at 16:03