nvim-lspconfig icon indicating copy to clipboard operation
nvim-lspconfig copied to clipboard

autostart=false: server does not automatically attach when editing new, nonexistent file

Open Diomendius opened this issue 2 years ago • 1 comments

Description

When autostart = false and any language server has been manually started, editing an existing file of a compatible file type automatically attaches the server to the new buffer, but editing a nonexistent file (with the appropriate extension such that filetype is automatically set to a compatible filetype) does not attach the server.

When autostart = true, language servers automatically attach to buffers regardless of whether the buffer is backed by an existing file.

Neovim version

NVIM v0.9.1 Build type: Release LuaJIT 2.1.0-beta3

Nvim-lspconfig version

deade69789089c3da15237697156334fb3e943f0

Operating system and version

Arch Linux, up to date as of 2023-07-09

Affected language servers

all

Steps to reproduce

This assumes Pyright is installed, though these steps and the provided config should be simple to adapt to any language server, as this issue is specific to nvim-lspconfig, not any language server.

  1. touch foo.py
  2. nvim -nu minimal_init.lua foo.py
  3. :LspStart
  4. :LspInfo, observe that pyright is attached to the current buffer and the workspace root is the current working directory.
  5. :e bar.py
  6. :LspInfo, observe that no language server is attached.
  7. :w
  8. :e
  9. :LspInfo, observe that pyright is attached to the current buffer.

Actual behavior

At step 6, the language server has not attached to the newly opened buffer.

Expected behavior

Already-started language servers should automatically attach to all compatible files within their workspace, not just those that already exist.

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'

nvim_lsp.pyright.setup {
  cmd = { 'pyright-langserver', '--stdio' },
  on_attach = on_attach,
  autostart = false,
  root_dir = function() return vim.fn.getcwd() end,
}

LSP log

https://gist.github.com/Diomendius/e359bacac0b4db45f0cf0a1b1cdc26aa

Diomendius avatar Jul 12 '23 09:07 Diomendius

As #3347 reverted the fix for this, this should be reopened until a better fix is implemented.

Diomendius avatar Oct 04 '24 10:10 Diomendius

No longer relevant since we migrated to vim.lsp.config

justinmk avatar Apr 23 '25 13:04 justinmk