Add support for `--compile_one_dependency` (#2598)

The `--compile_one_dependency` flag
([docs](https://bazel.build/docs/user-manual#compile-one-dependency))
changes `bazel build` etc. to accept a file path and build the target
corresponding to that file. This is useful for check-on-save with
rust-analyzer in combination with the newly-added `$saved_file` command
substitution (https://github.com/rust-lang/rust-analyzer/pull/15476).

Officially `--compile_one_dependency` only supports the builtin C++ and
Java rules, but an [undocumented
flag](https://github.com/bazelbuild/bazel/blob/7.1.1/src/main/java/com/google/devtools/build/lib/packages/Attribute.java#L102)
can be added to attributes to turn them into sources supporting
`--compile_one_dependency`. I'm not sure what the status of this support
is, but it appears to work for all bazel versions up to at least 7.1.1,
and if support is removed the flag is pretty harmless.

Before this change:
```
> bazel build --compile_one_dependency tools/rust_analyzer/main.rs
WARNING: Target pattern parsing failed.
ERROR: Couldn't find dependency on target '//tools/rust_analyzer:main.rs'
ERROR: Couldn't find dependency on target '//tools/rust_analyzer:main.rs'
INFO: Elapsed time: 0.956s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
```

After:
```
> bazel build --compile_one_dependency tools/rust_analyzer/main.rs
INFO: Analyzed target //tools/rust_analyzer:gen_rust_project (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //tools/rust_analyzer:gen_rust_project up-to-date:
  bazel-bin/tools/rust_analyzer/gen_rust_project
INFO: Elapsed time: 0.341s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
```

---------

Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
2 files changed
tree: d4b667dd87d27112bebfb039b3db9b95ec7a1056
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. bindgen/
  5. cargo/
  6. crate_universe/
  7. docs/
  8. examples/
  9. ffi/
  10. nix/
  11. proto/
  12. rust/
  13. test/
  14. tools/
  15. util/
  16. wasm_bindgen/
  17. .bazelignore
  18. .bazelrc
  19. .clang-format
  20. .envrc
  21. .gitattributes
  22. .gitignore
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. ARCHITECTURE.md
  26. AUTHORS
  27. BUILD.bazel
  28. CODEOWNERS
  29. COMPATIBILITY.md
  30. CONTRIBUTING.md
  31. CONTRIBUTORS
  32. LICENSE.txt
  33. MODULE.bazel
  34. README.md
  35. version.bzl
  36. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

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.