"Goto definition" feature not working in hackwaly's OCaml extension on VSCode

Every time I try the “Goto definition” feature I get the answer “No definition found” :

Note that I opened in VSCode the absolute_path.ml file where the definition
of the Absolute_path.to_string function is.
So what is it that prevents VSCode from finding the definition ?

1 Like

Did you compile you project before to try to go to the definition? Merlin (which powers the goto definition feature of vscode-ocaml) is not able to jump to another file if you have not compile your project at least once.

I just recompiled everything from my terminal, but I still get “No definition found”.
I guess I need to compile from “within” VSCode, but how exactly do I do that ?

You don’t need to compile from vscode. Or more exactly, it won’t make any difference. To build from vscode, you need to configure a build task. You can search for build task in the command panel.

Do you have a .merlin file in your project? Did you use an editor like emacs/vim before? If yes, was it working in this editor?

If you don’t have a .merlin file, you may need to create one. https://github.com/ocaml/merlin/wiki/project-configuration

I never used emacs/vim before.
I just created an empty .merlin file in my main project directory, and following the instructions in the page you linked to, added all the needed S <directory> commands into it.
I recompiled but “Goto definition” still doesn’t work.

By the way, I already did opam install merlin and I have version 2.1.2 installed at ~/.opam/system/bin/ocamlmerlin

What further configurations steps need I accomplish ?

Out of curiosity, do type hints work? I.e. when you hover over some value or name, does a tooltip appear with the type?

No, type hints are not working (yet, hopefully)

Sorry, another basic question to establish context; did you associate .ml and .mli files with OCaml in VSCode? By default they’re treated as F# files. The settings are mentioned in the extension documentation:

  "files.associations": {
    "*.ml": "ocaml",
    "*.mli": "ocaml"
  },

You may also need to add B option to the .merlin file to explain merlin where build artifacts are located.

I looks like you are using the system switch. I don’t know if it’s related to your issue, but some tools are not working properly with the system switch. You should probably try to use another switch with opam switch 4.04.1 for example.

Also, if you do switch, you’ll need to reinstall merlin and any other packages you need for that switch, after switching.

I did just that, and properly reinstalled merlin and ocp-indent.
But “Goto definition” still isn’t working at all it seems

Check if “go to definition” works in the same file:

let x = 5

let _ = x (* <- go to definition should jump to first line *)

If it doesn’t work: problem with editor setup
If it works in this case but not in your project, then you need to adjust your build.

Locating external definitions need “cmt” files. Check if you have a “cmt” file for your module, for that you need to pass -bin-annot to the compiler.

1 Like

This is the case for me.
And I do have a cmt file for every compiled module in my project, I (nearly) always use the -bin-annot option with ocamlc.
So how do I fix this “editor setup” ?

I think opam environment is just not availabe from your visual studio code session.

An easy way to try is to start visual studio code from a terminal which is known to be correctly setup:

$ opam switch
# Normal output should be your switch name highlighted among other installed switches.
# If this is displayed:
#   [WARNING] The environment is not in sync with the current switch.
#            You should run: eval `opam config env`
# Run eval `opam config env` to fix it
$ which ocamlmerlin
/.../bin/ocamlmerlin
# merlin is in PATH
$ code
# Launch vscode from this environment.
2 Likes

I did that and nothing changed. Here is the output from my Terminal :

$ opam switch
[ERROR] At
        ~/.opam/compilers/4.03.0/4.03.0+beta2+flambda/4.03.0+beta2+flambda.comp:2:30:
          '+' is not allowed in compiler versions
[ERROR] At ~/.opam/compilers/4.04.0/4.04.0+afl/4.04.0+afl.comp:2:20:
          '+' is not allowed in compiler versions
--     -- 3.07    Official 3.07 release
--     -- 3.08.0  Official 3.08.0 release
--     -- 3.08.1  Official 3.08.1 release
--     -- 3.08.2  Official 3.08.2 release
--     -- 3.08.3  Official 3.08.3 release
--     -- 3.08.4  Official 3.08.4 release
--     -- 3.09.0  Official 3.09.0 release
--     -- 3.09.1  Official 3.09.1 release
--     -- 3.09.2  Official 3.09.2 release
--     -- 3.09.3  Official 3.09.3 release
--     -- 3.10.0  Official 3.10.0 release
--     -- 3.10.1  Official 3.10.1 release
--     -- 3.10.2  Official 3.10.2 release
--     -- 3.11.0  Official 3.11.0 release
--     -- 3.11.1  Official 3.11.1 release
--     -- 3.11.2  Official 3.11.2 release
--     -- 3.12.0  Official 3.12.0 release
--     -- 3.12.1  Official 3.12.1 release
--     -- 4.00.0  Official 4.00.0 release
--     -- 4.00.1  Official 4.00.1 release
--     -- 4.01.0  Official 4.01.0 release
--     -- 4.02.0  Official 4.02.0 release
--     -- 4.02.1  Official 4.02.1 release
--     -- 4.02.2  Official 4.02.2 release
--     -- 4.02.3  Official 4.02.3 release
--     -- 4.03.0  Official 4.03.0 release
--     -- 4.04.0  Official 4.04.0 release
4.04.1  C 4.04.1  Official 4.04.1 release
system  I system  System compiler (4.02.3)
# 245 more patched or experimental compilers, use '--all' to show
 $ which ocamlmerlin
/Users/jonathandoyle/.opam/4.04.1/bin/ocamlmerlin
 $ open /Applications/Visual\ Studio\ Code.app/
 $ 

I infer that you are on MacOS. IIRC that won’t work if you are using open. Setting up environment variables for MacOS apps is a mess, see here for some more details.

Indeed. Thank you for your feedback. Most or all of the info in the link you gave is outdated for my Mac 10.11.3, as I don’t have a ~/.MacOSX directory , nor do I have a launchd.conf file in my /etc/ directory.

The odd thing is that I no longer do this environment.plist either and it nowadays works for me in Emacs.app (I don’t know if they maybe do something on their side noawadays).

You should however at least try to invoke the executable directly from the terminal (not via open). I.e. /Applications/Visual\ Studio\ Code.app/Contents/MacOS/WHATVERTHEECUTABLEISNAMED. You should then have the correct env (according to the link).

Sorry, I just checked and didn’t see this in the thread so far, wanted to make sure: did you set up the environment that OPAM needs before running code? I.e.:

$ eval `opam config env`
$ /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron &
1 Like