Skip to content

Commit 0ccebcd

Browse files
zackwintermdbMongoDB Bot
authored andcommitted
SERVER-98856 Run buildifier as part of "bazel run format" (#30689)
GitOrigin-RevId: 6970bee
1 parent a0737c0 commit 0ccebcd

File tree

6 files changed

+99
-16
lines changed

6 files changed

+99
-16
lines changed

bazel/format/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ py_binary(
2121
data = [":prettier"],
2222
main = "format.py",
2323
visibility = ["//visibility:public"],
24+
deps = [
25+
"//buildscripts:buildifier",
26+
],
2427
)

bazel/format/format.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
import pathlib
44
import subprocess
55

6+
from buildscripts import download_buildifier
7+
from buildscripts.buildifier import fix_all, lint_all
68

7-
def run_prettier(prettier: pathlib.Path, check: bool) -> int:
9+
10+
def run_buildifier(check: bool) -> bool:
11+
binary_path = os.path.join(os.curdir, "buildifier")
12+
if not os.path.exists(binary_path):
13+
download_buildifier.download(download_location=os.curdir)
14+
if check:
15+
return lint_all(binary_path, generate_report=True)
16+
else:
17+
fix_all(binary_path)
18+
return True
19+
20+
21+
def run_prettier(prettier: pathlib.Path, check: bool) -> bool:
822
# Explicitly ignore anything in the output directories or any symlinks in the root of the repository
923
# to prevent bad symlinks from failing the run, see https://github.com/prettier/prettier/issues/11568 as
1024
# to why it the paths being present in .prettierignore isn't sufficient
@@ -30,11 +44,11 @@ def run_prettier(prettier: pathlib.Path, check: bool) -> int:
3044
print("Found formatting errors. Run 'bazel run //:format' to fix")
3145
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
3246
print("python buildscripts/install_bazel.py")
33-
return 1
47+
return False
3448

3549
if check:
3650
print("No formatting errors")
37-
return 0
51+
return True
3852

3953

4054
def main() -> int:
@@ -59,7 +73,8 @@ def main() -> int:
5973
prettier_path: pathlib.Path = args.prettier.resolve()
6074

6175
os.chdir(default_dir)
62-
return run_prettier(prettier_path, args.check)
76+
77+
return 0 if run_prettier(prettier_path, args.check) and run_buildifier(args.check) else 1
6378

6479

6580
if __name__ == "__main__":

buildscripts/BUILD.bazel

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,54 @@ py_binary(
7070
],
7171
)
7272

73+
py_library(
74+
name = "install_bazel",
75+
srcs = [
76+
"install_bazel.py",
77+
],
78+
deps = [
79+
dependency(
80+
"retry",
81+
group = "testing",
82+
),
83+
],
84+
)
85+
86+
py_library(
87+
name = "unittest_grouper",
88+
srcs = [
89+
"unittest_grouper.py",
90+
],
91+
visibility = ["//visibility:public"],
92+
deps = [
93+
"install_bazel",
94+
dependency(
95+
"typing-extensions",
96+
group = "core",
97+
),
98+
],
99+
)
100+
101+
py_library(
102+
name = "download_buildifier",
103+
srcs = [
104+
"download_buildifier.py",
105+
],
106+
visibility = ["//visibility:public"],
107+
)
108+
109+
py_binary(
110+
name = "buildifier",
111+
srcs = [
112+
"buildifier.py",
113+
],
114+
main = "buildifier.py",
115+
visibility = ["//visibility:public"],
116+
deps = [
117+
"download_buildifier",
118+
"simple_report",
119+
"unittest_grouper",
120+
],
121+
)
122+
73123
exports_files(["cheetah_source_generator.py"])

buildscripts/buildifier.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import argparse
22
import json
33
import os
4-
import pathlib
54
import platform
65
import subprocess
76
import sys
87

9-
import download_buildifier
10-
from simple_report import make_report, put_report, try_combine_reports
11-
from unittest_grouper import validate_bazel_groups
12-
13-
mongo_dir = pathlib.Path(__file__).parents[1]
8+
from buildscripts import download_buildifier
9+
from buildscripts.simple_report import make_report, put_report, try_combine_reports
10+
from buildscripts.unittest_grouper import validate_bazel_groups
1411

