Rust Analyzer in Emacs with different targets within a workspace doesn't work

I a developing a kernel, so I can only build my project with target x86_64-unknown-none (having other targets will make rust complain about found duplicate lang item `panic_impl` ). However, to build and run my kernel, I actually need to link it to a bootloader and launch qemu, so I put the crate in a workspace, and added an other crate whose purpose is to do all that for me, as indicated in the documentation of the bootloader I use. The layout is thus the following

.
├── Cargo.toml
├── build.rs
├── src/
│   └── main.rs
└── kernel/
    ├── Cargo.toml
    ├── .cargo/
    │   └── config.toml
    └── src/
        └── main.rs

The idea is that kernel is a build dependency of the main crate (the one at the toplevel) producing a binary artifact that is consumed by the main crate, and used to produce an actual, bootable image. The build.rs is responsible for linking properly the kernel with the bootloader. main.rs in the root crate is responsible for create a suitable qemu command and launching it.

The important part (I think) is that the toplevel crate has to be built / ran with the host target, whereas the kernel crate (as specified in kernel/.cargo/config.toml) as to be built for the x86_64-unknown-none target.


Now that the context is given, I can explain my actual problem: I can't make rust-analyzer happy about this setup. By default, I have the impression that it plainly ignores the kernel/.cargo/config.toml and tries to build the kernel crate for every possible target, which makes it complain about found duplicate lang item `panic_impl` . If I specify, in my text editor (Emacs) that it should not compile all targets, but only for x86_64-unknown-none by setting lsp-rust-all-targets to nil and lst-rust-analyzer-cargo-target to x86_64-unknown-none in a .dir-locals.el file located in kernel/src, I have the impression that rust analyzer will just hang forever. In my modeline, it will show LSP[rust-analyzer:port/starting] with a small loading animation. It won't show any error or warning, and any attempt to use directly its functionalities (ie. show me the type of this thing, goto the definition of this one, give me information about this symbol, show me the doc, ...) will plainly do nothing. When asking lsp to give me the capabilities of rust-analyzer, it will just show it has none.

Surprisingly enough, if I remove the lsp-rust-analyzer-cargo-target variable declaration in .dir-locals.el, it doesn't work, but if I set it manually after having open src/kernel/main.rs, it will work (that is, if I do (setq lsp-rust-analyzer-cargo-target "x86_64-unknown-none") then restart the server) but produce yet another error, which is can't find crate for `test` . Googling that results in people saying it's still an all-targets configuration issue (that is, all targets should be disable, and the target x86_64-unknown-none should be put instead), but I've already configured that :confused: .

I suspect that I failed to configure rust-analyzer to make it understand the two crates of my workspace need different build targets, and in general it fails to pickup properly my configuration, but I have to admit I don't really know what to do anymore.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.