Open EJammy opened 12 months ago
output of go env GOMODCACHE
# go env GOMODCACHE
C:\Users\connor.chang\go\pkg\mod
problem is why there does not get this value ..although we can add a nil check but it should be have a value. looks like only happen in windows
It looks like result
is nil. I'm not sure how async_run_command
works, but somehow it's failing. I think we should change the code to fall back to using vim.fn.system
for now.
local result = util.async_run_command 'go env GOMODCACHE'
if result and result[1] then
mod_cache = vim.trim(result[1])
else
mod_cache = vim.fn.system 'go env GOMODCACHE'
end
I don't think it make sense to have a code path where mod_cache is nil anyway
vim.fn.system
is sync. and problem not nil
check. it's only in windows async get command output failed. in *nix it works fine. I am not familar on windows. maybe there need cmd like this cmd.exe /c go env GOMODCACHE
?
Sorry, I'm not sure what you are trying to say. vim.fn.system
works fine. That's why I proposed the code above, to fall back to run in sync when async fails. I can create a pr, but this wouldn't fix the issue with async_run_command
.
fallback to sync will cause an performance issue . could you try the cmd like i comment on above ?
Did not work :(. I did some more testing and this only happens when launching from msys2 environment, so some environment variable might be interfering. Does not happen when launching from cmd or using neovim qt.
If it helps at all, I'm having the same problem with WSL2 using Ubuntu 22.04.2 LTS.
When I run go env GOMODCACHE
I get the following output:
go env GOMODCACHE
/home/gilad/go/pkg/mod
I was using Window Terminal with Git Bash, and I was getting the same error. If I keep using Windows Terminal but use PowerShell instead of Bash, the error goes away.
go package from snap has the same issue
I'm having the same problem since updating to nvim v0.9.x, I temp fixed by setting the value of mod_cache
at the top of gopls.lua
file with the value thrown by go env GOMODCACHE
local util = require 'lspconfig.util'
local mod_cache = '/home/myuser/go/pkg/mod'
In another station, which runs nvim v0.8.1, works fine, with the latest lspconfig update. I dont want to update nvim
on that one yet.
I'm having the same problem since updating to nvim v0.9.x, I temp fixed by setting the value of
mod_cache
at the top ofgopls.lua
file with the value thrown bygo env GOMODCACHE
local util = require 'lspconfig.util' local mod_cache = '/home/myuser/go/pkg/mod'
In another station, which runs nvim v0.8.1, works fine, with the latest lspconfig update. I dont want to update
nvim
on that one yet.
Had same issue with nvim 0.9.1
with lazy-nvim, replaced my nvim-data/lazy/nvim-lspconfig/lua/lspconfig/server_configurations/gopls.lua
and set the mod_cache default from nil to whatever prints out in your terminal when you run go env GOMODCACHE
, for windows remember you will need to escape the backslashes, so replace \
with \\
Hey! Is there some outcome here? This is still open and even with the most recent changes and neovim
version 0.9.2
on MacOS it does cause issues...
For anyone having this issue, you can overwrite the root_dir in your config:
local util = require'lspconfig.util'
require'lspconfig'.gopls.setup {
-- ...some other setups
root_dir = function(fname)
-- see: https://github.com/neovim/nvim-lspconfig/issues/804
local mod_cache = vim.trim(vim.fn.system 'go env GOMODCACHE')
if fname:sub(1, #mod_cache) == mod_cache then
local clients = vim.lsp.get_active_clients { name = 'gopls' }
if #clients > 0 then
return clients[#clients].config.root_dir
end
end
return util.root_pattern 'go.work'(fname) or util.root_pattern('go.mod', '.git')(fname)
end,
}
or if you're still having issues:
require'lspconfig'.gopls.setup {
-- ...some other setups
root_dir = function(fname)
return util.root_pattern 'go.work'(fname) or util.root_pattern('go.mod', '.git')(fname)
end,
}
For those who are experiencing the same issue, you can fix the error by manually providing the config with the proper value set for the variable mod_cache
. To do this, first obtain the value by running::
go env GOMODCACHE
# for me result is:
# /root/go/pkg/mod
Now, provide the configuration for gopls
with the variable value set to the result of calling the previous command (you can do this in your init.lua, for example).
-- loading module to provide config for a server following steps from guide here
-- https://github.com/neovim/nvim-lspconfig/blob/ede4114e1fd41acb121c70a27e1b026ac68c42d6/doc/lspconfig.txt#L326
local configs = require 'lspconfig.configs'
-- copy paste from
-- https://github.com/neovim/nvim-lspconfig/blob/ede4114e1fd41acb121c70a27e1b026ac68c42d6/lua/lspconfig/server_configurations/gopls.lua
local util = require 'lspconfig.util'
local async = require 'lspconfig.async'
-- -> the following line fixes it - mod_cache initially set to value that you've got from `go env GOMODCACHE` command
local mod_cache = '/root/go/pkg/mod'
-- setting the config for gopls, the contents below is also copy-paste from
-- https://github.com/neovim/nvim-lspconfig/blob/ede4114e1fd41acb121c70a27e1b026ac68c42d6/lua/lspconfig/server_configurations/gopls.lua
configs.gopls = {
default_config = {
cmd = { 'gopls' },
filetypes = { 'go', 'gomod', 'gowork', 'gotmpl' },
root_dir = function(fname)
-- see: https://github.com/neovim/nvim-lspconfig/issues/804
if not mod_cache then
local result = async.run_command 'go env GOMODCACHE'
if result and result[1] then
mod_cache = vim.trim(result[1])
end
end
if fname:sub(1, #mod_cache) == mod_cache then
local clients = vim.lsp.get_active_clients { name = 'gopls' }
if #clients > 0 then
return clients[#clients].config.root_dir
end
end
return util.root_pattern 'go.work'(fname) or util.root_pattern('go.mod', '.git')(fname)
end,
single_file_support = true,
},
docs = {
description = [[
https://github.com/golang/tools/tree/master/gopls
Google's lsp server for golang.
]],
default_config = {
root_dir = [[root_pattern("go.work", "go.mod", ".git")]],
},
},
}
svitiashchuk Thanks a lot! That worked indeed. Had this issue on windows 11 , even though on macOS there was no such issue
~/.local/share/nvim/site/pack/packer/start/nvim-lspconfig/lua/lspconfig/server_configurations/gopls.lua
just wanted to add this here, (linux). I had trouble finding the file location. (I use packer as my package manager.
like others have the same setup on another machine. no issues. but when I booted this machine up it gave me this issue. thanks @svitiashchuk !
I'm having this issue too and have tried all the solutions here to no avail.
This is only happening on nvim on windows.
on my nvim on fedora and ubuntu i don't have this issue
@EJammy Thanks alot for providing the override snippet. Even though i dont understand all of it, it solved the issue.
For the record, this is happening to me on an Ubuntu machine (uname -a
: Linux -- 6.5.0-15-generic #15~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Jan 12 18:54:30 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
) as well, so it's not just on windows.
Happened for me also on Ubuntu 22.04
Noticed that go commands were not printing anything when redirected to file, this file was empty:
go env GOMODCACHE > file
The issue was gone after removing go/gopls installed through ubuntu (sudo snap remove go
), and installing manually from https://go.dev/doc/install .
Possibly caused by https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1849753 - the symptoms are the same as in duplicate of this bug -- https://bugs.launchpad.net/snapd/+bug/1921110
Description
mod_cache
does not seem to be set correctly with the performance improvement usingasync_run_command
. Introduced in 9a2cc569c88662fa41d414bdb65b13ea72349f86 (#2673). Reverting the changes fix this.Neovim version
NVIM v0.8.3 Build type: RelWithDebInfo LuaJIT 2.1.0-beta3 Compiled by runneradmin@fv-az171-224
Features: -acl +iconv +tui See ":help feature-compile"
system vimrc file: "$VIM\sysinit.vim" fall-back for $VIM: "C:/Program Files (x86)/nvim/share/nvim"
Run :checkhealth for more info
Nvim-lspconfig version
No response
Operating system and version
Windows 10
Affected language servers
gopls
Steps to reproduce
go mod init example/hello
nvim hello.go
Actual behavior
Expected behavior
No response
Minimal config
LSP log