Help needed: Rust-analyzer seems to be ignoring configuration passed through nvim-lspconfig

I have been banging my head for days and I’ve run out of ideas to try. I want to set the rust-analyzer.server.extraEnv configuration option (listed here).

I went as far as trying all possible combinations of setting this but still, when examining the environment of the spawned rust-analyzer process it does not have my environment variable:

lua << EOF
vim.lsp.set_log_level("debug")
require('lspconfig').rust_analyzer.setup({
    ["server.extraEnv"] = { FOO = "bar" },
    server = {
        extraEnv = { FOO = "bar" },
        settings = {
            ["rust_analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                },
            },
            ["rust-analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                },
            },
            ["server.extraEnv"] = { FOO = "bar" },
            server = {
                extraEnv = { FOO = "bar" }
            },
        },
    },
    settings = {
        ["rust_analyzer"] = {
            ["server.extraEnv"] = { FOO = "bar" },
            server = {
                extraEnv = { FOO = "bar" }
            },
        },
        ["rust-analyzer"] = {
            ["server.extraEnv"] = { FOO = "bar" },
            server = {
                extraEnv = { FOO = "bar" }
            },
        },
        ["server.extraEnv"] = { FOO = "bar" },
        server = {
            extraEnv = { FOO = "bar" }
        },
    },
    ["rust-analyzer"] = {
        ["server.extraEnv"] = { FOO = "bar" },
        server = {
            extraEnv = { FOO = "bar" }
        },
        settings = {
            ["server.extraEnv"] = { FOO = "bar" },
            server = {
                extraEnv = { FOO = "bar" }
            },
            ["rust_analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                }
            },
            ["rust-analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                }
            },
        }
    },
    ["rust_analyzer"] = {
        ["server.extraEnv"] = { FOO = "bar" },
        server = {
            extraEnv = { FOO = "bar" }
        },
        settings = {
            ["server.extraEnv"] = { FOO = "bar" },
            server = {
                extraEnv = { FOO = "bar" }
            },
            ["rust-analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                }
            },
            ["rust_analyzer"] = {
                ["server.extraEnv"] = { FOO = "bar" },
                server = {
                    extraEnv = { FOO = "bar" }
                }
            },
        }
    }
})
EOF

If someone can shed some light on how to pass that configuration option to rust-analyzer I would hugely appreciate it, thanks!

lspconfig’s setup call does not take a server, server.extraenv, rust-analyzer, or rust_analyzer so you can delete those.

local nvim_lsp = require'lspconfig'

nvim_lsp.rust_analyzer.setup({
    settings = {
        ["rust-analyzer"] = {
            assist = {
                importGranularity = "module",
                importPrefix = "by_self",
            },
            cargo = {
                loadOutDirsFromCheck = true
            },
            procMacro = {
                enable = true
            },
        }
    }
})

That’s how you do it ^. If it’s not respecting the settings the issue is either the setting is invalid, or there is a bug in rust-analyzer.

Thank you! Indeed if I take your snippet and toggle procMacro I can see it taking effect while the extraEnv settings is being ignored. I will investigate further to see why rust-analyzer is ignoring this setting

I think this might be a vscode setting. You can use cmd_env (in setup {}) to pass environmental variables to rust_analyzer (or any language server)

1 Like

I think you’re right. I started looking into the rust-analyzer code and it doesn’t expect the extraEnv config option, which makes sense because by that time rust-analyzer is already running.

I’ll see if I can make it work with cmd_env. Btw, are the generic settings documented somewhere? I only see it mentioned in the bashls config options here nvim-lspconfig/server_configurations.md at master · neovim/nvim-lspconfig · GitHub

What do you mean generic settings? The available options to setup {} are documented in :help lspconfig-setup

I stumbled across this thread thinking it might solve my problem - I’ve built a proof of concept using tiberius and tokio-postgres amongst other things and something in my Cargo.toml implicitly depends on OpenSSL. On my system I have built a custom OpenSSL (for other reasons, months ago) and it resides in /usr/local/ssl. When I build this proof of concept I chose to not globally export an env var therefore I build with:
OPENSSL_DIR=/usr/local/ssl cargo build
and that works fine. It doesn’t work if I omit that variable.

I tried the suggestion above to set the same thing in my cmd_env yet rust-analyzer doesn’t seem to pick it up. My ~/.cache/nvim/lsp.log is full of rust-analyzer errors around how I should be setting OPENSSL_DIR or setting various PKG_CONFIG variables. For sanity check, I’m just doing like:

require('lspconfig').rust_analyzer.setup({
   cmd = { vim.env.HOME .. '/.cargo/bin/rust-analyzer' },
   cmd_env = { OPENSSL_DIR = '/usr/local/ssl' },
   -- ...
})

Note that rust-analyzer works just fine in my other projects. Any that don’t depend on OpenSSL behave fine. The irony is, I don’t require OpenSSL in this project (none of my connections are using encryption etc) and I’d love to remove the dependency yet I don’t know what is implicitly pulling it in or how to disable it.

Thanks for any help or tips.

1 Like

I’m having the same problem, trying to change the target directory for rust_analyzer so that I can avoid cargo blocking every time I want to build because rust_analyzer is running in another terminal. I tried

require('lspconfig').rust_analyzer.setup({
    cmd_env = { CARGO_TARGET_DIR = "/tmp/rust-analyzer" }
})

but when I check /tmp nothing is there.