Skip to content

Commit 473caac

Browse files
authored
Add support for inhibiting clippy and rustfmt aspect actions (#3080)
1 parent 5c795e5 commit 473caac

File tree

5 files changed

+41
-42
lines changed

5 files changed

+41
-42
lines changed

docs/src/rust_prost.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ register_toolchains("//toolchains:prost_toolchain")
142142
---
143143
---
144144

145+
<a id="rust_prost_library"></a>
146+
147+
## rust_prost_library
148+
149+
<pre>
150+
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-proto">proto</a>)
151+
</pre>
152+
153+
A rule for generating a Rust library using Prost.
154+
155+
**ATTRIBUTES**
156+
157+
158+
| Name | Description | Type | Mandatory | Default |
159+
| :------------- | :------------- | :------------- | :------------- | :------------- |
160+
| <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 | |
161+
| <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 | |
162+
163+
145164
<a id="rust_prost_toolchain"></a>
146165

147166
## rust_prost_toolchain
@@ -173,22 +192,3 @@ Rust Prost toolchain rule.
173192
| <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` |
174193

175194

176-
<a id="rust_prost_library"></a>
177-
178-
## rust_prost_library
179-
180-
<pre>
181-
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-kwargs">kwargs</a>)
182-
</pre>
183-
184-
A rule for generating a Rust library using Prost.
185-
186-
**PARAMETERS**
187-
188-
189-
| Name | Description | Default Value |
190-
| :------------- | :------------- | :------------- |
191-
| <a id="rust_prost_library-name"></a>name | The name of the target. | none |
192-
| <a id="rust_prost_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying `rust_prost_library` rule. | none |
193-
194-

extensions/prost/defs.bzl

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,5 @@ load(
147147
_rust_prost_toolchain = "rust_prost_toolchain",
148148
)
149149

150-
def rust_prost_library(name, **kwargs):
151-
"""A rule for generating a Rust library using Prost.
152-
153-
Args:
154-
name (str): The name of the target.
155-
**kwargs (dict): Additional keyword arguments for the underlying
156-
`rust_prost_library` rule.
157-
"""
158-
159-
# Clippy and Rustfmt will attempt to run on these targets.
160-
# This is not correct and likely a bug in target detection.
161-
tags = kwargs.pop("tags", [])
162-
if "no-clippy" not in tags:
163-
tags.append("no-clippy")
164-
if "no-rustfmt" not in tags:
165-
tags.append("no-rustfmt")
166-
167-
_rust_prost_library(
168-
name = name,
169-
tags = tags,
170-
**kwargs
171-
)
172-
150+
rust_prost_library = _rust_prost_library
173151
rust_prost_toolchain = _rust_prost_toolchain

extensions/prost/private/prost.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ def _rust_prost_aspect_impl(target, ctx):
275275
build_info = dep_variant_info.build_info,
276276
))
277277

278+
# Avoid running clippy or rustfmt on these targets the outputs
279+
# are all generated sources with no guarantee to be compliant.
280+
inhibit_output_groups = {
281+
"clippy_checks": depset(),
282+
"rustfmt_checks": depset(),
283+
}
284+
278285
return [
279286
ProstProtoInfo(
280287
dep_variant_info = dep_variant_info,
@@ -285,6 +292,7 @@ def _rust_prost_aspect_impl(target, ctx):
285292
OutputGroupInfo(
286293
rust_generated_srcs = [lib_rs],
287294
proto_descriptor_set = [proto_info.direct_descriptor_set],
295+
**inhibit_output_groups
288296
),
289297
]
290298

rust/private/clippy.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _get_clippy_ready_crate_info(target, aspect_ctx = None):
7777
ignore_tags = [
7878
"no_clippy",
7979
"no_lint",
80+
"nolint",
8081
"noclippy",
8182
]
8283
for tag in aspect_ctx.rule.attr.tags:
@@ -92,6 +93,12 @@ def _get_clippy_ready_crate_info(target, aspect_ctx = None):
9293
return None
9394

9495
def _clippy_aspect_impl(target, ctx):
96+
# Exit early if a target already has a clippy output group. This
97+
# can be useful for rules which always want to inhibit clippy.
98+
if OutputGroupInfo in target:
99+
if hasattr(target[OutputGroupInfo], "clippy_checks"):
100+
return []
101+
95102
crate_info = _get_clippy_ready_crate_info(target, ctx)
96103
if not crate_info:
97104
return [ClippyInfo(output = depset([]))]

rust/private/rustfmt.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def _perform_check(edition, srcs, ctx):
9898
return marker
9999

100100
def _rustfmt_aspect_impl(target, ctx):
101+
# Exit early if a target already has a rustfmt output group. This
102+
# can be useful for rules which always want to inhibit rustfmt.
103+
if OutputGroupInfo in target:
104+
if hasattr(target[OutputGroupInfo], "rustfmt_checks"):
105+
return []
106+
101107
crate_info = _get_rustfmt_ready_crate_info(target)
102108

103109
if not crate_info:

0 commit comments

Comments
 (0)