I was using a single global opam switch for multiple Ocaml projects and things were going well – but then suddenly when I installed some package a bunch of other packages were removed.
So I decided to try creating a different opam switch for each project.
But now all projects are broken because ocaml-lsp-server (ocamllsp on neovim) doesn’t pick the packages for the currently active opam switch, apparently it tries to use the default one (?) so everything is always red all the time and pretty much useless.
I use direnv and then for each project have a local opam switch, the .envrc (direnv config) looks like this:
export OPAMSWITCH="$PWD"
eval $(opam env)
this makes sure the executables installed in the switch are on PATH and thus launching neovim (or any other editor) from the directory will find the correct ocamllsp executable.
This neovim instance was launched from the directory I had the switch on, and in fact I ran the following commands from a terminal launched inside the same neovim instance:
~> opam install ocamlformat ocaml-lsp-server
[NOTE] Package ocaml-lsp-server is already installed (current version is 1.17.0).
[NOTE] Package ocamlformat is already installed (current version is 0.26.1).
~> opam install
~> echo $OPAMSWITCH
/home/ad/code/cassis
~> opam install bigstringaf stdint
which ocamll[NOTE] Package stdint is already installed (current version is 0.7.2).
[NOTE] Package bigstringaf is already installed (current version is 0.9.1).
~> which ocamllsp
/home/ad/code/cassis/_opam/bin/ocamllsp
~> which ocamlc
/home/ad/code/cassis/_opam/bin/ocamlc
And yet nothing works.
Is there a way to check what opam switch ocamllsp is using or tell it to use the current one or something like that?
My neovim config just setups the default 'ocamllsp' config with 'lspconfig'.
Run $ opam switch to see what switch you are currently using.
➜ ~ opam switch
# switch compiler description
/Users/owen/programming/ocaml/fooswitch ocaml-base-compiler.4.14.2 /Users/owen/programming/ocaml/fooswitch
→ /Users/owen/programming/ocaml/barswitch ocaml-base-compiler.5.1.1 /Users/owen/programming/ocaml/barswitch
default ocaml.5.1.0 default
[NOTE] Current switch is set locally through the OPAMSWITCH variable.
The current global system switch is default.
nvim-lspconfig just starts the ocamllsp executable, without any explicit path specification.
Can you post the output of command -v ocamllsp and echo $PATH for further troubleshooting. Maybe there is something weird going on with the shell configuration and ocamllsp from your current switch is not the first one in the PATH.
Thanks. I’d say this looks good. Unfortunately I’ve missed that you’ve already provided the output by the which command. You can also run :LspInfo from Neovim and verify that “cmd:” shows the correct ocamllsp binary.
Just one more basic question. Did you run dune build prior to opening the file in Neovim? I usually get these kind of errors, when starting a fresh project before running build or build --watch the first time.
You must be kidding me. It was the dune build that did the trick!
And I knew that, because I also knew from experience that the LSP would complain of everything before a dune build with the new libraries and so on, but I got so tangled up with the opam switch transition that I didn’t realize this basic step was missing.