rust_analyzer: query discovery targets and add a root target query (#4414)

Closes #4141

- Discovery resolves its target pattern with `bazel query --keep_going`
and passes the labels to the build with `--target_pattern_file`. Targets
tagged `manual` now show up in rust-project.json.
`--skip_incompatible_explicit_targets` keeps incompatible targets from
failing the build.
- New `setup --root-target-query=<QUERY>` flag. It is stored in
`user_config.json` and replaces `//...` for workspace-wide discovery,
e.g. `--root-target-query='kind("rust_.* rule", //...)'`. An empty value
clears it. Per-package discovery is unchanged.
- Docs: flag row and a "Discovered targets" section in
`docs/src/rust_analyzer.md`.

Tests:
- Unit tests in `lib.rs`, `user_config.rs` and `setup.rs`.
- New `//test/rust_analyzer:target_query_test` (added to the linux and
macOS ide integration lists): checks a `manual` target is discovered,
then that a root query narrows the result.
- Ran locally on Linux: `bazel test //tools/rust_analyzer/...
--config=clippy`, `--config=rustfmt`, `target_query_test`,
`partial_failure_test`.

This change was written with AI assistance (Claude). I reviewed and
tested it.

Assisted by Claude.
8 files changed
tree: b2d058813bb1e0f555b91ed7cdd630e1cf86a4da
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. cargo/
  5. crate_universe/
  6. docs/
  7. examples/
  8. extensions/
  9. ffi/
  10. nix/
  11. rust/
  12. test/
  13. tools/
  14. util/
  15. .bazelignore
  16. .bazelrc
  17. .clang-format
  18. .clippy.toml
  19. .envrc
  20. .gitattributes
  21. .gitignore
  22. .pre-commit-config.yaml
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. .typos.toml
  26. ARCHITECTURE.md
  27. AUTHORS
  28. BUILD.bazel
  29. CODEOWNERS
  30. COMPATIBILITY.md
  31. CONTRIBUTING.md
  32. CONTRIBUTORS
  33. LICENSE.txt
  34. MODULE.bazel
  35. README.md
  36. version.bzl
  37. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Starter repo

The fastest way to try this in an empty project is to click the green “Use this template” button on https://github.com/bazel-starters/rust.

Community

General discussions and announcements take place in the GitHub Discussions, but there are additional places where community members gather to discuss rules_rust.

Documentation

Please refer to the full documentation.