OCaml-LSP and .merlin files
To ergonomically use Emacs with ocaml-eglot and
ocaml-lsp
to work on an OCaml script that is not compiled by dune, we can
instead fall back on
.merlin
files. If an OCaml script is not compiled by dune in the project, we
might see a No config found for file ... error from
ocaml-lsp. Adding a .merlin file and ensure ocaml-lsp respects
it will resolve this error.
-
Add a
.merlinfile in the script’s directory. In my case, I have a simple script that uses the package containers, so I add:PKG containersIf there are no dependencies then an empty
.merlinfile should do the trick. -
ocaml-lspmust be configured to use.merlinfiles as fallback. Ensure that the--fallback-read-dot-merlinis passed ineglot-server-programs:(use-package neocaml :after eglot :vc (:url "https://github.com/bbatsov/neocaml" :rev :newest) ... :config (add-to-list 'eglot-server-programs '((neocaml-mode :language-id "ocaml") . ("ocamllsp" "--fallback-read-dot-merlin"))) (add-to-list 'eglot-server-programs '((neocaml-interface-mode :language-id "ocaml") . ("ocamllsp" "--fallback-read-dot-merlin"))))I’m using neocaml and for this reason I have to specify it twice, since
neocamlhas two modes: one for implementation files and one for interface files. Make sure to runM-x eglot-reconnector restart Emacs for the changes to take effect on any existing connection (seeM-x eglot-list-connections).