Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2673 Breaks gopls #2733

Closed
EJammy opened this issue Jul 25, 2023 · 24 comments
Closed

#2673 Breaks gopls #2733

EJammy opened this issue Jul 25, 2023 · 24 comments
Labels
bug Something isn't working

Comments

@EJammy
Copy link
Contributor

EJammy commented Jul 25, 2023

Description

mod_cache does not seem to be set correctly with the performance improvement using async_run_command. Introduced in 9a2cc56 (#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

  1. go mod init example/hello
  2. nvim hello.go

Actual behavior

[lspconfig] unhandled error: ...-lspconfig/lua/lspconfig/server_configurations/gopls.lua:16: attempt to get length of upval
ue 'mod_cache' (a nil value)

Expected behavior

No response

Minimal config

local on_windows = vim.loop.os_uname().version:match 'Windows'

local function join_paths(...)
  local path_sep = on_windows and '\\' or '/'
  local result = table.concat({ ... }, path_sep)
  return result
end

vim.cmd [[set runtimepath=$VIMRUNTIME]]

local temp_dir = vim.loop.os_getenv 'TEMP' or '/tmp'

vim.cmd('set packpath=' .. join_paths(temp_dir, 'nvim', 'site'))

local package_root = join_paths(temp_dir, 'nvim', 'site', 'pack')
local lspconfig_path = join_paths(package_root, 'test', 'start', 'nvim-lspconfig')

if vim.fn.isdirectory(lspconfig_path) ~= 1 then
  vim.fn.system { 'git', 'clone', 'https://github.com/neovim/nvim-lspconfig', lspconfig_path }
end

vim.lsp.set_log_level 'trace'
require('vim.lsp.log').set_format_func(vim.inspect)
local nvim_lsp = require 'lspconfig'
local on_attach = function(_, bufnr)
  local function buf_set_option(...)
    vim.api.nvim_buf_set_option(bufnr, ...)
  end

  buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  local opts = { buffer = bufnr, noremap = true, silent = true }
  vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
  vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
  vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
  vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
  vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
  vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
  vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
  vim.keymap.set('n', '<space>wl', function()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, opts)
  vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
  vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
  vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
  vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
  vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
  vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
  vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
end

-- Add the server that troubles you here
local name = 'gopls'
-- local cmd = { 'pyright-langserver', '--stdio' } -- needed for elixirls, lua_ls, omnisharp
if not name then
  print 'You have not defined a server name, please edit minimal_init.lua'
end
if not nvim_lsp[name].document_config.default_config.cmd and not cmd then
  print [[You have not defined a server default cmd for a server
    that requires it please edit minimal_init.lua]]
end

nvim_lsp[name].setup {
  cmd = cmd,
  on_attach = on_attach,
}

print [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]]

LSP log

[START][2023-07-26 09:12:41] LSP logging initiated
[INFO][2023-07-26 09:12:41] .../lua/vim/lsp.lua:1808	"exit_handler"	{}
@EJammy EJammy added the bug Something isn't working label Jul 25, 2023
@EJammy EJammy changed the title 9a2cc569c88662fa41d414bdb65b13ea72349f86 (#2673) Breaks golsp Breaks golsp Jul 25, 2023
@EJammy EJammy changed the title Breaks golsp #2673 Breaks golsp Jul 25, 2023
@glepnir
Copy link
Member

glepnir commented Jul 25, 2023

output of go env GOMODCACHE

@EJammy
Copy link
Contributor Author

EJammy commented Jul 26, 2023

# go env GOMODCACHE
C:\Users\connor.chang\go\pkg\mod

@glepnir
Copy link
Member

glepnir commented Jul 26, 2023

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

@EJammy
Copy link
Contributor Author

EJammy commented Jul 26, 2023

In here:

local result = util.async_run_command 'go env GOMODCACHE'
if result and result[1] then
mod_cache = vim.trim(result[1])
end

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

@glepnir
Copy link
Member

glepnir commented Jul 26, 2023

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 ?

@EJammy
Copy link
Contributor Author

EJammy commented Jul 26, 2023

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.

@glepnir
Copy link
Member

glepnir commented Jul 26, 2023

fallback to sync will cause an performance issue . could you try the cmd like i comment on above ?

@EJammy
Copy link
Contributor Author

EJammy commented Jul 26, 2023

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.

@giladsher
Copy link

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

@CalebJohnHunt
Copy link

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.

@x000zh
Copy link

x000zh commented Aug 7, 2023

go package from snap has the same issue

@juparave
Copy link

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.

@bartektelec
Copy link

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.

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 \\

@nitschmann
Copy link

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...

@EJammy
Copy link
Contributor Author

EJammy commented Sep 24, 2023

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,
}

@svitiashchuk
Copy link

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")]],
      },
    },
  }

@EJammy EJammy changed the title #2673 Breaks golsp #2673 Breaks gopls Sep 24, 2023
@BalanceBalls
Copy link

svitiashchuk Thanks a lot! That worked indeed. Had this issue on windows 11 , even though on macOS there was no such issue

@MikeDepinto
Copy link

~/.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 !

@BaktashGorgani
Copy link

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

samerickson added a commit to samerickson/nvim that referenced this issue Jan 7, 2024
The issue was due to GOMODCACHE and is present in the gopls lsp:
    neovim/nvim-lspconfig#2733
@tashish-soni
Copy link

@EJammy Thanks alot for providing the override snippet. Even though i dont understand all of it, it solved the issue.

@aaronik
Copy link

aaronik commented Jan 31, 2024

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.

@hauserx
Copy link

hauserx commented May 14, 2024

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

notthelewis added a commit to notthelewis/nvim-lua that referenced this issue Jun 23, 2024
Go language server failing due to not finding go modules dir.
Add `root_dir` property to the setup function in Mason.

References: neovim/nvim-lspconfig#2733 (comment)
@justinmk
Copy link
Member

So... is this still an issue? #2733 was a "partial" fix.

@DownerCase
Copy link

So... is this still an issue? #2733 was a "partial" fix.

Yeah, I just hit this, an updated nvim-lspconfig let gopls run but it wasn't fully working.
I had neovim and go installed via snaps, running go env GOMODCACHE was working outside neovim but not from inside (i.e: :! go env ...)

I took hauserx's hint from above and reinstalled go without snap and everything worked perfectly, even on the older nvim-lspconfig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests