OCaml-LSP and .merlin files

Posted on Mar 11, 2026

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.

  1. Add a .merlin file in the script’s directory. In my case, I have a simple script that uses the package containers, so I add:

    PKG containers
    

    If there are no dependencies then an empty .merlin file should do the trick.

  2. ocaml-lsp must be configured to use .merlin files as fallback. Ensure that the --fallback-read-dot-merlin is passed in eglot-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 neocaml has two modes: one for implementation files and one for interface files. Make sure to run M-x eglot-reconnect or restart Emacs for the changes to take effect on any existing connection (see M-x eglot-list-connections).