-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathlint.py
252 lines (216 loc) · 9.07 KB
/
lint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import pathlib
import platform
import subprocess
import sys
import tempfile
from typing import List
REPO_ROOT = pathlib.Path(__file__).parent.parent.parent
sys.path.append(str(REPO_ROOT))
def create_build_files_in_new_js_dirs() -> None:
base_dirs = ["src/mongo/db/modules/enterprise/jstests", "jstests"]
for base_dir in base_dirs:
for root, dirs, _ in os.walk(base_dir):
for dir in dirs:
full_dir = os.path.join(root, dir)
build_file_path = os.path.join(full_dir, "BUILD.bazel")
if not os.path.isfile(build_file_path):
js_files = [f for f in os.listdir(full_dir) if f.endswith(".js")]
if js_files:
with open(build_file_path, "w", encoding="utf-8") as build_file:
build_file.write("""load("@aspect_rules_js//js:defs.bzl", "js_library")
js_library(
name = "all_javascript_files",
srcs = glob([
"*.js",
]),
target_compatible_with = select({
"//bazel/config:ppc_or_s390x": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
)
""")
print(f"Created BUILD.bazel in {full_dir}")
def list_files_with_targets(bazel_bin: str) -> List:
return [
line.strip()
for line in subprocess.run(
[bazel_bin, "query", 'kind("source file", deps(//...))', "--keep_going"],
capture_output=True,
text=True,
check=False,
).stdout.splitlines()
]
def list_files_without_targets(
files_with_targets: List[str],
type_name: str,
ext: str,
dirs: List[str],
) -> bool:
# rules_lint only checks files that are in targets, verify that all files in the source tree
# are contained within targets.
exempt_list = {
# TODO(SERVER-101360): Remove the exemptions below once resolved.
"src/mongo/crypto/fle_options.cpp",
# TODO(SERVER-101368): Remove the exemptions below once resolved.
"src/mongo/db/modules/enterprise/src/streams/commands/update_connection.cpp",
# TODO(SERVER-101370): Remove the exemptions below once resolved.
"src/mongo/db/modules/enterprise/src/streams/third_party/mongocxx/dist/mongocxx/test_util/client_helpers.cpp",
# TODO(SERVER-101371): Remove the exemptions below once resolved.
"src/mongo/db/modules/enterprise/src/streams/util/tests/concurrent_memory_aggregator_test.cpp",
# TODO(SERVER-101373): Remove the exemptions below once resolved.
"src/mongo/executor/network_interface_thread_pool_test.cpp",
# TODO(SERVER-101375): Remove the exemptions below once resolved.
"src/mongo/platform/decimal128_dummy.cpp",
# TODO(SERVER-101377): Remove the exemptions below once resolved.
"src/mongo/util/icu_init_stub.cpp",
# TODO(SERVER-101377): Remove the exemptions below once resolved.
"src/mongo/util/processinfo_emscripten.cpp",
"src/mongo/util/processinfo_macOS.cpp",
"src/mongo/util/processinfo_solaris.cpp",
}
typed_files_in_targets = [line for line in files_with_targets if line.endswith(f".{ext}")]
print(f"Checking that all {type_name} files have BUILD.bazel targets...")
all_typed_files = (
subprocess.check_output(
["find", *dirs, "-name", f"*.{ext}"],
stderr=subprocess.STDOUT,
)
.decode("utf-8")
.splitlines()
)
# Convert typed_files_in_targets to a set for easy comparison
typed_files_in_targets_set = set()
for file in typed_files_in_targets:
# Remove the leading "//" and replace ":" with "/"
clean_file = file.lstrip("//").replace(":", "/")
typed_files_in_targets_set.add(clean_file)
# Create a new list of files that are in all_typed_files but not in typed_files_in_targets
new_list = []
for file in all_typed_files:
if file not in typed_files_in_targets_set and file not in exempt_list:
new_list.append(file)
if len(new_list) != 0:
print(f"Found {type_name} files without BUILD.bazel definitions:")
for file in new_list:
print(f"\t{file}")
print("")
print(
f"Please add these to a {ext}_library target in a BUILD.bazel file in their directory"
)
print("Run the following to attempt to fix the issue automatically:")
print("\tbazel run lint --fix")
return False
print(f"All {type_name} files have BUILD.bazel targets!")
return True
def run_rules_lint(bazel_bin: str, args: List[str]) -> bool:
if platform.system() == "Windows":
print("eslint not supported on windows")
return False
if "--fix" in args:
create_build_files_in_new_js_dirs()
files_with_targets = list_files_with_targets(bazel_bin)
if not list_files_without_targets(files_with_targets, "C++", "cpp", ["src/mongo"]):
return False
if not list_files_without_targets(
files_with_targets, "javascript", "js", ["src/mongo", "jstests"]
):
return False
# Default to linting everything if no path was passed in
if len([arg for arg in args if not arg.startswith("--")]) == 0:
args = ["//..."] + args
fix = ""
with tempfile.NamedTemporaryFile(delete=False) as buildevents:
buildevents_path = buildevents.name
args.append("--aspects=//tools/lint:linters.bzl%eslint")
args.extend(
[
# Allow lints of code that fails some validation action
# See https://github.com/aspect-build/rules_ts/pull/574#issuecomment-2073632879
"--norun_validations",
f"--build_event_json_file={buildevents_path}",
"--output_groups=rules_lint_human",
"--remote_download_regex='.*AspectRulesLint.*'",
]
)
# This is a rudimentary flag parser.
if "--fail-on-violation" in args:
args.extend(["--@aspect_rules_lint//lint:fail_on_violation", "--keep_going"])
args.remove("--fail-on-violation")
# Allow a `--fix` option on the command-line.
# This happens to make output of the linter such as ruff's
# [*] 1 fixable with the `--fix` option.
# so that the naive thing of pasting that flag to lint.sh will do what the user expects.
if "--fix" in args:
fix = "patch"
args.extend(["--@aspect_rules_lint//lint:fix", "--output_groups=rules_lint_patch"])
args.remove("--fix")
# the --dry-run flag must immediately follow the --fix flag
if "--dry-run" in args:
fix = "print"
args.remove("--dry-run")
args = (
[arg for arg in args if arg.startswith("--") and arg != "--"]
+ ["--"]
+ [arg for arg in args if not arg.startswith("--")]
)
# Actually run the lint itself
subprocess.run([bazel_bin, "build"] + args, check=True)
# Parse out the reports from the build events
filter_expr = '.namedSetOfFiles | values | .files[] | select(.name | endswith($ext)) | ((.pathPrefix | join("/")) + "/" + .name)'
# Maybe this could be hermetic with bazel run @aspect_bazel_lib//tools:jq or sth
# jq on windows outputs CRLF which breaks this script. https://github.com/jqlang/jq/issues/92
valid_reports = (
subprocess.run(
["jq", "--arg", "ext", ".out", "--raw-output", filter_expr, buildevents_path],
capture_output=True,
text=True,
check=True,
)
.stdout.strip()
.split("\n")
)
failing_reports = 0
for report in valid_reports:
# Exclude coverage reports, and check if the output is empty.
if "coverage.dat" in report or not os.path.exists(report) or not os.path.getsize(report):
# Report is empty. No linting errors.
continue
failing_reports += 1
print(f"From {report}:")
with open(report, "r", encoding="utf-8") as f:
print(f.read())
print()
# Apply fixes if requested
if fix:
valid_patches = (
subprocess.run(
["jq", "--arg", "ext", ".patch", "--raw-output", filter_expr, buildevents_path],
capture_output=True,
text=True,
check=True,
)
.stdout.strip()
.split("\n")
)
for patch in valid_patches:
# Exclude coverage, and check if the patch is empty.
if "coverage.dat" in patch or not os.path.exists(patch) or not os.path.getsize(patch):
# Patch is empty. No linting errors.
continue
if fix == "print":
print(f"From {patch}:")
with open(patch, "r", encoding="utf-8") as f:
print(f.read())
print()
elif fix == "patch":
subprocess.run(
["patch", "-p1"], check=True, stdin=open(patch, "r", encoding="utf-8")
)
else:
print(f"ERROR: unknown fix type {fix}", file=sys.stderr)
return False
elif failing_reports != 0:
return False
return True