Skip to content

Commit

Permalink
Add flag to set --error-format from command line (#525)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djmarcin committed Dec 15, 2020
1 parent 4b2a744 commit 22d6730
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "error_format")

bzl_library(
name = "rules",
Expand All @@ -7,3 +8,10 @@ bzl_library(
],
visibility = ["//visibility:public"],
)

# This setting may be changed from the command line to generate machine readable errors.
error_format(
name = "error_format",
build_setting_default = "human",
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ rust_clippy_aspect = aspect(
allow_single_file = True,
cfg = "exec",
),
"_error_format": attr.label(default = "//:error_format"),
},
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
Expand Down
1 change: 1 addition & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ _rust_common_attrs = {
allow_single_file = True,
cfg = "exec",
),
"_error_format": attr.label(default = "//:error_format"),
}

_rust_library_attrs = {
Expand Down
21 changes: 21 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ DepInfo = provider(
},
)

_error_format_values = ["human", "json", "short"]

ErrorFormatInfo = provider(
doc = "Set the --error-format flag for all rustc invocations",
fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"},
)

def _get_rustc_env(ctx, toolchain):
"""Gathers rustc environment variables
Expand Down Expand Up @@ -471,6 +478,8 @@ def construct_arguments(
args.add(crate_info.root)
args.add("--crate-name=" + crate_info.name)
args.add("--crate-type=" + crate_info.type)
if hasattr(ctx.attr, "_error_format"):
args.add("--error-format=" + ctx.attr._error_format[ErrorFormatInfo].error_format)

# Mangle symbols to disambiguate crates with the same name
extra_filename = "-" + output_hash if output_hash else ""
Expand Down Expand Up @@ -833,3 +842,15 @@ def _get_dirname(file):
str: Directory name of `file`
"""
return file.dirname

def _error_format_impl(ctx):
raw = ctx.build_setting_value
if raw not in _error_format_values:
fail(str(ctx.label) + " expected a value in [" + ", ".join(_error_format_values) +
"] but got " + raw)
return ErrorFormatInfo(error_format = raw)

error_format = rule(
implementation = _error_format_impl,
build_setting = config.string(flag = True),
)

0 comments on commit 22d6730

Please sign in to comment.