rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Support Goto Definition into `include!`

Open cynecx opened this issue 4 years ago • 11 comments

settings.json (vscode):

{
    "rust-analyzer.cargoFeatures.loadOutDirsFromCheck": true,
}

rust-analyzer: 1c2d4135db867efe335a0654d86429bea7bb9caf (current master)

How to reproduce:

  1. Add lapin = { version = "0.34" } to your deps.
  2. 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.

cynecx avatar Mar 29 '20 17:03 cynecx

Goto definition do not work in include! macro yet. Do completion show them correctly ?

edwin0cheng avatar Mar 29 '20 18:03 edwin0cheng

Oh, I see. Yes, completions do work. Is there an already opened issue for that? (So I might close this one.)

cynecx avatar Mar 29 '20 18:03 cynecx

@cynecx This is the first issue for that :)

edwin0cheng avatar Mar 29 '20 18:03 edwin0cheng

@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.

cynecx avatar Apr 05 '20 16:04 cynecx

Maybe we update the title? I was about to open an issue for include! macros not being followed for goto definition.

fahrradflucht avatar May 03 '20 12:05 fahrradflucht

What is missing for this to be supported?

mvtec-bergdolll avatar Jun 01 '22 16:06 mvtec-bergdolll

@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.

matklad avatar Jun 17 '22 08:06 matklad

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?

g0hl1n avatar Jul 11 '22 13:07 g0hl1n

[...] 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.

sebpuetz avatar Aug 10 '22 12:08 sebpuetz

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.

bjorn3 avatar Aug 10 '22 12:08 bjorn3

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)

sebpuetz avatar Aug 10 '22 12:08 sebpuetz

@rustbot claim

jonas-schievink avatar Apr 13 '23 15:04 jonas-schievink