Skip to content

Commit d23a2e9

Browse files
committed
Add bool config setting to enable diagnostic output
1 parent 1780838 commit d23a2e9

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

BUILD.bazel

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags", "is_proc_macro_dep", "is_proc_macro_dep_enabled")
2+
load(
3+
"//rust:defs.bzl",
4+
"capture_clippy_output",
5+
"clippy_flags",
6+
"error_format",
7+
"extra_exec_rustc_flag",
8+
"extra_exec_rustc_flags",
9+
"extra_rustc_flag",
10+
"extra_rustc_flags",
11+
"is_proc_macro_dep",
12+
"is_proc_macro_dep_enabled",
13+
"output_diagnostics",
14+
)
315

416
exports_files(["LICENSE"])
517

@@ -18,6 +30,13 @@ error_format(
1830
visibility = ["//visibility:public"],
1931
)
2032

33+
# This setting may be changed from the command line to generate rustc diagnostics.
34+
output_diagnostics(
35+
name = "output_diagnostics",
36+
build_setting_default = False,
37+
visibility = ["//visibility:public"],
38+
)
39+
2140
# This setting may be used to pass extra options to clippy from the command line.
2241
# It applies across all targets.
2342
clippy_flags(

rust/defs.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ load(
4949
_extra_rustc_flags = "extra_rustc_flags",
5050
_is_proc_macro_dep = "is_proc_macro_dep",
5151
_is_proc_macro_dep_enabled = "is_proc_macro_dep_enabled",
52+
_output_diagnostics = "output_diagnostics",
5253
)
5354
load(
5455
"//rust/private:rustdoc.bzl",
@@ -103,6 +104,9 @@ rust_clippy = _rust_clippy
103104
capture_clippy_output = _capture_clippy_output
104105
# See @rules_rust//rust/private:clippy.bzl for a complete description.
105106

107+
output_diagnostics = _output_diagnostics
108+
# See @rules_rust//rust/private:rustc.bzl for a complete description.
109+
106110
error_format = _error_format
107111
# See @rules_rust//rust/private:rustc.bzl for a complete description.
108112

rust/private/rust.bzl

+8-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ def _assert_correct_dep_mapping(ctx):
6464
),
6565
)
6666

67-
6867
def _rustc_output_name(name):
69-
return name + ".rustc-output"
68+
return name + ".rustc-output"
7069

7170
def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
7271
"""See https://github.com/bazelbuild/rules_rust/issues/405
@@ -282,7 +281,8 @@ def _rust_library_common(ctx, crate_type):
282281

283282
rust_lib = ctx.actions.declare_file(rust_lib_name)
284283
rust_lib_build_output = None
285-
if ctx.attr._process_wrapper:
284+
output_diagnostics = ctx.attr._output_diagnostics
285+
if ctx.attr._process_wrapper and output_diagnostics:
286286
rust_lib_build_output = ctx.actions.declare_file(_rustc_output_name(rust_lib_name))
287287

288288
rust_metadata = None
@@ -294,7 +294,8 @@ def _rust_library_common(ctx, crate_type):
294294
rust_metadata_name,
295295
sibling = rust_lib,
296296
)
297-
rust_metadata_build_output = ctx.actions.declare_file(_rustc_output_name(rust_metadata_name))
297+
if output_diagnostics:
298+
rust_metadata_build_output = ctx.actions.declare_file(_rustc_output_name(rust_metadata_name))
298299

299300
deps = transform_deps(ctx.attr.deps)
300301
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
@@ -665,6 +666,9 @@ _common_attrs = {
665666
"_error_format": attr.label(
666667
default = Label("//:error_format"),
667668
),
669+
"_output_diagnostics": attr.label(
670+
default = Label("//:output_diagnostics"),
671+
),
668672
"_extra_exec_rustc_flag": attr.label(
669673
default = Label("//:extra_exec_rustc_flag"),
670674
),

rust/private/rustc.bzl

+32-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ ErrorFormatInfo = provider(
5252
fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"},
5353
)
5454

55+
OutputDiagnosticsInfo = provider(
56+
doc = "Save json diagnostics form rustc",
57+
fields = {"output_diagnostics": "(bool)"},
58+
)
59+
5560
ExtraRustcFlagsInfo = provider(
5661
doc = "Pass each value as an additional flag to non-exec rustc invocations",
5762
fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"},
@@ -857,10 +862,8 @@ def construct_arguments(
857862
process_wrapper_flags.add("--rustc-quit-on-rmeta", "true")
858863
if crate_info.rust_metadata_rustc_output:
859864
process_wrapper_flags.add("--output-file", crate_info.rust_metadata_rustc_output.path)
860-
else:
861-
if crate_info.rust_lib_rustc_output:
862-
process_wrapper_flags.add("--output-file", crate_info.rust_lib_rustc_output.path)
863-
865+
elif crate_info.rust_lib_rustc_output:
866+
process_wrapper_flags.add("--output-file", crate_info.rust_lib_rustc_output.path)
864867

865868
rustc_flags.add("--error-format=" + error_format)
866869

@@ -1104,7 +1107,7 @@ def rustc_compile_action(
11041107
build_flags_files = build_flags_files,
11051108
force_all_deps_direct = force_all_deps_direct,
11061109
stamp = stamp,
1107-
use_json_output = bool(build_metadata),
1110+
use_json_output = bool(build_metadata) or bool(rust_lib_rustc_output) or bool(rust_metadata_rustc_output),
11081111
)
11091112

11101113
args_metadata = None
@@ -1844,6 +1847,30 @@ error_format = rule(
18441847
build_setting = config.string(flag = True),
18451848
)
18461849

1850+
def _output_diagnostics_impl(ctx):
1851+
"""Implementation of the `output_diagnostics` rule
1852+
1853+
Args:
1854+
ctx (ctx): The rule's context object
1855+
1856+
Returns:
1857+
list: A list containing the OutputDiagnosticsInfo provider
1858+
"""
1859+
return [OutputDiagnosticsInfo(output_diagnostics = ctx.build_setting_value)]
1860+
1861+
output_diagnostics = rule(
1862+
doc = (
1863+
"Setting this flag from the command line with `--@rules_rust//:output_diagnostics` " +
1864+
"makes rules_rust save rustc json output(suitable for consumption by rust-analyzer) in a file. " +
1865+
"These are accessible via the " +
1866+
"`rust_metadata_rustc_output`(for pipelined compilation) and `rust_lib_rustc_output` output groups. " +
1867+
"You can find these either by using something like `find <dir> -name '*.rustc-output'` or by using " +
1868+
"`bazel cquery --output=files`."
1869+
),
1870+
implementation = _output_diagnostics_impl,
1871+
build_setting = config.bool(flag = True),
1872+
)
1873+
18471874
def _extra_rustc_flags_impl(ctx):
18481875
return ExtraRustcFlagsInfo(extra_rustc_flags = ctx.build_setting_value)
18491876

0 commit comments

Comments
 (0)