lsp-config starts the gopls server only if there is a go.mod file in the directory. However, it should also start without a go.mod file. How can I configure this?
Hello, language servers are started when the root directory pattern matches. You can read about it in :help lspconfig-root-detection
Thank you for yor support, but no, the server will not start if there is no go.mod file in the directory. Try it out. I just looked at the help (again), but I can’t figure it out. I definitely don’t understand what to put where so that gopls starts without go.mod in the same directory.
This is a common misconception. The LSP is primarily concerned with “projects”, and hence Neovim needs to know how to find the project that a file is part of to tell the language server how to find the other files for that project. This is of course language-dependent; for Go, it’s the presence of a go.mod
file (although there’s usually a fallback to the top-level directory of the current git repository). This is documented in lspconfig
's server configuration.
The specification doesn’t address what should happen with files that are not part of a project, and different language servers handle that case differently – including crashing when there is no project! Hence Neovim does not try to start a server unless it is sure that will go well.
However, some servers have been tested to work reasonably well, and these have single_file_support = true
set in the default config. This is not the case for gopls
, so either this doesn’t work, or it hasn’t been tested yet. (Remember that all configs are user-contributed!)
So what you can do is to pass the single_file_support=true
to the setup
for gopls
and test whether that works as expected; if it does, you can open a PR to lspconfig to add it to the default configuration.
Thank you! A very good explanation of the problem. Also, the tip with gopls works perfectly:
require('lspconfig').gopls.setup{
single_file_support = true
}