Skip to content

Commit

Permalink
Add support for inhibiting clippy and rustfmt aspect actions (#3080)
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre authored Dec 11, 2024
1 parent 5c795e5 commit 473caac
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
38 changes: 19 additions & 19 deletions docs/src/rust_prost.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ register_toolchains("//toolchains:prost_toolchain")
---
---

<a id="rust_prost_library"></a>

## rust_prost_library

<pre>
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-proto">proto</a>)
</pre>

A rule for generating a Rust library using Prost.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_prost_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_prost_library-proto"></a>proto | A `proto_library` target for which to generate Rust gencode. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


<a id="rust_prost_toolchain"></a>

## rust_prost_toolchain
Expand Down Expand Up @@ -173,22 +192,3 @@ Rust Prost toolchain rule.
| <a id="rust_prost_toolchain-tonic_runtime"></a>tonic_runtime | The Tonic runtime crates to use. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |


<a id="rust_prost_library"></a>

## rust_prost_library

<pre>
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-kwargs">kwargs</a>)
</pre>

A rule for generating a Rust library using Prost.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="rust_prost_library-name"></a>name | The name of the target. | none |
| <a id="rust_prost_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying `rust_prost_library` rule. | none |


24 changes: 1 addition & 23 deletions extensions/prost/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,5 @@ load(
_rust_prost_toolchain = "rust_prost_toolchain",
)

def rust_prost_library(name, **kwargs):
"""A rule for generating a Rust library using Prost.
Args:
name (str): The name of the target.
**kwargs (dict): Additional keyword arguments for the underlying
`rust_prost_library` rule.
"""

# Clippy and Rustfmt will attempt to run on these targets.
# This is not correct and likely a bug in target detection.
tags = kwargs.pop("tags", [])
if "no-clippy" not in tags:
tags.append("no-clippy")
if "no-rustfmt" not in tags:
tags.append("no-rustfmt")

_rust_prost_library(
name = name,
tags = tags,
**kwargs
)

rust_prost_library = _rust_prost_library
rust_prost_toolchain = _rust_prost_toolchain
8 changes: 8 additions & 0 deletions extensions/prost/private/prost.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ def _rust_prost_aspect_impl(target, ctx):
build_info = dep_variant_info.build_info,
))

# Avoid running clippy or rustfmt on these targets the outputs
# are all generated sources with no guarantee to be compliant.
inhibit_output_groups = {
"clippy_checks": depset(),
"rustfmt_checks": depset(),
}

return [
ProstProtoInfo(
dep_variant_info = dep_variant_info,
Expand All @@ -285,6 +292,7 @@ def _rust_prost_aspect_impl(target, ctx):
OutputGroupInfo(
rust_generated_srcs = [lib_rs],
proto_descriptor_set = [proto_info.direct_descriptor_set],
**inhibit_output_groups
),
]

Expand Down
7 changes: 7 additions & 0 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _get_clippy_ready_crate_info(target, aspect_ctx = None):
ignore_tags = [
"no_clippy",
"no_lint",
"nolint",
"noclippy",
]
for tag in aspect_ctx.rule.attr.tags:
Expand All @@ -92,6 +93,12 @@ def _get_clippy_ready_crate_info(target, aspect_ctx = None):
return None

def _clippy_aspect_impl(target, ctx):
# Exit early if a target already has a clippy output group. This
# can be useful for rules which always want to inhibit clippy.
if OutputGroupInfo in target:
if hasattr(target[OutputGroupInfo], "clippy_checks"):
return []

crate_info = _get_clippy_ready_crate_info(target, ctx)
if not crate_info:
return [ClippyInfo(output = depset([]))]
Expand Down
6 changes: 6 additions & 0 deletions rust/private/rustfmt.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def _perform_check(edition, srcs, ctx):
return marker

def _rustfmt_aspect_impl(target, ctx):
# Exit early if a target already has a rustfmt output group. This
# can be useful for rules which always want to inhibit rustfmt.
if OutputGroupInfo in target:
if hasattr(target[OutputGroupInfo], "rustfmt_checks"):
return []

crate_info = _get_rustfmt_ready_crate_info(target)

if not crate_info:
Expand Down

0 comments on commit 473caac

Please sign in to comment.