bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
650 stars 410 forks source link

Integration with rust-analyzer's automatic project discovery #2755

Open davidbarsky opened 1 month ago

davidbarsky commented 1 month ago

Hi folks! I added Cargo-style automatic project discovery in rust-analyzer for non-Cargo build systems (Buck primarily, but it's designed to be usable by Bazel...) in https://github.com/rust-lang/rust-analyzer/pull/17246. The documentation for this feature is in the manual, under "rust-analyzer.workspace.discoverConfig". I've been using it with Buck for the last month and a half, and while some details are still likely to change, the general shape of the API is in a spot that I'm happy with. I think this would make the Rust IDE experience with Bazel a lot nicer and I'd be very happy to provide guidance on integrating this feature into rules_rust!

jondo2010 commented 1 month ago

For reference, the Buck-side implementation of this can be found here: https://github.com/facebook/buck2/tree/main/integrations/rust-project/src

otiv-emiel-vanseveren commented 1 month ago

Hi, @davidbarsky I read a few of the discussions on RAtoml (including: https://github.com/rust-lang/rust-analyzer/pull/17246). But I don't fully understand the concept/advantages of ra's automatic project discovery. Would you mind explaining? In what ways will it improve the Rust Bazel IDE experience?

davidbarsky commented 1 month ago

But I don't fully understand the concept/advantages of ra's automatic project discovery. Would you mind explaining? In what ways will it improve the Rust Bazel IDE experience?

I think it improves the situation in two major ways:

  1. For VS Code, Bazel directs users to setup a task that generates a rust-project.json for the entire workspace. In my opinion as a rust-analyzer team member, this has two downsides:
    1. Generating a rust-project.json for the entire workspace might not scale to larger workspaces. I don't believe this is an inherent limitation to @rules_rust//tools/rust_analyzer:gen_rust_project, but https://github.com/rust-lang/rust-analyzer/pull/17246 can be substantially more fine-grained with what is and isn't indexed.
    2. @rules_rust//tools/rust_analyzer:gen_rust_project runs on editor starting up, but doesn't necessarily refresh the workspace if dependencies are added or removed. The PR I linked to can reload when a BUILD file changes.
  2. Since the functionality lives in the rust-analyzer server, support across editors wouldn't require per-editor work—the required integrations are O(build_system), not O(build system * editor). I also think that the video I attached to the PR is pretty nice—it looks decently Cargo-like.
otiv-emiel-vanseveren commented 1 month ago

Thanks for the explanation!

  1. Generating a rust-project.json for the entire workspace might not scale to larger workspaces. I don't believe this is an inherent limitation to @rules_rust//tools/rust_analyzer:gen_rust_project

Does this mean it could generate based on the open buffers / crate you're currently in ?

davidbarsky commented 1 month ago

Does this mean it could generate based on the open buffers / crate you're currently in ?

Yes, but you can still can still generate a rust-project.json for the entire workspace if somthing like /... is provided as an argument to the command—the scaling is just a bit more graceful.

illicitonion commented 1 month ago

Thanks for the pointer @davidbarsky, this looks really nice!

I'd happily review a contribution if someone wanted to put one together :)

otiv-emiel-vanseveren commented 3 weeks ago

Hi @davidbarsky, I’d like to contribute, but I’ll need some guidance. It looks like I'll need to update the existing rust-project-gen similar to the one Buck has (reference above)? mainly what buck's rust-project develop(+json) and check cmds implement? What else is needed?

Does rust-analyzer provide the currently edited files?