SERVER-101035 Set up rules_lint formatter (#32543)

GitOrigin-RevId: 3be2f4b329b4559632bea9d56cd7232ab11f7961
This commit is contained in:
Zack Winter 2025-02-19 11:46:14 -08:00 committed by MongoDB Bot
parent d8d59cfedb
commit b4a21ab28f
10 changed files with 156 additions and 15 deletions

2
.gitattributes vendored
View File

@ -1,3 +1,5 @@
*.vcproj -crlf -diff -merge
*.pbxproj -crlf -diff -merge
*.sh text eol=lf
**/third_party/**/* rules-lint-ignored=true
external rules-lint-ignored=true

View File

@ -5,13 +5,16 @@ load("//bazel/toolchains:mongo_toolchain.bzl", "setup_mongo_toolchain_aliases")
load("//bazel/config:render_template.bzl", "render_template")
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin")
load("//bazel:mongo_js_rules.bzl", "mongo_js_library")
load("@npm//:prettier/package_json.bzl", prettier = "bin")
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_rules_lint//format:defs.bzl", "format_multirun", "format_test")
package(
default_visibility = ["//visibility:public"],
)
exports_files([
".prettierrc",
"pyproject.toml",
"poetry.lock",
"symbols.orderfile",
@ -43,6 +46,28 @@ eslint_bin.eslint_binary(
],
)
prettier.prettier_binary(
name = "prettier",
# Include this js_library and its dependencies in the runfiles (runtime dependencies)
data = ["//:.prettierrc"],
# Allow the binary to be run outside bazel
# See more details about this by commenting this out and running `bazel run //:format`
env = {"BAZEL_BINDIR": "."},
fixed_args = [
# `require` statements in the config file will be resolved relative to its location
# Therefore to make it hermetic, prettier must be pointed at the copy of the config file
# in the runfiles folder rather than the one in the source folder.
"--config=\"$$JS_BINARY__RUNFILES\"/$(rlocationpath //:.prettierrc)",
# default log level is "log" which spams on success
# https://prettier.io/docs/en/cli.html#--log-level
# NB: prettier 2 names this loglevel, in prettier 3 it's renamed log-level, see
# https://prettier.io/blog/2023/07/05/3.0.0.html#cli-1
"--log-level=warn",
# Speed this up a little bit with caching
"--cache",
],
)
alias(
name = "format",
actual = "//bazel/format",

View File

@ -1,27 +1,19 @@
load("@npm//:prettier/package_json.bzl", prettier = "bin")
# TODO: SERVER-82329 eslint binary should almost exactly mirror prettier binary
# To update prettier change the version in package.json
# Run `pnpm install`
# Commit changes
prettier.prettier_binary(
name = "prettier",
# Allow the binary to be run outside bazel
# See more details about this by commenting this out and running `bazel run //:format`
env = {"BAZEL_BINDIR": "."},
)
load("@aspect_rules_lint//format:defs.bzl", "format_multirun", "format_test")
py_binary(
name = "format",
srcs = ["format.py"],
args = [
"--prettier",
"$(location :prettier)",
"$(location //:prettier)",
"--shellscripts-linters",
"$(location //buildscripts:shellscripts_linters)",
"--rules-lint-format",
"$(location :rules_lint_format)",
],
data = [
":prettier",
":rules_lint_format",
"//:prettier",
"//buildscripts:shellscripts_linters",
"@shfmt",
],
@ -34,3 +26,20 @@ py_binary(
"//buildscripts:buildifier",
],
)
format_multirun(
name = "rules_lint_format",
css = "//:prettier",
graphql = "//:prettier",
html = "//:prettier",
markdown = "//:prettier",
sql = "//:prettier",
visibility = ["//visibility:public"],
# TODO(SERVER-101031): Use rules_lint buildifier formatter once set up
# starlark = "@buildifier_prebuilt//:buildifier",
# TODO(SERVER-101033): Enable clang_format once a way to only format a user's changeset is set up
# cc = "//:clang_format",
# c = "//:clang_format",
# TODO(SERVER-101034): Enable rules_lint shfmt after sh files are reformatted with .editorconfig
# shell = "@shfmt//:shfmt",
)

View File

@ -7,6 +7,22 @@ from buildscripts import download_buildifier
from buildscripts.buildifier import fix_all, lint_all
def run_rules_lint(rules_lint_format_path: pathlib.Path, check: bool) -> bool:
try:
command = [str(rules_lint_format_path)]
env = os.environ
if check:
print("Running rules_lint formatter in check mode")
env["mode"] = "check"
else:
print("Running rules_lint formatter")
repo_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
subprocess.run(command, check=True, env=env, cwd=repo_path)
except subprocess.CalledProcessError:
return False
return True
def run_shellscripts_linters(shellscripts_linters: pathlib.Path, check: bool) -> bool:
try:
command = [str(shellscripts_linters)]
@ -103,6 +119,12 @@ def main() -> int:
required=True,
type=pathlib.Path,
)
parser.add_argument(
"--rules-lint-format",
help="Set the path to rules_lint's formatter",
required=True,
type=pathlib.Path,
)
args = parser.parse_args()
prettier_path: pathlib.Path = args.prettier.resolve()
@ -112,7 +134,8 @@ def main() -> int:
return (
0
if run_shellscripts_linters(shellscripts_linters_path, args.check)
if run_rules_lint(args.rules_lint_format, args.check)
and run_shellscripts_linters(shellscripts_linters_path, args.check)
and run_prettier(prettier_path, args.check)
and run_buildifier(args.check)
else 1

View File

@ -175,3 +175,10 @@ filegroup(
"{version}/bin/llvm-symbolizer",
],
)
filegroup(
name = "clang_format",
srcs = [
"{version}/bin/clang-format",
],
)

View File

@ -161,6 +161,7 @@ def setup_mongo_toolchain_aliases():
# Map from target's name inside the toolchain to the name we want to alias it to.
toolchain_targets = {
"llvm_symbolizer": "llvm_symbolizer",
"clang_format": "clang_format",
"clang_tidy": "clang_tidy",
"mongo_toolchain": "mongo_toolchain",
"all_files": "toolchain_files",

View File

@ -0,0 +1,17 @@
package(default_visibility = ["//visibility:public"])
py_library(
name = "wrapper_hook",
srcs = [
"autogenerated_targets.py",
"compiledb.py",
"developer_bes_keywords.py",
"engflow_check.py",
"install_modules.py",
"lint.py",
"plus_interface.py",
"wrapper_debug.py",
"wrapper_hook.py",
],
visibility = ["//visibility:public"],
)

View File

@ -147,6 +147,12 @@ def run_rules_lint(bazel_bin, 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)

View File

@ -228,9 +228,58 @@ py_library(
],
)
py_binary(
name = "sbom_linter",
srcs = [
"sbom_linter.py",
],
visibility = ["//visibility:public"],
deps = [
dependency(
"jsonschema",
group = "build-metrics",
),
],
)
bin.eslint_binary(
name = "eslint_binary",
# Allow the binary to be run outside bazel
# See more details about this by commenting this out and running `bazel run //:format`
env = {"BAZEL_BINDIR": "."},
)
sh_binary(
name = "auto_install_db_contrib_tool",
srcs = ["auto_install_db_contrib_tool.sh"],
)
sh_binary(
name = "setup_engflow_creds",
srcs = ["setup_engflow_creds.sh"],
)
sh_binary(
name = "consolidate_repos",
srcs = ["consolidate-repos.sh"],
)
sh_binary(
name = "consolidate_repos_enterprise",
srcs = ["consolidate-repos-enterprise.sh"],
)
sh_binary(
name = "poetry_sync",
srcs = ["poetry_sync.sh"],
)
sh_binary(
name = "setup_node_env",
srcs = ["setup_node_env.sh"],
)
sh_binary(
name = "mount_drives",
srcs = ["mount_drives.sh"],
)

View File

@ -227,6 +227,8 @@ def lint_sbom(
def main() -> int:
os.chdir(os.environ.get("BUILD_WORKSPACE_DIRECTORY", "."))
parser = argparse.ArgumentParser()
parser.add_argument(
"--format",