# Jbuilder utop from emacs facility

**URL:** <https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176>\
**Category:** Ecosystem\
**Tags:** jbuilder\
**Created:** [June 20, 2018, 2:13pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176 "2018-06-20T14:13:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 2:13pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/1 "2018-06-20T14:13:50Z")

</div>

Hello,

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`

Salutations

---

<div class="post-metadata">

**Author:** ![jeremiedimino](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeremiedimino/32/699_2.png) [@jeremiedimino](https://discuss.ocaml.org/u/jeremiedimino)\
**Post date:** [June 20, 2018, 2:27pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/2 "2018-06-20T14:27:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 2:43pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/3 "2018-06-20T14:43:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeremiedimino](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeremiedimino/32/699_2.png) [@jeremiedimino](https://discuss.ocaml.org/u/jeremiedimino)\
**Post date:** [June 20, 2018, 3:09pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/4 "2018-06-20T15:09:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 3:32pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/5 "2018-06-20T15:32:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 3:45pm UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/6 "2018-06-20T15:45:10Z")

</div>

> [@jeremiedimino](#):
>
> The utop emacs mode should probably be updated to do this directly when it detects a dune project.

That is a good idea, indeed.

---

<div class="post-metadata">

**Author:** ![ins](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ins/32/923_2.png) [@ins](https://discuss.ocaml.org/u/ins)\
**Post date:** [May 3, 2019, 2:21am UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/7 "2019-05-03T02:21:43Z")

</div>

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

```auto
$ 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.

---

<div class="post-metadata">

**Author:** ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)\
**Post date:** [May 3, 2019, 3:25am UTC](https://discuss.ocaml.org/t/jbuilder-utop-from-emacs-facility/2176/8 "2019-05-03T03:25:27Z")

</div>

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.

```auto
  ;; 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.
