Skip to content

Commit

Permalink
Add support for --compile_one_dependency (#2598)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
william-smith-skydio and illicitonion authored Apr 3, 2024
1 parent 127b9b8 commit 2e460de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,13 @@ tasks:
working_directory: examples/bzlmod/hello_world_no_cargo
build_targets:
- "//..."
compile_one_dependency:
name: --compile_one_dependency flag
platform: ubuntu2004
build_flags:
- "--compile_one_dependency"
build_targets:
- "tools/rust_analyzer/main.rs"

buildifier:
version: latest
Expand Down
4 changes: 4 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@ _common_attrs = {
is the root of the crate to be passed to rustc to build this crate.
"""),
allow_files = [".rs"],
# Allow use of --compile_one_dependency with rust targets. Support for this feature for
# non-builtin rulesets is undocumented outside of the bazel source:
# https://github.com/bazelbuild/bazel/blob/7.1.1/src/main/java/com/google/devtools/build/lib/packages/Attribute.java#L102
flags = ["DIRECT_COMPILE_TIME_INPUT"],
),
"stamp": _stamp_attribute(
default_value = 0,
Expand Down

0 comments on commit 2e460de

Please sign in to comment.