`ocamllsp --fallback-read-dot-merlin`, can't jump to the definition of "compilation unit modules"

I’ve made this repo to experiment with ocamllsp for non-dune projects as a first step to supporting LSP in Alice projects. Everything mostly works as expected except that modules which are compilation units (e.g. a Foo module corresponding to a foo.ml file) can’t be jumped to. Jumping to functions and modules (defined with the module keyword) from other files works fine, including from the standard library. I can even hover over references to compilation unit modules and see their signatures. But when I try to go to the definition of such modules LSP doesn’t seem to know about the definition (vscode says No definition found for Foo for example).

I’ve tested this in vscode and neovim. The former is configured like (using the ocaml platform):

{
    "ocaml.server.args": [
        "--fallback-read-dot-merlin"
    ]
}

…and the latter like (using nvim-lspconfig):

vim.lsp.enable('ocamllsp')
vim.lsp.config('ocamllsp', {
  cmd = { 'ocamllsp', '--fallback-read-dot-merlin' },
})

I’ve tried compiling the code natively and with the bytecode compiler.

I’m using an opam switch with ocam-lsp-server, dot-merlin-reader, ocamlformat, and the OCaml compiler.

To reproduce, in the linked repo, build the project with make, configure your editor as above, open main.ml and try to go to the definition of Foo referenced within.

Currently I’m just using an empty .merlin file. Do I need to do any additional configuration to support jumping to the definition of compilation unit modules? I’ll note that in Dune projects this works as expected.

You should indicate the source and build directories in the .merlin file. Which means:

S .
B .

These should be the default, but maybe having an empty .merlin overrides it ?

If it still doesn’t work with these, please fill an issue.

See Project configuration · ocaml/merlin Wiki · GitHub for more configuration directives.

Ah found it, you need to compile using the flag -bin-annot to generate the cmt files that are necessary for several features, such as jump to definition.

1 Like

Ah excellent, thank you!