NeoVim vs. "Blocking waiting for file lock on build directory"

I’m using rust-analyzer in NeoVim via nvim-lspconfig. I’m having frequent problems when running cargo build in a different window, where it's blocking waiting for a directory lock, and this often takes quite long (even minutes - the project we're working on is quite big):

    Blocking waiting for file lock on build directory

AFAIU, this happens due to rust-analyzer’s cargo check subprocess taking the lock.

I found a suggestion somewhere, that this could be worked around by passing something like CARGO_TARGET_DIR=/tmp to cargo check. However, I just don’t seem to be able to understand how to setup this in nvm-lspconfig :cry: I tried a few ways but was unsuccessful — e.g. something like below in my vimrc doesn't seem to result in any visible files in /tmp/rust-analyzer, and the Blocking (...) problem/message still occurs:

local servers = { 'rnix', 'rust_analyzer' }
for _, lsp in ipairs(servers) do
  nvim_lsp[lsp].setup {
    on_attach = on_attach,
    flags = {
      debounce_text_changes = 150,
    },
    settings = {
      ['rust-analyzer'] = {
        server = {
          extraEnv = {
            CARGO_TARGET_DIR = '/tmp/rust-analyzer'
          },
        },
      },
    },
  }
end

Could someone help me find a way to avoid this lock contention? I'd be very grateful! :heart: The current situation this puts me in is worryingly distracting for me :sweat:

Huh, so the immediate problem here is that neovim documentation is just wrong. rust-analyzer.server.extraEnv isn't a setting that exists in neovim. I've opened rust-analyzer documentation lists wrong config options · Issue #1735 · neovim/nvim-lspconfig · GitHub for that.

I think there might be some other way to specify the environment for language server which is run by neovim, but I don't know what it is.

As a workaround, you can try rust-analyzer.checkOnSave.enable: false to disable cargo check altogether (unlike extraEnv, which is a setting of the VS Code extension, checkOnSave is the setting of the rust-analyzer binary iteself and should work).

2 Likes

Uhhh, got it, thanks for the debugging effort! That said, rust-analyzer is something notably useful and that I'd really like to use (esp. jumping around & type info are indispensable to me), so disabling it on save sounds less than ideal... if I want to be able to pass a custom env variable to it, should I then open an issue in nvim-lspconfig to add such a feature? (Or, per the "XY problem", open an issue about being able to run rust-analyzer in a way not blocking cargo in the workdir, with a possible solution being passing some CARGO_TARGET_DIR?)

The checkOnSave settings disbles only the bit about running cargo check on save. All the actual IDE features like goto definition, completion, hints, etc, will continue to work.

Oh wow, ok, didn't know - thanks!!! :heart: Will try right now.

BTW you could pass cmd_env = { ... } (as a top level key in the setup function) instead of settings.ra.extraEnv because as matklad said, that configuration is only used in vscode, while neovim / lspconfig uses cmd_env to set environment variables

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.