How to regenerate the opam files from dune-project?

The documentation mentions “commands like dune build and dune runtest”. This works but unfortunately, it doesn’t work with the commands like dune build <target> that we use to save on build time.

Our use case is:

  1. We keep the opam package constraints in dune-project.
  2. Opam files are derived from dune-project by running dune build and ^C when the programmer remembers to do it.
  3. The opam files are kept under version control.
  4. Our automated build (CI) uses the opam files to install the opam dependencies before building the project.

The problems are:

  • Contributors don’t know or forget to run dune build after editing dune-project and make doesn’t call the exact command dune build either.
  • After getting a CI failure (probably without knowing about dune-project), contributors will modify the opam files manually and it will work until the files get overwritten accidentally by dune build.
  • We don’t want to run a full dune build because it takes too long.

Is there a fast command to regenerate the opam files from dune-project?

A workaround consists in calling this from a makefile:

$ timeout 0.5 dune build

This runs dune build and aborts after 0.5 s, which is sufficient to generate our opam files. The timeout command on my machine is from GNU and I don’t know yet if a compatible equivalent is installed by default on MacOS (the point is I don’t want to have to think about this).

I think the @check alias does the opam file promotion from dune-project (amongst other things Aliases — Dune documentation). Would that still be too slow ? If the opam files are targets might dune build *.opam work ? Might be worth waiting for someone with more dune knowledge :))

1 Like

Unfortunately, it’s too slow in our setup (~1 min). It does invoke compilers, apparently.

Can a make target be added e.g. make opam to just run dune build and ask contributors to run that if they make changes to dune-project? Theoretically at least, the dune-project should not be changing that often, so the expensive dune build step shouldn’t be changing that often either, right?

Then add dune build myproject.opam into the make target that contributors are running anyway.

1 Like

That does work, I’m not sure about the wildcard but if you have a package foo in your dune-project you can run dune build foo.opam to regenerate the opam file.


yes, it does work. Sorry that I didn’t read your message carefully on the first pass.