1512

1613
def find_all_failed(bin_path: str) -> list[str]:
1714
# TODO(SERVER-81039): Remove once third_party libs can be compiled from the root directory.
1815
ignored_paths = []
19-
with open(os.path.join(mongo_dir, ".bazelignore"), "r") as file:
16+
with open(os.path.join(os.curdir, ".bazelignore"), "r", encoding="utf-8") as file:
2017
for line in file.readlines():
2118
contents = line.split("#")[0].strip()
2219
if contents:
@@ -43,8 +40,9 @@ def find_all_failed(bin_path: str) -> list[str]:
4340

4441
def lint_all(bin_path: str, generate_report: bool):
4542
files = find_all_failed(bin_path)
46-
lint(bin_path, files, generate_report)
43+
result = lint(bin_path, files, generate_report)
4744
validate_bazel_groups(generate_report=generate_report, fix=False, quick=False)
45+
return result
4846

4947

5048
def fix_all(bin_path: str):
@@ -60,6 +58,7 @@ def fix_unittests(bin_path: str):
6058

6159

6260
def lint(bin_path: str, files: list[str], generate_report: bool):
61+
found_errors = False
6362
for file in files:
6463
process = subprocess.run(
6564
[bin_path, "--format=json", "--mode=check", file], check=True, capture_output=True
@@ -76,6 +75,7 @@ def lint(bin_path: str, files: list[str], generate_report: bool):
7675
diff = process.stdout
7776
print(f"{file} has linting errors")
7877
print(diff)
78+
found_errors = True
7979

8080
if generate_report:
8181
header = (
@@ -91,6 +91,7 @@ def lint(bin_path: str, files: list[str], generate_report: bool):
9191
put_report(report)
9292

9393
print("Done linting files")
94+
return not found_errors
9495

9596

9697
def fix(bin_path: str, files: list[str]):
@@ -101,6 +102,14 @@ def fix(bin_path: str, files: list[str]):
101102

102103

103104
def main():
105+
default_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY")
106+
if not default_dir:
107+
print("This script must be run though bazel. Please run 'bazel run //:format' instead")
108+
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
109+
print("python buildscripts/install_bazel.py")
110+
return 1
111+
os.chdir(default_dir)
112+
104113
parser = argparse.ArgumentParser(description="buildifier wrapper")
105114
parser.add_argument(
106115
"--binary-dir",
@@ -139,9 +148,6 @@ def main():
139148
lint_parser.set_defaults(subcommand="fix")
140149

141150
args = parser.parse_args()
142-
assert os.path.abspath(os.curdir) == str(
143-
mongo_dir.absolute()
144-
), "buildifier.py must be run from the root of the mongo repo"
145151
binary_name = "buildifier.exe" if platform.system() == "Windows" else "buildifier"
146152
if args.binary_dir:
147153
binary_path = os.path.join(args.binary_dir, binary_name)

buildscripts/unittest_grouper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import urllib.request
1111
from typing import Dict, List
1212

13-
from simple_report import make_report, put_report, try_combine_reports
13+
from buildscripts.simple_report import make_report, put_report, try_combine_reports
1414

1515
sys.path.append(".")
1616

evergreen/bazel_run.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ elif [[ $ARCH == "s390x" ]]; then
5252
export JAVA_HOME="/usr/lib/jvm/java-21-openjdk"
5353
fi
5454

55+
# AL2 stores certs in a nonstandard location
56+
if [[ -f /etc/os-release ]]; then
57+
DISTRO=$(awk -F '[="]*' '/^PRETTY_NAME/ { print $2 }' < /etc/os-release)
58+
if [[ $DISTRO == "Amazon Linux 2" ]]; then
59+
export SSL_CERT_DIR=/etc/pki/tls/certs
60+
export SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
61+
fi
62+
fi
63+
5564
# Print command being run to file that can be uploaded
5665
echo "python buildscripts/install_bazel.py" > bazel-invocation.txt
5766
echo "bazel run --verbose_failures $LOCAL_ARG ${args} ${target}" >> bazel-invocation.txt

0 commit comments

Comments
 (0)