Skip to content

Commit d0511f8

Browse files
committed
Export helpers from rules_lint
makes writing aspects for internal tooling easier CMK-26168 Change-Id: Ifce499f62af9f99f32f30f100055a3ce931962a9
1 parent a6df20f commit d0511f8

File tree

3 files changed

+21
-106
lines changed

3 files changed

+21
-106
lines changed

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ single_version_override(
121121
# https://github.com/aspect-build/rules_lint/pull/747
122122
"//bazel/patches:rules_lint-no_copy.patch",
123123
"//bazel/patches:rules_lint-clippy_fail_on_violation.patch",
124+
# For astrein and other internal tooling.
125+
"//bazel/patches:rules_lint_export_helpers.patch",
124126
],
125127
version = "2.0.0",
126128
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git a/lint/defs.bzl b/lint/defs.bzl
2+
new file mode 100644
3+
index 0000000..5ec52f8
4+
--- /dev/null
5+
+++ b/lint/defs.bzl
6+
@@ -0,0 +1,11 @@
7+
+"""Make helpers available to internal tooling."""
8+
+
9+
+load("//lint/private:lint_aspect.bzl", _filter_srcs = "filter_srcs")
10+
+load("//lint/private:lint_aspect.bzl", _noop_lint_action = "noop_lint_action")
11+
+load("//lint/private:lint_aspect.bzl", _output_files = "output_files")
12+
+load("//lint/private:lint_aspect.bzl", _should_visit = "should_visit")
13+
+
14+
+filter_srcs = _filter_srcs
15+
+noop_lint_action = _noop_lint_action
16+
+output_files = _output_files
17+
+should_visit = _should_visit

bazel/tools/lint_astrein.bzl

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,11 @@
11
"""Astrein linter aspect for aspect_rules_lint framework"""
22

3-
_MNEMONIC = "AspectRulesLintAstrein"
4-
5-
# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl
6-
# to avoid imports from private modules
7-
def should_visit(rule, allow_kinds, allow_filegroup_tags = []):
8-
"""Determine whether a rule is meant to be visited by a linter aspect
9-
10-
A target with the "no-lint" tag will not be visited.
3+
load("@aspect_rules_lint//lint:defs.bzl", "filter_srcs", "noop_lint_action", "output_files", "should_visit")
114

12-
Args:
13-
rule: a [rules_attributes](https://bazel.build/rules/lib/builtins/rule_attributes.html) object
14-
allow_kinds (list of string): return true if the rule's kind is in the list
15-
allow_filegroup_tags (list of string): return true if the rule is a filegroup and has a tag in this list
16-
17-
Returns:
18-
whether to apply the aspect on this rule
19-
"""
20-
if "no-lint" in rule.attr.tags:
21-
return False
22-
23-
if rule.kind in allow_kinds:
24-
return True
25-
if rule.kind == "filegroup":
26-
for allow_tag in allow_filegroup_tags:
27-
if allow_tag in rule.attr.tags:
28-
return True
29-
return False
30-
31-
# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl
32-
# to avoid imports from private modules
33-
def filter_srcs(rule):
34-
if "lint-genfiles" in rule.attr.tags:
35-
return rule.files.srcs
36-
else:
37-
return [s for s in rule.files.srcs if s.is_source and s.owner.workspace_name == ""]
5+
_MNEMONIC = "AspectRulesLintAstrein"
386

397
OUTFILE_FORMAT = "{label}.{mnemonic}.{suffix}"
408

41-
# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl
42-
# to avoid imports from private modules
43-
def output_files(mnemonic, target, ctx):
44-
"""Declare linter output files.
45-
46-
Args:
47-
mnemonic: used as part of the filename
48-
target: the target being visited by a linter aspect
49-
ctx: the aspect context
50-
51-
Returns:
52-
tuple of struct() of output files, and the OutputGroupInfo provider that the rule should return
53-
"""
54-
human_out = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "out"))
55-
56-
# NB: named ".report" as there are existing callers depending on that
57-
machine_out = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "report"))
58-
59-
# The exit codes should instead be provided as action outputs so the build succeeds.
60-
# Downstream tooling like `aspect lint` will be responsible for reading the exit codes
61-
# and interpreting them.
62-
human_exit_code = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "out.exit_code"))
63-
machine_exit_code = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "report.exit_code"))
64-
65-
human_outputs = [f for f in [human_out, human_exit_code] if f]
66-
machine_outputs = [f for f in [machine_out, machine_exit_code] if f]
67-
return struct(
68-
human = struct(
69-
out = human_out,
70-
exit_code = human_exit_code,
71-
),
72-
machine = struct(
73-
out = machine_out,
74-
exit_code = machine_exit_code,
75-
),
76-
), OutputGroupInfo(
77-
rules_lint_human = depset(human_outputs),
78-
rules_lint_machine = depset(machine_outputs),
79-
# Legacy name used by existing callers.
80-
# TODO(2.0): remove
81-
rules_lint_report = depset(machine_outputs),
82-
# Always cause the action to execute, even if the output isn't requested
83-
_validation = depset([human_out]),
84-
)
85-
86-
# Inspired by https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl
87-
def noop_lint_action(ctx, outputs):
88-
"""Generates an action that creates empty outputs when no files need linting.
89-
90-
Args:
91-
ctx: Bazel context
92-
outputs: Output files struct
93-
"""
94-
ctx.actions.write(
95-
output = outputs.human.out,
96-
content = "",
97-
)
98-
if outputs.human.exit_code:
99-
ctx.actions.write(
100-
output = outputs.human.exit_code,
101-
content = "0\n",
102-
)
103-
ctx.actions.write(
104-
output = outputs.machine.out,
105-
content = "",
106-
)
107-
if outputs.machine.exit_code:
108-
ctx.actions.write(
109-
output = outputs.machine.exit_code,
110-
content = "0\n",
111-
)
112-
1139
def astrein_action(ctx, executable, srcs, stdout, exit_code, format, astrein_runfiles):
11410
"""Run astrein linter on source files.
11511

0 commit comments

Comments
 (0)