basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

file renaming doesn't work on relative imports

Open AlexElizard opened this issue 9 months ago • 13 comments

Description

Hello.

  1. When renaming a file, imports are not resolved :LspInfo
vim.lsp: require("vim.lsp.health").check()

- LSP log level : WARN
- Log path: /home/alexelizard/.local/state/lazyvim/lsp.log
- Log size: 9 KB

vim.lsp: Active Clients ~
- basedpyright (id: 1)
    Root directory: ~/Projects/lazyvim-basedpyright-example
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/basedpyright-langserver --stdio
    Settings: {
      basedpyright = {
        analysis = {
          autoSearchPaths = true,
          diagnosticMode = "openFilesOnly",
          useLibraryCodeForTypes = true
        }
      }
    }
    Attached buffers: 39
- ruff (id: 2)
    Root directory: ~/Projects/lazyvim-basedpyright-example
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/ruff server
    Settings: {}
    Attached buffers: 39

vim.lsp: File Watcher ~
- file watching "(workspace/didChangeWatchedFiles)" disabled on all clients

vim.lsp: Position Encodings ~
- No buffers contain mixed position encodings

I was able to solve this problem by changing the setting diagnosticMode = "workspace"

# ~/.config/lazyvim/lua/plugins/lsp.lua

return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      servers = {
        basedpyright = {
          settings = {
            basedpyright = {
              analysis = {
                diagnosticMode = "workspace",
              },
            },
          },
        },
      },
    },
  },
}
  1. When renaming a file again, imports are not resolved. As far as I understand, the LSP server does not see the file because File Watcher is not working.

:LspInfo

vim.lsp: require("vim.lsp.health").check()

- LSP log level : WARN
- Log path: /home/alexelizard/.local/state/lazyvim/lsp.log
- Log size: 9 KB

vim.lsp: Active Clients ~
- basedpyright (id: 1)
    Root directory: ~/Projects/lazyvim-basedpyright-example
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/basedpyright-langserver --stdio
    Settings: {
      basedpyright = {
        analysis = {
          autoSearchPaths = true,
          diagnosticMode = "workspace",
          useLibraryCodeForTypes = true
        }
      }
    }
    Attached buffers: 9
- ruff (id: 2)
    Root directory: ~/Projects/lazyvim-basedpyright-example
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/ruff server
    Settings: {}
    Attached buffers: 9

vim.lsp: File Watcher ~
- file watching "(workspace/didChangeWatchedFiles)" disabled on all clients

vim.lsp: Position Encodings ~
- No buffers contain mixed position encodings

I was able to solve this in one of two ways:

  • Run :LspRestart
  • Add dynamicRegistration = true
# ~/.config/lazyvim/lua/plugins/lsp.lua

return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      capabilities = {
        workspace = {
          didChangeWatchedFiles = {
            dynamicRegistration = true,
          },
        },
      },
      servers = {
        basedpyright = {
          settings = {
            basedpyright = {
              analysis = {
                diagnosticMode = "workspace",
              },
            },
          },
        },
      },
    },
  },
}
  1. With this configuration, each file rename results in a rereading (scanning) of the entire repository.

:LspInfo

vim.lsp: require("vim.lsp.health").check()

- LSP log level : WARN
- Log path: /home/alexelizard/.local/state/lazyvim/lsp.log
- Log size: 9 KB

vim.lsp: Active Clients ~
- ruff (id: 1)
    Root directory: ~/Projects/crm-qc
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/ruff server
    Settings: {}
    Attached buffers: 9
- basedpyright (id: 2)
    Root directory: ~/Projects/crm-qc
    Command: /home/alexelizard/.local/share/lazyvim/mason/bin/basedpyright-langserver --stdio
    Settings: {
      basedpyright = {
        analysis = {
          autoSearchPaths = true,
          diagnosticMode = "workspace",
          useLibraryCodeForTypes = true
        }
      }
    }
    Attached buffers: 9

vim.lsp: File Watcher ~
- File watch backend: libuv-watchdirs
- WARNING libuv-watchdirs has known performance issues. Consider installing fswatch.

vim.lsp: Position Encodings ~
- No buffers contain mixed position encodings

Question: What should the configuration be?

Environment

Installation Lazyvim

$ NVIM_APPNAME=lazyvim nvim

Example project

lazyvim-basedpyright-example.zip

  1. Try to rename bar.py to baz.py. Import must be resolved in foo.py. For 1 and 2 problem.
  2. Try to rename qux.py to quz.py. Import must be resolved in foo.py. It is not resolve for all configuration

