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"],
}
@GeordieP
Copy link
Author

GeordieP commented Aug 18, 2023

@cristianpjensen

Now, I have the same problem, but with all #[config(not(feature = "ssr"))]. Do you have a solution that enables the LSP for all code, SSR and non-SSR?

I don't right now, sorry! I haven't worked with leptos in a minute and the issue isn't fresh in my mind. One thing we could try is outlined in this stackoverflow post but it would additionally require changing all the SSR-related #[config] lines in a project. I'll try and find some time to test it out today.

Another option may be to totally disable the dead code lint warning, but obviously that's not ideal.

@cristianpjensen
Copy link

@cristianpjensen

Now, I have the same problem, but with all #[config(not(feature = "ssr"))]. Do you have a solution that enables the LSP for all code, SSR and non-SSR?

I don't right now, sorry! I haven't worked with leptos in a minute and the issue isn't fresh in my mind. One thing we could try is outlined in this stackoverflow post but it would additionally require changing all the SSR-related #[config] lines in a project. I'll try and find some time to test it out today.

Another option may be to totally disable the dead code lint warning, but obviously that's not ideal.

Hey, thanks for your reply! That stackoverflow post seems promising. I will try it out tomorrow and report back.

@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