Skip to content

Commit 22d6730

Browse files
authored
Add flag to set --error-format from command line (#525)
This PR adds the ability for users to pass the flag `--@io_bazel_rules_rust//:error_format=json` in order to get machine readable output. The options match https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format This is most useful for IDE integrations that want to be able to parse compilation errors.
1 parent 4b2a744 commit 22d6730

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "error_format")
23

34
bzl_library(
45
name = "rules",
@@ -7,3 +8,10 @@ bzl_library(
78
],
89
visibility = ["//visibility:public"],
910
)
11+
12+
# This setting may be changed from the command line to generate machine readable errors.
13+
error_format(
14+
name = "error_format",
15+
build_setting_default = "human",
16+
visibility = ["//visibility:public"],
17+
)

rust/private/clippy.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ rust_clippy_aspect = aspect(
144144
allow_single_file = True,
145145
cfg = "exec",
146146
),
147+
"_error_format": attr.label(default = "//:error_format"),
147148
},
148149
toolchains = [
149150
"@io_bazel_rules_rust//rust:toolchain",

rust/private/rust.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ _rust_common_attrs = {
472472
allow_single_file = True,
473473
cfg = "exec",
474474
),
475+
"_error_format": attr.label(default = "//:error_format"),
475476
}
476477

477478
_rust_library_attrs = {

rust/private/rustc.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ DepInfo = provider(
7676
},
7777
)
7878

79+
_error_format_values = ["human", "json", "short"]
80+
81+
ErrorFormatInfo = provider(
82+
doc = "Set the --error-format flag for all rustc invocations",
83+
fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"},
84+
)
85+
7986
def _get_rustc_env(ctx, toolchain):
8087
"""Gathers rustc environment variables
8188
@@ -471,6 +478,8 @@ def construct_arguments(
471478
args.add(crate_info.root)
472479
args.add("--crate-name=" + crate_info.name)
473480
args.add("--crate-type=" + crate_info.type)
481+
if hasattr(ctx.attr, "_error_format"):
482+
args.add("--error-format=" + ctx.attr._error_format[ErrorFormatInfo].error_format)
474483

475484
# Mangle symbols to disambiguate crates with the same name
476485
extra_filename = "-" + output_hash if output_hash else ""
@@ -833,3 +842,15 @@ def _get_dirname(file):
833842
str: Directory name of `file`
834843
"""
835844
return file.dirname
845+
846+
def _error_format_impl(ctx):
847+
raw = ctx.build_setting_value
848+
if raw not in _error_format_values:
849+
fail(str(ctx.label) + " expected a value in [" + ", ".join(_error_format_values) +
850+
"] but got " + raw)
851+
return ErrorFormatInfo(error_format = raw)
852+
853+
error_format = rule(
854+
implementation = _error_format_impl,
855+
build_setting = config.string(flag = True),
856+
)

0 commit comments

Comments
 (0)