SERVER-98856 Run buildifier as part of "bazel run format" (#30689)

GitOrigin-RevId: 6970beeafc7ae17e507020c868d5784ce374e2d8
This commit is contained in:
Zack Winter 2024-12-30 14:42:07 -08:00 committed by MongoDB Bot
parent a0737c0d29
commit 0ccebcd4d4
6 changed files with 99 additions and 16 deletions

View File

@ -21,4 +21,7 @@ py_binary(
data = [":prettier"],
main = "format.py",
visibility = ["//visibility:public"],
deps = [
"//buildscripts:buildifier",
],
)

View File

@ -3,8 +3,22 @@ import os
import pathlib
import subprocess
from buildscripts import download_buildifier
from buildscripts.buildifier import fix_all, lint_all
def run_prettier(prettier: pathlib.Path, check: bool) -> int:
def run_buildifier(check: bool) -> bool:
binary_path = os.path.join(os.curdir, "buildifier")
if not os.path.exists(binary_path):
download_buildifier.download(download_location=os.curdir)
if check:
return lint_all(binary_path, generate_report=True)
else:
fix_all(binary_path)
return True
def run_prettier(prettier: pathlib.Path, check: bool) -> bool:
# Explicitly ignore anything in the output directories or any symlinks in the root of the repository
# to prevent bad symlinks from failing the run, see https://github.com/prettier/prettier/issues/11568 as
# 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:
print("Found formatting errors. Run 'bazel run //:format' to fix")
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
print("python buildscripts/install_bazel.py")
return 1
return False
if check:
print("No formatting errors")
return 0
return True
def main() -> int:
@ -59,7 +73,8 @@ def main() -> int:
prettier_path: pathlib.Path = args.prettier.resolve()
os.chdir(default_dir)
return run_prettier(prettier_path, args.check)
return 0 if run_prettier(prettier_path, args.check) and run_buildifier(args.check) else 1
if __name__ == "__main__":

View File

@ -70,4 +70,54 @@ py_binary(
],
)
py_library(
name = "install_bazel",
srcs = [
"install_bazel.py",
],
deps = [
dependency(
"retry",
group = "testing",
),
],
)
py_library(
name = "unittest_grouper",
srcs = [
"unittest_grouper.py",
],
visibility = ["//visibility:public"],
deps = [
"install_bazel",
dependency(
"typing-extensions",
group = "core",
),
],
)
py_library(
name = "download_buildifier",
srcs = [
"download_buildifier.py",
],
visibility = ["//visibility:public"],
)
py_binary(
name = "buildifier",
srcs = [
"buildifier.py",
],
main = "buildifier.py",
visibility = ["//visibility:public"],
deps = [
"download_buildifier",
"simple_report",
"unittest_grouper",
],
)
exports_files(["cheetah_source_generator.py"])

View File

@ -1,22 +1,19 @@
import argparse
import json
import os
import pathlib
import platform
import subprocess
import sys
import download_buildifier
from simple_report import make_report, put_report, try_combine_reports
from unittest_grouper import validate_bazel_groups
mongo_dir = pathlib.Path(__file__).parents[1]
from buildscripts import download_buildifier
from buildscripts.simple_report import make_report, put_report, try_combine_reports
from buildscripts.unittest_grouper import validate_bazel_groups
def find_all_failed(bin_path: str) -> list[str]:
# TODO(SERVER-81039): Remove once third_party libs can be compiled from the root directory.
ignored_paths = []
with open(os.path.join(mongo_dir, ".bazelignore"), "r") as file:
with open(os.path.join(os.curdir, ".bazelignore"), "r", encoding="utf-8") as file:
for line in file.readlines():
contents = line.split("#")[0].strip()
if contents:
@ -43,8 +40,9 @@ def find_all_failed(bin_path: str) -> list[str]:
def lint_all(bin_path: str, generate_report: bool):
files = find_all_failed(bin_path)
lint(bin_path, files, generate_report)
result = lint(bin_path, files, generate_report)
validate_bazel_groups(generate_report=generate_report, fix=False, quick=False)
return result
def fix_all(bin_path: str):
@ -60,6 +58,7 @@ def fix_unittests(bin_path: str):
def lint(bin_path: str, files: list[str], generate_report: bool):
found_errors = False
for file in files:
process = subprocess.run(
[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):
diff = process.stdout
print(f"{file} has linting errors")
print(diff)
found_errors = True
if generate_report:
header = (
@ -91,6 +91,7 @@ def lint(bin_path: str, files: list[str], generate_report: bool):
put_report(report)
print("Done linting files")
return not found_errors
def fix(bin_path: str, files: list[str]):
@ -101,6 +102,14 @@ def fix(bin_path: str, files: list[str]):
def main():
default_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY")
if not default_dir:
print("This script must be run though bazel. Please run 'bazel run //:format' instead")
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
print("python buildscripts/install_bazel.py")
return 1
os.chdir(default_dir)
parser = argparse.ArgumentParser(description="buildifier wrapper")
parser.add_argument(
"--binary-dir",
@ -139,9 +148,6 @@ def main():
lint_parser.set_defaults(subcommand="fix")
args = parser.parse_args()
assert os.path.abspath(os.curdir) == str(
mongo_dir.absolute()
), "buildifier.py must be run from the root of the mongo repo"
binary_name = "buildifier.exe" if platform.system() == "Windows" else "buildifier"
if args.binary_dir:
binary_path = os.path.join(args.binary_dir, binary_name)

View File

@ -10,7 +10,7 @@ import time
import urllib.request
from typing import Dict, List
from simple_report import make_report, put_report, try_combine_reports
from buildscripts.simple_report import make_report, put_report, try_combine_reports
sys.path.append(".")

View File

@ -52,6 +52,15 @@ elif [[ $ARCH == "s390x" ]]; then
export JAVA_HOME="/usr/lib/jvm/java-21-openjdk"
fi
# AL2 stores certs in a nonstandard location
if [[ -f /etc/os-release ]]; then
DISTRO=$(awk -F '[="]*' '/^PRETTY_NAME/ { print $2 }' < /etc/os-release)
if [[ $DISTRO == "Amazon Linux 2" ]]; then
export SSL_CERT_DIR=/etc/pki/tls/certs
export SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
fi
fi
# Print command being run to file that can be uploaded
echo "python buildscripts/install_bazel.py" > bazel-invocation.txt
echo "bazel run --verbose_failures $LOCAL_ARG ${args} ${target}" >> bazel-invocation.txt