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