# Using merlin with camlp5

**URL:** <https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082>\
**Category:** Community\
**Tags:** merlin, camlp5\
**Created:** [January 12, 2021, 6:44pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082 "2021-01-12T18:44:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 12, 2021, 6:44pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/1 "2021-01-12T18:44:24Z")

</div>

I created a simple [dune project](https://github.com/Kakadu/ocaml_lsp_camlp5) where I have a library preprocessed with camlp5 (more precisely, as a standalone executable built using camlp5 and which outputs serialized OCaml AST) and another library preprocessed with PPX.

Library 1 is being preprocessed into file `_build/default/lib1.pp.ml` and another into `_build/default/libppx/lib2.pp.ml`. Mimetype of both files is `OCaml abstract syntax tree implementation file (Version 027)`

The problem is that it’s OK to use PPX-preprocessed library in IDE but for camlp5 one I get and error. It seems it treats OCaml abstract syntax tre file as a plain text file.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/ocaml/original/2X/8/8543b1660a12de37c5bc78ef244f7fe9892b1d5a.png)

It seems the same problem appears both in just merlin and ocaml-lsp.

Any thoughts where should I diagnose this issue?

P.S. I create a test for merlin testsuite to reproduce this behaviour.

> **[Kakadu/merlin](https://github.com/Kakadu/merlin/tree/camlp5-test-fail)**
>
> camlp5-test-fail

---

<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:** [January 12, 2021, 8:32pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/2 "2021-01-12T20:32:58Z")

</div>

If I remember correctly, merlin doesn’t have support for the camlp4/5 syntax. And for ppxes, merlin doesn’t read the preprocessed file but runs the ppxes itself.

You can probably follow an approach similar to the one taken by reason. Reason has a different frontend for merlin that is automatically transforming the reason file into an ocaml ast. And merlin is working on this ast. I don’t know if camlp5 generates good enough locations for this to be practical.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 12, 2021, 8:34pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/3 "2021-01-12T20:34:48Z")

</div>

I thought that merlin applies various rewriters and runs custom parser on the result. Am I wrong?

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 12, 2021, 9:11pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/4 "2021-01-12T21:11:44Z")

</div>

Kakadu,

I just want to clarify what you’re seeing:

(1) you have a test-case where merlin or LSP is inhaling serialized AST, and not source-code (via parsing), and it’s blowing up. So if this is a camlp5 bug, it’s due to camlp5’s outputting bad AST, not a parsing issue, yes?

(2) furthermore, these same serialized ASTs are consumed by the OCaml compiler just fine: sans any sort of complaint.

(2) It would be good to -verify- that merlin/lsp is not somehow attempting to invoke camlp5, but I suspect that that’s the case, maybe not high-priority.

(2) I don’t see a standalone test-case. Perhaps a next step would be to take a unit-test from merlin/lsp and modify it to use these serialized AST files, to elicit a failure?

With a standalone unit-test involving merlin/lsp, I could try to explore whether it’s something bad put out by Camlp5.

I’m not a merlin/lsp user, so a full-stack reproduction involving and editor, multiple processes, etc, is Greek to me.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 12, 2021, 9:20pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/5 "2021-01-12T21:20:54Z")

</div>

(1) I’m not sure whose bug it is. If it will indeed be a bug of camlp5 I will let you know

(2) I’m sure merlin/lsp doesn’t invoke camlp5. AFAIU it was design not to handle camp5 from very beginning

(2) I don’t want prepare a unit-test right now. I hope that I will get an answer like `Merlin doesn't care about contents of '*.pp.ml' files, look at another files` or `these files should have another mime type` or something.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 13, 2021, 1:45pm UTC](https://discuss.ocaml.org/t/using-merlin-with-camlp5/7082/6 "2021-01-13T13:45:26Z")

</div>

Maybe the contents of `.merlin` files will help

```nohighlight
$ cat .merlin 
EXCLUDE_QUERY_DIR
B _build/default/.lib1.objs/byte
S .
FLG -pp '/media/oldhome/kakadu/prog/asp/camlp5_pp_demo/_build/default/rewriter.exe'
FLG -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs -rectypes -warn-error -A -w -33

```

```nohighlight
$ cat libppx/.merlin 
EXCLUDE_QUERY_DIR
B ../_build/default/libppx/.lib2.objs/byte
S .
FLG -ppx '/media/oldhome/kakadu/prog/asp/camlp5_pp_demo/_build/default/.ppx/c3461e88ae1503d575d582eb5874e916/ppx.exe --as-ppx --cookie '\''library-name="lib2"'\'''
FLG -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs -rectypes -warn-error -A -w -33

```

I also prepared a [log](https://gist.github.com/Kakadu/ffdec916527051416bbb9ec08af12a7d) while reproducing the same behavior in vim+merlin.
