Jbuilder utop from emacs facility


does anyone has any facility to run jbuilder utop from emacs, or a solution simpler than my current one?

I first run jbuilder utop mylib from command line once (in order to generate the utop.exe),
and from emacs, I run the utop command and manually enter something like:
opam config exec -- /path/to/my/project/_build/default/mylib/.utop/utop.exe -emacs


You could also run this in emacs: opam config exec -- jbuilder utop -- -emacs. It avoids having to look inside the _build directory. The utop emacs mode should probably be updated to do this directly when it detects a dune project.

This is something I tried, but I got:

Process utop exited with code 1

I suspect it is a problem of environment: I don’t know from where M-x utop is starting the sub-process.

I believe it is running it from the same directory as the current buffer. You could check by running Sys.getcwd () in a classic utop buffer.

Oh my!

I forgot to pass my library dir to jbuilder utop.

It works fine with the following:

opam config exec -- jbuilder utop mylib -- -emacs

Thanks for your help!

That is a good idea, indeed.

Just FYI, I was able to get this working recently by doing:

$ emacs src/expr.ml
M-x utop <RET>
opam config exec -- dune utop . -- -emacs <RET>

Even though it hasn’t been updated in a while, it still works pretty well. The completion is the thing I miss most. I use Merlin + company for emacs completion but it seems you can’t have both merlin-mode mode and utop-mode running simultaneously? The completion seems wonky but I’m probably doing something wrong.

I have some similar setting (with an ugly script to launch this command from the root of the dune-project). And a similar problem regarding merlin and utop completion. Utop adds its completion backend to the global list of company backends. And if it does so after merlin, then utop completion now has the lead and will basically break the completion everywhere.

  ;; add company completion hook:
  (with-eval-after-load 'company
    (add-to-list 'company-backends #'utop-company-backend))

It might not be hard to fix utop mode. Probably wrapping the body of utop-company-backend into a (when utop-mode ...) should be enough.