file renaming doesn't work on relative imports
Description
Hello.
- 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",
},
},
},
},
},
},
},
}
- 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",
},
},
},
},
},
},
},
}
- 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
- Extract lazyvim.zip to
~/.config/ - Run lazyvim
$ NVIM_APPNAME=lazyvim nvim
Example project
lazyvim-basedpyright-example.zip
- Try to rename
bar.pytobaz.py. Import must be resolved infoo.py. For 1 and 2 problem. - Try to rename
qux.pytoquz.py. Import must be resolved infoo.py. It is not resolve for all configuration
looks like you accidentally submitted this issue before you finished writing?
Yes, that's right. I need some time to provide a minimal project to reproduce the problem.
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)
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.
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.
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?
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
Current result:
- 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 inotifysudo sysctl fs.inotify.max_queued_events=100000Sources:
- 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
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?
i can't seem to reproduce the issue with that example project :(
when you rename the file, do you see this confirmation box?
if not, maybe basedpyright isn't running? can you check if diagnostics or any other language server features are working?
LSP is working - diagnostics, documentation, go to definition, etc
Renaming files demonstration https://github.com/user-attachments/assets/697fbbd0-2398-437a-b9dc-89f75d29a4fe
VS Code version 1.99 Basedpyright version 1.28.4
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
Renaming bar.py works normally
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
I'm facing the same issue