Skip to content

Commit 2e460de

Browse files
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 (rust-lang/rust-analyzer#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 <[email protected]>
1 parent 127b9b8 commit 2e460de

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

.bazelci/presubmit.yml

+7
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,13 @@ tasks:
690690
working_directory: examples/bzlmod/hello_world_no_cargo
691691
build_targets:
692692
- "//..."
693+
compile_one_dependency:
694+
name: --compile_one_dependency flag
695+
platform: ubuntu2004
696+
build_flags:
697+
- "--compile_one_dependency"
698+
build_targets:
699+
- "tools/rust_analyzer/main.rs"
693700

694701
buildifier:
695702
version: latest

rust/private/rust.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ _common_attrs = {
679679
is the root of the crate to be passed to rustc to build this crate.
680680
"""),
681681
allow_files = [".rs"],
682+
# Allow use of --compile_one_dependency with rust targets. Support for this feature for
683+
# non-builtin rulesets is undocumented outside of the bazel source:
684+
# https://github.com/bazelbuild/bazel/blob/7.1.1/src/main/java/com/google/devtools/build/lib/packages/Attribute.java#L102
685+
flags = ["DIRECT_COMPILE_TIME_INPUT"],
682686
),
683687
"stamp": _stamp_attribute(
684688
default_value = 0,

0 commit comments

Comments
 (0)