Using OCaml LSP and Vim with .mll and .mly files

I’m configuring my Vim for an OCaml project, and I’m having issues with .mll and .mly files. My setup uses ocaml-lsp-server with Vim’s coc plugin in addition to the vim-ocaml plugin. My OCaml project uses dune, ocamllex, and menhir.

Vim detects .mll and .mly files as OCaml files, but OCaml LSP doesn’t seem to understand them at all. It raises syntax errors or “unbound record field” errors as if they were .ml syntax.

The LSP works fine with the project’s .ml files, and dune can compile everything (including the .mll and .mly files) without any errors.

I don’t necessarily need the LSP to run on my .mll and .mly files, but I also don’t want it reporting false errors that pollute my diagnostics.

I searched but I couldn’t find any documentation or discussion about this, so I wonder if I’m just missing something obvious. I’m not sure which specific plugin or setting could be the culprit.

1 Like

I searched but I couldn’t find any documentation or discussion about this, so I wonder if I’m just missing something obvious. I’m not sure which specific plugin or setting could be the culprit.

I believe this behaviour happens because vim reports all kinds of different files with the ocaml filetype, which confuses ocaml-lsp-server.

I started an effort to make vim report the different types of ocaml files as different filetypes to avoid this situation, but it’s not the standard yet because not the whole vim ecosystem has been ported yet to support these. (Ironically neither merlin nor ocaml-lsp-server (the ocaml tooling) have been ported yet.

You can find the effort in: Use ocamlinterface (mli), ocamllex (mll) and menhir (mly) filetypes by undu · Pull Request #61 · ocaml/vim-ocaml · GitHub
Installing the branch as a vim plugin will make the errors go away on mll and mly files, but you’ll lose lsp support in .mli files.

2 Likes

I started a branch of Merlin (the backend of ocaml-lsp) to add support for mll and mly files: GitHub - ocaml/merlin at lex-n-yacc.

The support for ocamllex (mll) is quite complete, we could merge it soon, but good support for ,mly files will take a lot of time. And I have no time to work on that anytime soon.

4 Likes

Thank you both for the information. If supporting mll and mly files isn’t imminent, would it make sense to add a disclaimer about that limitation to the ocaml-lsp readme? Then at least other users won’t be confused as to why it doesn’t work as expected.

I’ll keep an eye on those pull requests, and I look forward to when mll and mly support is added!

2 Likes