Skip to content

Commit a43314d

Browse files
alexeaglefhanau
authored andcommitted
feat: get clang-format from rules_lint
This provides a shorter and more standard way to invoke formatters for all languages. rules_lint is widely adopted and has many companies contributing. Subset of #5227 to rollout in smaller steps code review feedback add prettier
1 parent 6b02637 commit a43314d

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_library")
12
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
23
load("@bazel_skylib//lib:selects.bzl", "selects")
34
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
@@ -24,6 +25,14 @@ alias(
2425
visibility = ["//visibility:public"],
2526
)
2627

28+
# For convenience, less typing than the full label:
29+
# (assuming working directory is the root of the repository)
30+
# bazel run format
31+
alias(
32+
name = "format",
33+
actual = "//build/deps/formatters:format",
34+
)
35+
2736
npm_link_all_packages(name = "node_modules")
2837

2938
npm_link_package(
@@ -44,6 +53,12 @@ capnp_es_bins.capnpc_ts_binary(
4453
visibility = ["//visibility:public"],
4554
)
4655

56+
js_library(
57+
name = "prettierrc",
58+
srcs = [":.prettierrc.json"],
59+
visibility = ["//build/deps/formatters:__pkg__"],
60+
)
61+
4762
# Platform definition for using clang-cl on Windows
4863
platform(
4964
name = "x64_windows-clang-cl",

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module(name = "workerd")
44

55
bazel_dep(name = "aspect_bazel_lib", version = "2.21.2")
6+
bazel_dep(name = "aspect_rules_lint", version = "1.10.2")
67
bazel_dep(name = "bazel_features", version = "1.37.0")
78
bazel_dep(name = "bazel_skylib", version = "1.8.2")
89
bazel_dep(name = "rules_shell", version = "0.6.1")

build/deps/formatters/BUILD

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
1+
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
2+
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
3+
load("@npm//:prettier/package_json.bzl", prettier = "bin")
24

35
config_setting(
46
name = "linux_amd64",
@@ -40,7 +42,7 @@ config_setting(
4042
],
4143
)
4244

43-
copy_file(
45+
native_binary(
4446
name = "clang-format@rule",
4547
src = select({
4648
":linux_amd64": "@clang-format-linux-amd64//file:file",
@@ -52,7 +54,7 @@ copy_file(
5254
visibility = ["//visibility:public"],
5355
)
5456

55-
copy_file(
57+
native_binary(
5658
name = "ruff@rule",
5759
src = select({
5860
":linux_amd64": "@ruff-linux-amd64//:file",
@@ -64,7 +66,7 @@ copy_file(
6466
visibility = ["//visibility:public"],
6567
)
6668

67-
copy_file(
69+
native_binary(
6870
name = "buildifier@rule",
6971
src = select({
7072
":linux_amd64": "@buildifier-linux-amd64//file:file",
@@ -77,3 +79,27 @@ copy_file(
7779
out = "buildifier",
7880
visibility = ["//visibility:public"],
7981
)
82+
83+
prettier.prettier_binary(
84+
name = "prettier@rule",
85+
# Include this js_library and its dependencies in the runfiles (runtime dependencies)
86+
data = ["//:prettierrc"],
87+
# Allow the binary to be run outside bazel
88+
env = {"BAZEL_BINDIR": "."},
89+
fixed_args = [
90+
# `require` statements in the config file will be resolved relative to its location
91+
# Therefore to make it hermetic, prettier must be pointed at the copy of the config file
92+
# in the runfiles folder rather than the one in the source folder.
93+
"--config=\"$$JS_BINARY__RUNFILES\"/$(rlocationpath //:prettierrc)",
94+
"--log-level=warn",
95+
],
96+
)
97+
98+
format_multirun(
99+
name = "format",
100+
cc = ":clang-format@rule",
101+
javascript = ":prettier@rule",
102+
python = ":ruff@rule",
103+
starlark = ":buildifier@rule",
104+
visibility = ["//:__pkg__"],
105+
)

0 commit comments

Comments
 (0)