Skip to content

Instantly share code, notes, and snippets.

@GeordieP
Last active January 12, 2024 06:24
Show Gist options
  • Save GeordieP/07ad21a714f98deda71a9ea4330caef1 to your computer and use it in GitHub Desktop.
Save GeordieP/07ad21a714f98deda71a9ea4330caef1 to your computer and use it in GitHub Desktop.
in the leptos-start example project, neovim will mark the SSR main function as dead code.

Problem:

in the leptos-start example project, neovim will mark the SSR main function as dead code:

image

the rust-analyzer LSP client inside nvim thinks the SSR flag is disabled by default.

Fix:

one way to fix this is to pass the "ssr" flag to rust-analyzer globally. neovim LSP lets us define settings for specific language servers.

Warning

this will mean the "ssr" flag gets provided to EVERY rust project you open in neovim! be careful. you may get startup errors when you open any projects that don't support the ssr flag.

according to rust-analyzer docs, the option we should pass is called rust-analyzer.cargo.features. we'll pass it an array containing the string "ssr".

my neovim setup is based on nvim-basic-ide, so my LSP settings go in nvim/lua/user/lsp/settings/rust_analyzer.lua. you may have to write your settings in a different place.

rust_analyzer.lua

in here we add a table with the key ["rust-analyzer"], and inside that we write a structure representing the rest of the key (rust-analyzer.cargo.features).

return {
  settings = {
    ["rust-analyzer"] = {
      cargo = {
        features = { "ssr" } -- features = ssr, for LSP support in leptos SSR functions
      }
    }
  }
}

vscode

in vscode, this can be done globally or per-project by putting the following into a .vscode/settings.json file:

{
    "rust-analyzer.cargo.features": ["ssr"],
}
@RobertoMaurizzi
Copy link

I had the same problem and I've seen that LazyVim works around it using the allFeatures parameter (and expanding macros to make if_cfg! sections work. Only drawbacks, it'll compile everything again for that configuration and will keep the (several GBs of) results in your vim tmp directory.

The configuration they use is more or less:

 ["rust-analyzer"] = {
          cargo = {
            allFeatures = true,
            loadOutDirsFromCheck = true,
            runBuildScripts = true,
          },
          -- Add clippy lints for Rust.
          checkOnSave = {
            allFeatures = true,
            command = "clippy",
            extraArgs = { "--no-deps" },
          },
          procMacro = {
            enable = true,
            ignored = {
              ["async-trait"] = { "async_trait" },
              ["napi-derive"] = { "napi" },
              ["async-recursion"] = { "async_recursion" },
            },
          }
   }

The neoconf plugin should be able to merge local per-project configurations into the lsp and other plugins, but I still can't understand how I should go about configuring that (I add a file, nothing happens, not even errors...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment