ocaml / ocaml-lsp

OCaml Language Server Protocol implementation
Other
747 stars 117 forks source link

[NeoVim lsp] Unable to find root directory #1050

Open anzhi0708 opened 1 year ago

anzhi0708 commented 1 year ago

The error message from ~/.local/state/nvim/lsp.log:

[START][2023-03-13 22:40:41] LSP logging initiated
[WARN][2023-03-13 22:40:41] .../lua/vim/lsp.lua:1065    "server_request: no handler found for"  "workspace/diagnostic/refresh"
[WARN][2023-03-13 22:40:41] .../lua/vim/lsp.lua:1065    "server_request: no handler found for"  "workspace/diagnostic/refresh"
[START][2023-03-13 22:41:19] LSP logging initiated
[WARN][2023-03-13 22:41:19] ...lsp/handlers.lua:487 'failed to poll dune registry. Unix.Unix_error(Unix.ENOENT, "stat", "/Users/aj/.local/share/dune/rpc")'

OCaml version:

➜  hello_world ocaml --version
The OCaml toplevel, version 5.0.0

OPAM version:

➜  hello_world opam --version
2.1.4

Packages installed:

``` # Packages matching: installed # Name # Installed # Synopsis base-bigarray base base-bytes base Bytes library distributed with the OCaml comp base-domains base base-nnp base Naked pointers prohibited in the OCaml heap base-threads base base-unix base chrome-trace 3.7.0 Chrome trace event generation library cmdliner 1.1.1 Declarative definition of command line interf cppo 1.6.9 Code preprocessor like cpp for OCaml csexp 1.5.1 Parsing and printing of S-expressions in Cano dot-merlin-reader 4.6 Reads config files for merlin dune 3.7.0 Fast, portable, and opinionated build system dune-build-info 3.7.0 Embed build information inside executable dune-configurator 3.7.0 Helper library for gathering system configura dune-rpc 3.6.2 Communicate with dune using rpc dyn 3.6.2 Dynamic type fiber 3.6.2 Structured concurrency library lambda-term 3.3.1 Terminal manipulation library for OCaml logs 0.7.0 Logging infrastructure for OCaml lwt 5.6.1 Promises and event-driven I/O lwt_react 1.2.0 Helpers for using React with Lwt merlin-lib 4.8-500 Merlin's libraries mew 0.1.0 Modal editing witch mew_vi 0.5.0 Modal editing witch, VI interpreter ocaml 5.0.0 The OCaml compiler (virtual package) ocaml-base-compiler 5.0.0 Official release 5.0.0 ocaml-config 3 OCaml Switch Configuration ocaml-lsp-server 1.15.1-5.0 LSP Server for OCaml ocaml-options-vanilla 1 Ensure that OCaml is compiled with no special ocamlbuild 0.14.2 OCamlbuild is a build system with builtin rul ocamlc-loc 3.6.2 Parse ocaml compiler output into structured f ocamlfind 1.9.6 A library manager for OCaml ocamlformat-rpc-lib 0.25.1 Auto-formatter for OCaml code (RPC mode) ocplib-endian 1.2 Optimised functions to read and write int16/3 octavius 1.2.2 Ocamldoc comment syntax parser omd 1.3.2 A Markdown frontend in pure OCaml ordering 3.6.2 Element ordering pp 1.1.2 Pretty-printing library ppx_yojson_conv_lib v0.15.0 Runtime lib for ppx_yojson_conv re 1.10.4 RE is a regular expression library for OCaml react 1.2.2 Declarative events and signals for OCaml result 1.5 Compatibility Result module seq base Compatibility package for OCaml's standard it spawn v0.15.1 Spawning sub-processes stdune 3.6.2 Dune's unstable standard library topkg 1.0.7 The transitory OCaml software packager trie 1.0.0 Strict impure trie tree uchar 0.0.2 Compatibility library for OCaml's Uchar modul user-setup 0.7 Helper for the configuration of editors for t utop 2.11.0 Universal toplevel for OCaml uucp 15.0.0 Unicode character properties for OCaml uuseg 15.0.0 Unicode text segmentation for OCaml uutf 1.0.3 Non-blocking streaming Unicode codec for OCam xdg 3.7.0 XDG Base Directory Specification yojson 2.0.2 Yojson is an optimized parsing and printing l zed 3.2.1 Abstract engine for text edition in OCaml ```

NeoVim OCaml LSP configuration (init.lua):

lspconfig.ocamllsp.setup {
  filetypes = { "ocaml", "ocaml.menhir", "ocaml.interface", "ocaml.ocamllex", "reason", "dune" },
  root_dir = lspconfig.util.root_pattern("*.opam", "esy.json", "package.json", ".git", "dune-project", "dune-workspace"),
  on_attach = on_attach,
  capabilities = capabilities
}

(At first I simply tried lspconfig.ocamllsp.setup {} but no success, same error message as above config. I also tried to add 'ml' to root_dir but still no luck. Instead of lspconfig.util.root_pattern, I also tried another form of root_dir configuration, basically a lua table if I remembered correctly, but still got the same error when started to edit a .ml buffer)

Note

ocamllsp confirmed working, because helix(editor) uses the exact same binary ($ which ocamllsp) and everything works fine on helix

➜  hello_world hx --health ocaml
Configured language server: ocamllsp
Binary for language server: /Users/aj/.opam/OCaml5BaseCompiler/bin/ocamllsp
Configured debug adapter: None
Highlight queries: ✓
Textobject queries: ✘
Indent queries: ✓

So I'm somewhat confused, both helix and neovim uses default configuration but neovim lsp is not working

rgrinberg commented 1 year ago

The error message from neovim is expected - it just means you don't have dune running in watch mode. ocamllsp should work as expected.

anzhi0708 commented 1 year ago

The error message from neovim is expected - it just means you don't have dune running in watch mode. ocamllsp should work as expected.

Thanks a lot for replying!

I'm a beginner in OCaml, and that's why I really need LSP related tools to help me learn OCaml.

Could you kindly show me how to set dune in watch mode, so that I can use ocamllsp within nvim? Appreciate it!

Khady commented 1 year ago

you can add --watch to basically any dune build command. But as mentioned earlier this is not necessary for ocamllsp to work. Having dune running in watch mode will add some features like reporting of errors happening outside of the file you are currently editing.

rsulli55 commented 1 year ago

@anzhi0708 I just ran into the same issue, the LSP wasn't working in a directory with a plain ocaml source file. I was able to resolve it by running git init on the directory, which forced the root directory to be populated and the LSP to actually attach. I think any file listed under root_dir here would suffice to have root directory be found and the L:SP should work after that.

josecastillolema commented 3 months ago

Thanks @rsulli55 the workaround works. However, is there a way to disable this root_dir requirement? The same folder without .git, .opam files, etc. works fine on VSCode, however it does not on NeoVim.

Workaround #2:

lspconfig.ocamllsp.setup {
  filetypes = { "ocaml", "ocaml.menhir", "ocaml.interface", "ocaml.ocamllex", "reason", "dune" },
  root_dir = lspconfig.util.root_pattern("*.opam", "esy.json", "package.json", ".git", "dune-project", "dune-workspace", "*.ml"),
  on_attach = on_attach,
  capabilities = capabilities
}