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

LSP client doesn't delete old buffers after renaming a directory #22617

Closed
ivaaaan opened this issue Mar 10, 2023 · 3 comments
Closed

LSP client doesn't delete old buffers after renaming a directory #22617

ivaaaan opened this issue Mar 10, 2023 · 3 comments
Labels
bug issues reporting wrong behavior lsp

Comments

@ivaaaan
Copy link
Contributor

ivaaaan commented Mar 10, 2023

Problem

When I'm renaming a directory, LSP client doesn't delete opened buffers from that directory after renaming is done, which causes an error when trying to save renaming result with wa.

I think the problem is in this function https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/util.lua#L776, which is working correctly if you try to rename a single file, but doesn't delete old buffers with old_fname is a dir.

Steps to reproduce using "nvim -u minimal_init.lua"

--- CHANGE THESE
local pattern = 'go'
local cmd = {'gopls'}
-- Add files/folders here that indicate the root of a project
local root_markers = {'pack', 'pack/file.go', 'main.go'}
-- Change to table with settings if required
local settings = vim.empty_dict()

vim.api.nvim_create_autocmd('FileType', {
  pattern = pattern,
  callback = function(args)
    local match = vim.fs.find(root_markers, { path = args.file, upward = true })[1]
    local root_dir = match and vim.fn.fnamemodify(match, ':p:h') or nil
    vim.lsp.start({
      name = 'bugged-ls',
      cmd = cmd,
      root_dir = root_dir,
      settings = settings
    })
  end
})
cd /tmp && mkdir -p lsp/pack && cd lsp
go mod init nvim.lua
echo "package pack" > pack/pack.go

Try call rename action by placing cursor on pack and then :wa.

Expected behavior

All files open buffers within the renamed directory are deleted.

Neovim version (nvim -v)

NVIM v0.9.0-dev-1189+gd3c8d104b-dirty

Language server name/version

gopls

Operating system/version

macos

Log file

No response

@ivaaaan ivaaaan added bug issues reporting wrong behavior lsp labels Mar 10, 2023
@ivaaaan ivaaaan changed the title LSP client doesn't close old buffers after renaming a directory LSP client doesn't delete old buffers after renaming a directory Mar 10, 2023
@ivaaaan
Copy link
Contributor Author

ivaaaan commented Mar 11, 2023

#22618 my ugly fix which tries to fix it. if you think it is a viable solution I can try to clean it up and write some tests

@justinmk
Copy link
Member

Related: #22075

Try call rename action by placing cursor on pack and then :wa.

what exactly does that mean? rename() is called by apply_workspace_edit which happens in several places. To judge the use-case it is important to mention the actions taken.

Though in general the idea makes sense. If apply_workspace_edit renamed a bunch of files then the dangerous part already happened, so deleting some buffers seems reasonable.

@ivaaaan
Copy link
Contributor Author

ivaaaan commented Mar 11, 2023

what exactly does that mean? rename() is called by apply_workspace_edit which happens in several places. To judge the use-case it is important to mention the actions taken.

I meant renaming package name in Go. When you rename it, the directory is renamed as well, which is causing problems with old buffers.

yesean pushed a commit to yesean/neovim that referenced this issue Mar 25, 2023
Problem:

When LSP client renames a directory, opened buffers in the edfitor are not
renamed or closed. Then `:wall` shows errors.

https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/util.lua#L776
works correctly if you try to rename a single file, but doesn't delete old
buffers with `old_fname` is a dir.

Solution:
Update the logic in runtime/lua/vim/lsp/util.lua:rename()

Fixes neovim#22617
folke pushed a commit to folke/neovim that referenced this issue May 22, 2023
Problem:

When LSP client renames a directory, opened buffers in the edfitor are not
renamed or closed. Then `:wall` shows errors.

https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/util.lua#L776
works correctly if you try to rename a single file, but doesn't delete old
buffers with `old_fname` is a dir.

Solution:
Update the logic in runtime/lua/vim/lsp/util.lua:rename()

Fixes neovim#22617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior lsp
Projects
None yet
Development

No branches or pull requests

2 participants