Minimal_init.lua does not work

I am trying to set up a basic lsp config, but this minimal_init.lua simply don’t work.

Symptons

type :LspInfo will show correctly that lsp server is attached, but the problem is, all the config does not work, and everything in on_attach callback does not get called, keymapping does not work also!

-- this template is borrowed from nvim-lspconfig
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
if on_windows then
	temp_dir = vim.loop.os_getenv("TEMP")
else
	temp_dir = "/tmp"
end

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

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")

local null_ls_config = function()
	local null_ls = require("null-ls")
	-- add only what you need to reproduce your issue
	null_ls.setup({
		sources = {
			null_ls.builtins.code_actions.gitsigns,
		},
		debug = true,
	})
end

local on_attach = function(client, bufnr)
	require("completion").on_attach()

	local function buf_set_keymap(...)
		vim.api.nvim_buf_set_keymap(bufnr, ...)
	end
	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 = { noremap = true, silent = true }
	buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
	buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
	buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
	buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
	buf_set_keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
	buf_set_keymap("n", "<space>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
	buf_set_keymap("n", "<space>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
	buf_set_keymap("n", "<space>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
	buf_set_keymap("n", "<space>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
	buf_set_keymap("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
	buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
	buf_set_keymap("n", "<space>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
	buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
	buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
	buf_set_keymap("n", "<space>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)

	-- Set some keybinds conditional on server capabilities
	if client.resolved_capabilities.document_formatting then
		buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
	elseif client.resolved_capabilities.document_range_formatting then
		buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
	end
end

local lspconfig_config = function()
	local lspconfig = require("lspconfig")
	lspconfig.gopls.setup({
		on_attach = on_attach,
	})
end

local function load_plugins()
	-- only add other plugins if they are necessary to reproduce the issue
	require("packer").startup({
		{
			"wbthomason/packer.nvim",
			{
				"jose-elias-alvarez/null-ls.nvim",
				requires = { "nvim-lua/plenary.nvim" },
				config = null_ls_config,
			},
			"neovim/nvim-lspconfig",
			{
				config = lspconfig_config,
			},
		},
		config = {
			package_root = package_root,
			compile_path = compile_path,
		},
	})
end

if vim.fn.isdirectory(install_path) == 0 then
	vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
	load_plugins()
	require("packer").sync()
else
	load_plugins()
	require("packer").sync()
end

I don’t think your packer.startup works the way you’re expecting. Try invoking “lspconfig_config()” explicitly instead of setting it as the package.config. Maybe call it right after you call load_plugins().

Applying some basic debug skills, It is easy to prove that config is called by just adding a print("111111111") in it, and the print just works, so that’s why it is so weird.

Also, even if I manually call it after load_plugins(), it still does work.

Alright, I just tested it locally.

Your null-ls client is connected, but not the actual gopls client, right?

Move the opening bracket in your setup.

	require("packer").startup({
		{
			"wbthomason/packer.nvim",
			{
				"jose-elias-alvarez/null-ls.nvim",
				requires = { "nvim-lua/plenary.nvim" },
				config = null_ls_config,
			},
			"neovim/nvim-lspconfig",
------------------->{
				config = lspconfig_config,
			},
		},
		config = {
			package_root = package_root,
			compile_path = compile_path,
		},
	})

should be

	require("packer").startup({
		{
			"wbthomason/packer.nvim",
			{
				"jose-elias-alvarez/null-ls.nvim",
				requires = { "nvim-lua/plenary.nvim" },
				config = null_ls_config,
			},
 ----------------->{
			        "neovim/nvim-lspconfig",
				config = lspconfig_config,
			},
		},
		config = {
			package_root = package_root,
			compile_path = compile_path,
		},
	})

I do get this result by modifying to the more commonly seen use syntax,

local function load_plugins()
	-- only add other plugins if they are necessary to reproduce the issue
	require("packer").startup({
		function(use)
			use({ "wbthomason/packer.nvim" })
			use({
				"jose-elias-alvarez/null-ls.nvim",
				requires = { "nvim-lua/plenary.nvim" },
				config = null_ls_config,
			})
			use({
				"neovim/nvim-lspconfig",
				config = lspconfig_config,
			})
		end,
		config = {
			package_root = package_root,
			compile_path = compile_path,
		},
	})
end

:LspInfo will show correctly the lsp servers are installed, but still, keymaps don’t work, it just like that on_attach function isn’t called.

1 Like