Not able to go-to-definition inside external libraries

Hi,

This was my experience with ocaml-lsp-server.

Issue:

I’m quite happy with it except for this small issue.

I wanted to go-to-definition inside an external library but was unable to do so.

I can go-to-definition to a function/module of an external library but I can not browse inside it.

This has been discussed in depth [here].

Discussion:

As of now I’m quite happy with ocaml-lsp-server due to its efficiency, and because it does not consume too much RAM.

I’m a new learner in OCaml and wanted to read code written by experts to learn more about OCaml.

When I learned Rust I was able to browse through the code of any library, and understand how everything worked.

I read from previous discussions [here] that it is not possible to do so due to some reason, however if we build as an opam-monorepo we could circumvent the restrictions and browse the code of the external.

However I tried to build the otoml library to read toml files using a monorepo but failed to do so.

Solution (kind of):

Since nothing worked, as of now I have git cloned the otoml repo. Now that I have cloned the repo and built it with dune, I can use ocaml-lsp-server to browse through the code with ease.

Conclusion:

I’m pretty happy with most of the things with ocaml, however wished that we could just browse through the code of external libraries as we do in rust.

Since external libraries are pre-compiled they should not change, so is there any strong reason why the LSP can not browse through them?

It would help us learn/use/debug our code as new learners.

Thanks.

2 Likes

What do you mean by “browse”?

You can go to the references of a symbol across files now, as described in this post:

Project-wide occurrences only returns results in the user’s project, not in the installed world.

The main issue is that Merlin’s configuration files, which are generated by the build system, are not part of the installed artifacts. Cloning the sources of the projects you are interested-in into your project (locally vendoring) is the best approach right now.

I think there is ongoing work on odoc to provide source-file navigation along with documentation, which might fit your needs, I don’t think there is any ETA yet though (cc @jonludlam @panglesd).

2 Likes

Thanks for sharing the information regarding Merlin.

I read through their documentation and saw that we can configure it little bit with .merlin files.

[Merlin docs]

Can we manually generate this file within our project directory, to point to the source code installed by opam?

Merlin seems to provide recursive globbing support so I hope that if we can just manually point to the directories where our external library source code is stored then Merlin should be able to provide go-to-definition support for those libraries.

Will this work?

The issue is not your project’s configuration, it is already generated by Dune and aware of externally installed dependencies. That’s why you can jump, from your project, to the source of an external dependency. However, once you’re there there is no more configuration available, and you don’t benefit from merlin/lsp a lot anymore. As of today, there is no easy method to solve this.

The ETA is “pretty soonish”!

Yes, source code navigation generated by odoc for ocaml.org will be a nice feature to navigate a dependency source. As it is pretty new, it won’t be as reliable as good old Merlin. And it does not replace navigating the source from the editor!

It’s my turn to CC someone else: @maiste, might know if dune package management allows (or will allow) to navigate the source of the dependencies using merlin!

2 Likes