How to build the dune-based project from inside the Emacs

Hi all,

I moved my project into dunes and now I have a probably very stupid question:
Let’s say, I’m in Emacs editing my dune or .ml file. How can I start building my entire project from inside Emacs and get the list of all errors in all files? I did try С-с С-с with “dune build @install” and “make -f /path/to/my/project/Makefile”. Doesn’t work.

Thanks a lot in advance :slight_smile:

cd to your project root before running dune may help
dune build @check builds the minimum to get the list of errors

Gaëtan Gilbert

Wow! Thanks :slight_smile:

cd really helped which is a little bit confusing as from the command line I can run dune from every subdirectory of the project and everything works as expected.

1 Like

I use this snippet so pressing M-c will @check everything, IMHO it works well:

(defun eg-compile ()
  (let* ((default-directory
           (or (locate-dominating-file buffer-file-name "Makefile") default-directory))
         (compile-command (concat "(cd " default-directory " && dune build @check)"))
          (or (locate-dominating-file buffer-file-name "Makefile") nil)))

(eval-after-load 'tuareg
     (require 'merlin-xref)
     (add-hook 'xref-backend-functions #'merlin-xref-backend)
     (define-key tuareg-mode-map "\M-c" 'eg-compile)
     (define-key tuareg-mode-map "\M-p" 'merlin-error-prev)
     (define-key tuareg-mode-map "\M-n" 'merlin-error-next)))

Thanks a lot, works for me too :slight_smile:

I’ve been looking for a solution that won’t require keeping a terminal session open, and while the accepted approach here gets me close, it doesn’t quite work out. If I use @ejgallego’s function as is, then it doesn’t pick up my opam switch environment. But if I prefix the build command with opam exec -- <...> then it seems to not be depositing the build artifacts in the correct location!

I’ll keep noodling around on this, but if anyone knows a better way off the top of their heads I’d be grateful for it.

Ultimately, I’d like an emacs integration with dune --watch, that will give me push notifications when something in the project breaks, rather than having to pull that info out by telling dune to build manually. But I’d be happy if I could get a clean build/fix cycle working inside of emacs for a start :slight_smile:

I’ve written a fairly small emacs minor mode that does exactly this:

GIF for the skeptical:


This is great! Thanks so much for sharing, @Gopiandcode :smiley: It makes my weekend.

I’ve played with a lit a little, and already find it very useful! I imagine it could be noodled on to provide an really wonderful, minimally invasive but maximally informative, assistant to ocaml dev.

Would you be interested in submitting this upstream so it could become part of the official dune-mode or tuareg (unless there’s a better fit)? Or, if not, on getting packaged and into melpa?

I’d be happy to help with these things.

1 Like

Hi @shonfeder, thanks for the feedback! If there’s interest in integrating with dune-mode or tuareg or publishing it as a standalone package, I’d be happy to do so.

1 Like

That is very cool, please do. :slight_smile: