go-client icon indicating copy to clipboard operation
go-client copied to clipboard

ExecLua function will cause neovim hangup

Open glepnir opened this issue 3 years ago • 6 comments

@zchee I can confirm my lua code can works well.

func (c *Command) LspInstall(p *plugin.Plugin) ([]string, error) {
	userConfig := make([]string, 0)
	getUserConfig := "require('lspmeta').get_user_config_server()"
	if err := c.nvim.ExecLua(getUserConfig, userConfig); err != nil {
		return nil, err
	}
	return userConfig, nil
}

lua code

function lspmeta.get_user_config_server()
  local lspconfig = npcall(require,'lspconfig')
  if not lspconfig then
    -- check the packer exist
    if next(packer_plugins) ~= nil then
      vim.cmd [[packadd nvim-lspconfig]]
      lspconfig = require('lspconfig')
    else
      return
    end
  end

  local configs = require('lspconfig/configs')
  return vim.tbl_keys(configs)
end

glepnir avatar Mar 05 '21 08:03 glepnir

@glepnir Thanks! will check it.

zchee avatar Mar 05 '21 08:03 zchee

take code form test file. also hangup. @zchee cc

Untitled

glepnir avatar Mar 10 '21 12:03 glepnir

any update for this issue?

glepnir avatar Mar 12 '21 08:03 glepnir

@glepnir I'd dig this issue but not yet solved...

zchee avatar Mar 14 '21 12:03 zchee

@glepnir Could you post Go side minimal code?

It's doesn't work at least my local.

func (c *Command) LspInstall(p *plugin.Plugin) ([]string, error) {
	userConfig := make([]string, 0)
	getUserConfig := "require('lspmeta').get_user_config_server()"
	if err := c.nvim.ExecLua(getUserConfig, userConfig); err != nil {
		return nil, err
	}
	return userConfig, nil
}

zchee avatar Mar 16 '21 14:03 zchee

hmm it can't work for you because you does not have lua function right? check my second image. it use the go test code from go-client

glepnir avatar Mar 16 '21 14:03 glepnir

Hi @glepnir @zchee I was facing a similar issue and found out that the issue was due to the --embed flag not being set. Because --embed flag was not set, the RPC call to nvim_exec_lua was hanging.

A small, working lua example is:

func hello(args []string) (int, error) {
	nv, err := nvim.NewChildProcess(
                 ChildProcessArgs("--embed") // we need to start `nvim` with this flag. In the unit tests this was set, but not in the example above
        )
	if err != nil {
		return 0, err
	}

	var result int
	program := "local a, b = ... return a + b"

	if err := nv.ExecLua(program, &result, 1, 2); err != nil {
		return result, err
	}

	return result, nil
}

func main() {
	plugin.Main(func(p *plugin.Plugin) error {
		p.HandleFunction(&plugin.FunctionOptions{Name: "Hello"}, hello)
		return nil
	})
}

There was another issue #155 where setting the --embed flag also helped with the panic issues. Given it is easy to overlook setting the --embed flag, one way to fix this is to set --embed flag by default. I've done so in this MR #161 , and allowed users to overwrite this behavior with another option.

Please help review and let me know what you think about this solution. Thanks!

https://github.com/neovim/go-client/assets/1814687/4b3df875-66cf-4fa4-af3e-f1e678755a1a

9Y5 avatar Jul 14 '23 20:07 9Y5