rust-analyzer
rust-analyzer copied to clipboard
Support Goto Definition into `include!`
settings.json (vscode):
{
"rust-analyzer.cargoFeatures.loadOutDirsFromCheck": true,
}
rust-analyzer: 1c2d4135db867efe335a0654d86429bea7bb9caf
(current master)
How to reproduce:
- Add
lapin = { version = "0.34" }
to your deps. - Use this code snippet:
use lapin::{
message::Delivery,
options::{BasicAckOptions, BasicConsumeOptions, BasicRejectOptions},
types::FieldTable,
Connection, ConnectionProperties,
};
BasicAckOptions
, BasicConsumeOptions
and BasicRejectOptions
come from proc-macro. "Go to definition" doesn't work on them. It works but redirects you a wrong source-site.
Goto definition do not work in include!
macro yet. Do completion show them correctly ?
Oh, I see. Yes, completions do work. Is there an already opened issue for that? (So I might close this one.)
@cynecx This is the first issue for that :)
@edwin0cheng ~~I am currently on master. And it seems that it's broken (In terms of it doesn't even find/resolve items). I am not quite sure when it broke though. I haven't realized since today when I came across this again.~~
Nevermind, it seems that the option changed from rust-analyzer.cargoFeatures.loadOutDirsFromCheck
to rust-analyzer.cargo.loadOutDirsFromCheck
.
Maybe we update the title? I was about to open an issue for include!
macros not being followed for goto definition.
What is missing for this to be supported?
@mvtec-bergdolll I think it's not so much "anything missing", but rather "we need to redesign one of our core abstractions to support this at all". See https://github.com/rust-lang/rust-analyzer/issues/9403.
This would make life a lot easier when working with tonic in vscode. Any ETA for a fix or any workarounds anybody is aware of?
[...] any workarounds anybody is aware of?
It's a bit hacky and brittle but you can set the output path for the generated Rust files
tonic_build::configure()
.out_dir("src/") // put the generated files under src/, could be a nested directory like src/proto, too.
.compile(&["./protobuf.proto"], &["./"])
and include the generated file like a regular module:
// lib.rs
mod protobuf;
If your CI runs cargo fmt
/ rustfmt
, you'll probably want ot run rustfmt
on the generated files since tonic_build / prost don't format the code.
If you put that in build.rs you need to be aware that cargo prevents publishing crates if it detects that building them changes their source code.
Publishing should work by adding a feature gate to the code gen (and running rustfmt), e.g. lapin
has a code gen feature: https://github.com/amqp-rs/lapin/blob/main/build.rs#L1-L7
with code gen turned off by default: https://github.com/amqp-rs/lapin/blob/main/Cargo.toml#L18
(I have no experience with publishing the workaround through cargo, so this is speculation on my end)
@rustbot claim