rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
905 stars 60 forks source link

Adding `rustc_private = true` to Global Cargo.toml to Allow Parsing in Some lsp-based IDE/editor Backends #428

Closed tempdragon closed 6 months ago

tempdragon commented 6 months ago

I noticed that on Emacs, the rustc_codegen_gcc along with some other rustc-related libraries/projects can be correctly parsed with the following lines added to Cargo.toml,

[package.metadata.rust-analyzer]
rustc_private = true

according to rust-analyzer manual (Search for "rustc_private" if you can't see it directly.).

Otherwise, the rust-analyzer will fail to parse with unresolved external crate for rustc-related crates.

Since there is probably other methods to allow the correct analysis in your IDE without this line, could you please give me some hints about how to have them parse the gcc backend correctly if you choose not to add these lines?

Thanks in advance.

antoyo commented 6 months ago

Adding these lines doesn't fix the issue for me (I use neovim and nvm-lspconfig).

Maybe you could add this setting directly in your editor-specific LSP config until we find a proper fix that also works in neovim?

tempdragon commented 6 months ago

Further Comments to the Original Question

  1. In my case, it also requires setting the "Lsp Rust Analyzer Rustc Source" to discover. Having finished these two steps, my rust-analyzer can correctly index the rustc. But according to GPT, there doesn't seem to be any alternatives to putting rustc_private in Cargo.toml.(However, being a workaround itself, it may be unnecessary for parsing the backend.)

  2. I don't use vim myself but it might be related to this: rustc source in lspconfig, as is used by init.vim(on ln 233)

    My Questions

    So how what configurations do you use to have it indexed properly now? BTW, You can see here for the original answer to this question. And, remember to install rustc source code(rust-src) with cargo. Rust Forum

sadlerap commented 6 months ago

I got rust-analyzer working in neovim a while back. I needed to do two things:

  1. Set the lines above in Cargo.toml.
  2. Set the lsp option rust-analyzer.rustc.source = "discover" (you can also set it to an explicit path).

For nvim-lspconfig in particular, you need to have the following:

require('lspconfig').rust_analyzer.setup{
  settings = {
    ['rust-analyzer'] = {
      -- add this section
      rustc = {
        source = "discover",
      },
    },
  },
}

The way I see it, there are two things that need to change. First, we need to add these lines into Cargo.toml to get things working out-of-the-box there; expecting users to juggle this in and out of their local tree doesn't make a ton of sense to me IMO. Second, we should add a section in the readme letting users know that if they want rust-analyzer support in their editor/IDE of choice, they need to set the rustc.source = "discover" option in their editor. This is going to vary from one setup to another, but it might make sense to have the configuration changes for common setups (e.g. VSCode, nvim, emacs, etc.) documented so that users don't have to figure this out.

antoyo commented 6 months ago

@sadlerap: Thanks, now it works for me!

@tempdragon: Please open a PR with the change and I'll accept it.

antoyo commented 6 months ago

Fixed in #447.