Skip to content

Commit 828c807

Browse files
authored
Added rust_unpretty_aspect and rust_unpretty rules (#2356)
Proposal for #1642 Duplicates #1643 (special thanks to @freeformstu) ### Summary Rustc can be used to expand all macros so that you can inspect the generated source files easier. This feature is enabled via `-Zunpretty={mode}`. The `-Z` flag is only available in the nightly version of `rustc` (rust-lang/rust#43364). ### Unprettying Build and test your targets normally. ``` bazel build //:ok_binary INFO: Analyzed target //:ok_binary (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //:ok_binary up-to-date: bazel-bin/ok_binary INFO: Elapsed time: 0.081s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action ``` Use the aspect to generate the expanded files in as a one-off build. (`.bazelrc`) ``` # Enable unpretty for all targets in the workspace build:unpretty --aspects=@rules_rust//rust:defs.bzl%rust_unpretty_aspect build:unpretty --output_groups=+rust_unpretty # `unpretty` requires the nightly toolchain. See tracking issue: # rust-lang/rust#43364 build:unpretty --@rules_rust//rust/toolchain/channel=nightly ``` ``` bazel build --config=unpretty //:ok_binary INFO: Analyzed target //:ok_binary (1 packages loaded, 2 targets configured). INFO: Found 1 target... Aspect @rules_rust//rust/private:unpretty.bzl%rust_unpretty_aspect of //:ok_binary up-to-date: bazel-bin/ok_binary.expand.rs INFO: Elapsed time: 0.149s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action ``` Targeting tests is valid as well. ``` bazel build --config=unpretty //:ok_test INFO: Analyzed target //:ok_test (0 packages loaded, 2 targets configured). INFO: Found 1 target... Aspect @rules_rust//rust/private:unpretty.bzl%rust_expand_aspect of //:ok_test up-to-date: bazel-bin/test-397521499/ok_test.expand.rs INFO: Elapsed time: 0.113s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action ``` Finally, manually wire up a `rust_unpretty` target explicitly if you want a target to build. This rule is unique compared to the aspect in that it forces a transition to a nightly toolchain so that `-Zunpretty` can be used. ```starlark load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_unpretty") rust_binary( name = "ok_binary", srcs = ["src/main.rs"], edition = "2021", ) rust_unpretty( name = "ok_binary_expand", deps = [":ok_binary"], ) ``` ``` bazel build //:ok_binary_expand INFO: Analyzed target //:ok_binary_expand (0 packages loaded, 1 target configured). INFO: Found 1 target... Target //:ok_binary_expand up-to-date: bazel-bin/ok_binary.expand.rs INFO: Elapsed time: 0.090s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action ```
1 parent 6a93592 commit 828c807

File tree

12 files changed

+565
-50
lines changed

12 files changed

+565
-50
lines changed

.bazelci/presubmit.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ single_rust_channel_targets: &single_rust_channel_targets
1515
- "//..."
1616
# These tests are expected to fail as they require both a nightly and stable toolchain.
1717
- "-//test/unit/channel_transitions/..."
18+
- "-//test/unpretty/..."
1819
default_linux_targets: &default_linux_targets
1920
- "--"
2021
- "//..."
@@ -169,6 +170,13 @@ tasks:
169170
test_targets: *default_windows_targets
170171
soft_fail: yes
171172
bazel: "rolling"
173+
ubuntu2004_unpretty:
174+
name: Unpretty
175+
platform: ubuntu2004
176+
build_targets: *default_linux_targets
177+
test_targets: *default_linux_targets
178+
build_flags:
179+
- "--config=unpretty"
172180
ubuntu2004_bzlmod_only:
173181
name: With bzlmod
174182
platform: ubuntu2004

.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ build:rustfmt --output_groups=+rustfmt_checks
3232
build:clippy --aspects=//rust:defs.bzl%rust_clippy_aspect
3333
build:clippy --output_groups=+clippy_checks
3434

35+
# Enable unpretty for all targets in the workspace
36+
build:unpretty --aspects=//rust:defs.bzl%rust_unpretty_aspect
37+
build:unpretty --output_groups=+rust_unpretty
38+
39+
# `unpretty` requires the nightly toolchain. See tracking issue:
40+
# https://github.com/rust-lang/rust/issues/43364
41+
build:unpretty --//rust/toolchain/channel=nightly
42+
3543
###############################################################################
3644
## Incompatibility flags
3745
###############################################################################

rust/defs.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ load(
6565
_rustfmt_aspect = "rustfmt_aspect",
6666
_rustfmt_test = "rustfmt_test",
6767
)
68+
load(
69+
"//rust/private:unpretty.bzl",
70+
_rust_unpretty = "rust_unpretty",
71+
_rust_unpretty_aspect = "rust_unpretty_aspect",
72+
)
6873

6974
rust_library = _rust_library
7075
# See @rules_rust//rust/private:rust.bzl for a complete description.
@@ -111,6 +116,12 @@ capture_clippy_output = _capture_clippy_output
111116
rustc_output_diagnostics = _rustc_output_diagnostics
112117
# See @rules_rust//rust/private:rustc.bzl for a complete description.
113118

119+
rust_unpretty_aspect = _rust_unpretty_aspect
120+
# See @rules_rust//rust/private:unpretty.bzl for a complete description.
121+
122+
rust_unpretty = _rust_unpretty
123+
# See @rules_rust//rust/private:unpretty.bzl for a complete description.
124+
114125
error_format = _error_format
115126
# See @rules_rust//rust/private:rustc.bzl for a complete description.
116127

rust/private/rust.bzl

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@ def _rust_binary_impl(ctx):
266266

267267
return providers
268268

269+
def get_rust_test_flags(attr):
270+
"""Determine the desired rustc flags for test targets.
271+
272+
Args:
273+
attr (dict): Attributes of a rule
274+
275+
Returns:
276+
List: A list of test flags
277+
"""
278+
if getattr(attr, "use_libtest_harness", True):
279+
rust_flags = ["--test"]
280+
else:
281+
rust_flags = ["--cfg", "test"]
282+
283+
return rust_flags
284+
269285
def _rust_test_impl(ctx):
270286
"""The implementation of the `rust_test` rule.
271287
@@ -394,7 +410,7 @@ def _rust_test_impl(ctx):
394410
attr = ctx.attr,
395411
toolchain = toolchain,
396412
crate_info_dict = crate_info_dict,
397-
rust_flags = ["--test"] if ctx.attr.use_libtest_harness else ["--cfg", "test"],
413+
rust_flags = get_rust_test_flags(ctx.attr),
398414
skip_expanding_rustc_env = True,
399415
)
400416
data = getattr(ctx.attr, "data", [])
@@ -487,6 +503,56 @@ def _stamp_attribute(default_value):
487503
values = [1, 0, -1],
488504
)
489505

506+
# Internal attributes core to Rustc actions.
507+
RUSTC_ATTRS = {
508+
"_cc_toolchain": attr.label(
509+
doc = (
510+
"In order to use find_cc_toolchain, your rule has to depend " +
511+
"on C++ toolchain. See `@rules_cc//cc:find_cc_toolchain.bzl` " +
512+
"docs for details."
513+
),
514+
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
515+
),
516+
"_error_format": attr.label(
517+
default = Label("//:error_format"),
518+
),
519+
"_extra_exec_rustc_flag": attr.label(
520+
default = Label("//:extra_exec_rustc_flag"),
521+
),
522+
"_extra_exec_rustc_flags": attr.label(
523+
default = Label("//:extra_exec_rustc_flags"),
524+
),
525+
"_extra_rustc_flag": attr.label(
526+
default = Label("//:extra_rustc_flag"),
527+
),
528+
"_extra_rustc_flags": attr.label(
529+
default = Label("//:extra_rustc_flags"),
530+
),
531+
"_import_macro_dep": attr.label(
532+
default = Label("//util/import"),
533+
cfg = "exec",
534+
),
535+
"_is_proc_macro_dep": attr.label(
536+
default = Label("//rust/private:is_proc_macro_dep"),
537+
),
538+
"_is_proc_macro_dep_enabled": attr.label(
539+
default = Label("//rust/private:is_proc_macro_dep_enabled"),
540+
),
541+
"_per_crate_rustc_flag": attr.label(
542+
default = Label("//:experimental_per_crate_rustc_flag"),
543+
),
544+
"_process_wrapper": attr.label(
545+
doc = "A process wrapper for running rustc on all platforms.",
546+
default = Label("//util/process_wrapper"),
547+
executable = True,
548+
allow_single_file = True,
549+
cfg = "exec",
550+
),
551+
"_rustc_output_diagnostics": attr.label(
552+
default = Label("//:rustc_output_diagnostics"),
553+
),
554+
}
555+
490556
_common_attrs = {
491557
"aliases": attr.label_keyed_string_dict(
492558
doc = dedent("""\
@@ -618,62 +684,18 @@ _common_attrs = {
618684
"""),
619685
allow_files = [".rs"],
620686
),
621-
"stamp": _stamp_attribute(default_value = 0),
687+
"stamp": _stamp_attribute(
688+
default_value = 0,
689+
),
622690
"version": attr.string(
623691
doc = "A version to inject in the cargo environment variable.",
624692
default = "0.0.0",
625693
),
626-
"_cc_toolchain": attr.label(
627-
doc = (
628-
"In order to use find_cc_toolchain, your rule has to depend " +
629-
"on C++ toolchain. See `@rules_cc//cc:find_cc_toolchain.bzl` " +
630-
"docs for details."
631-
),
632-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
633-
),
634-
"_error_format": attr.label(
635-
default = Label("//:error_format"),
636-
),
637-
"_extra_exec_rustc_flag": attr.label(
638-
default = Label("//:extra_exec_rustc_flag"),
639-
),
640-
"_extra_exec_rustc_flags": attr.label(
641-
default = Label("//:extra_exec_rustc_flags"),
642-
),
643-
"_extra_rustc_flag": attr.label(
644-
default = Label("//:extra_rustc_flag"),
645-
),
646-
"_extra_rustc_flags": attr.label(
647-
default = Label("//:extra_rustc_flags"),
648-
),
649-
"_import_macro_dep": attr.label(
650-
default = Label("//util/import"),
651-
cfg = "exec",
652-
),
653-
"_is_proc_macro_dep": attr.label(
654-
default = Label("//rust/private:is_proc_macro_dep"),
655-
),
656-
"_is_proc_macro_dep_enabled": attr.label(
657-
default = Label("//rust/private:is_proc_macro_dep_enabled"),
658-
),
659-
"_per_crate_rustc_flag": attr.label(
660-
default = Label("//:experimental_per_crate_rustc_flag"),
661-
),
662-
"_process_wrapper": attr.label(
663-
doc = "A process wrapper for running rustc on all platforms.",
664-
default = Label("//util/process_wrapper"),
665-
executable = True,
666-
allow_single_file = True,
667-
cfg = "exec",
668-
),
669-
"_rustc_output_diagnostics": attr.label(
670-
default = Label("//:rustc_output_diagnostics"),
671-
),
672694
"_stamp_flag": attr.label(
673695
doc = "A setting used to determine whether or not the `--stamp` flag is enabled",
674696
default = Label("//rust/private:stamp"),
675697
),
676-
}
698+
} | RUSTC_ATTRS
677699

678700
_coverage_attrs = {
679701
"_collect_cc_coverage": attr.label(

0 commit comments

Comments
 (0)