AlexElizard avatar Apr 03 '25 08:04 AlexElizard

looks like you accidentally submitted this issue before you finished writing?

DetachHead avatar Apr 03 '25 09:04 DetachHead

Yes, that's right. I need some time to provide a minimal project to reproduce the problem.

AlexElizard avatar Apr 03 '25 09:04 AlexElizard

yeah this will happen when capabilities.workspace.didChangeWatchedFiles.dynamicRegistration is disabled, see https://github.com/microsoft/pyright/issues/4635#issuecomment-1430177826

i tried to work around this a few months ago by using a system file watcher instead in #1011, but it ended up causing crashes for neovim users due to too many system file watchers being registered so i had to revert it in #1053

so i don't think there's anything we can do about it unfortunately. setting dynamicRegistration = true like you mentioned is probably the best solution. is there any downside to doing that? if not, i think we should just update the neovim setup instructions in the docs to recommend this setting (i wasn't aware it was disabled by default)

DetachHead avatar Apr 03 '25 10:04 DetachHead

Setting dynamicRegistration = true on a working project re-reads 400 files, which results in a 10-30 second freeze.

In pylance this is not observed. I think pylance uses a project scan once and caches the result. And then when the event is renamed, it updates the cache.

AlexElizard avatar Apr 03 '25 10:04 AlexElizard

I added a example project to the initial post. While compiling it, another error popped up. I described it at the end of the initial post.

AlexElizard avatar Apr 03 '25 10:04 AlexElizard

In pylance this is not observed. I think pylance uses a project scan once and caches the result. And then when the event is renamed, it updates the cache.

yeah maybe. we have an issue to implement pylance's cacheing, see #427

vim.lsp: File Watcher ~
- File watch backend: libuv-watchdirs
- WARNING libuv-watchdirs has known performance issues. Consider installing fswatch.

i'm not very familiar with neovim or any of these tools, but this warning here does look a bit sus. if you do as it suggests and switch to fswatch do you notice any performance improvement?

DetachHead avatar Apr 03 '25 11:04 DetachHead

I am also a neovim newbie, but I will try to figure it out. I will write to this issue about the results of my research

AlexElizard avatar Apr 03 '25 12:04 AlexElizard

Current result:

  1. For neovim 0.10 fswatch improves file loading speed by about two times. However, there are still delays. For all this to work, fswatch needs to be installed. For Fedora Linux sudo dnf install fswatch. Also, you need to increase the maximum number of events in the queue for inotify sudo sysctl fs.inotify.max_queued_events=100000 Sources:
  1. For neovim 0.11 fswatch was replace to inotify-tools In fedora now neovim version 0.10.4 I will continue the research when 0.11 version appears in fedora repository

AlexElizard avatar Apr 03 '25 13:04 AlexElizard

I checked import resolves in the example project on VS Code. The behavior is identical to the behavior in lazyvim. Pylance was removed before the basedpyright extension was installed.

Example project lazyvim-basedpyright-example.zip

Try to rename bar.py to baz.py. Import must be resolved in foo.py. For 1 and 2 problem. Try to rename qux.py to quz.py. Import must be resolved in foo.py. It is not resolve for all configuration. In pylance there is no such problem.

Do I need to create a separate issue for this problem?

AlexElizard avatar Apr 06 '25 06:04 AlexElizard

i can't seem to reproduce the issue with that example project :(

when you rename the file, do you see this confirmation box?

Image

if not, maybe basedpyright isn't running? can you check if diagnostics or any other language server features are working?

DetachHead avatar Apr 07 '25 10:04 DetachHead

LSP is working - diagnostics, documentation, go to definition, etc Image

Renaming files demonstration https://github.com/user-attachments/assets/697fbbd0-2398-437a-b9dc-89f75d29a4fe

VS Code version 1.99 Basedpyright version 1.28.4

AlexElizard avatar Apr 07 '25 22:04 AlexElizard

I'm experiencing exactly the same problem. VS Code version 1.99.2 Basedpyright version: 1.28.5 I have the same issue also in Neovim. After changing the quz.py to qux.py

Image

Renaming bar.py works normally

neogib avatar Apr 11 '25 11:04 neogib

ok i reproduced it, thanks for the clear repro steps, not sure why i couldn't get it to happen before.

it seems that the rename functionality is broken on relative imports

DetachHead avatar Apr 18 '25 04:04 DetachHead

I'm facing the same issue

kiyoon avatar Jun 26 '25 08:06 kiyoon