From 9f5782b67b4d62b410d2a1002c44983b260de55c Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Wed, 24 Apr 2024 15:49:48 +0200 Subject: [PATCH] Bazel: introduce buildifier formatting This introduces tooling and enforcement for formatting bazel files. The tooling is provided as a bazel run target from [keith/buildifier-prebuilt](https://github.com/keith/buildifier-prebuilt). This is used in a [`pre-commit`](https://pre-commit.com/) hook for those having that installed. In turn this is used in a CI check. Relying on a `pre-commit` action gives us easy checking that buildifying did not change anything in the files and printing the diff, without having to hand-roll the check ourselves. This enforcement will make usage of gazelle easier, as gazelle itself might reformat files, even outside of `go`. Having them properly formatted will allow gazelle to leave them unchanged, without needing to configure awkward exclude directives. --- .github/workflows/buildifier.yml | 26 +++++++++++++++++++++ .pre-commit-config.yaml | 12 ++++++---- BUILD.bazel | 9 +++++++ MODULE.bazel | 2 ++ javascript/BUILD.bazel | 2 +- javascript/extractor/BUILD.bazel | 2 +- misc/codegen/BUILD.bazel | 2 -- misc/codegen/generators/BUILD.bazel | 2 -- python/extractor/tsg-python/tsp/BUILD.bazel | 2 +- swift/extractor/BUILD.bazel | 10 ++++---- swift/logging/BUILD.bazel | 2 +- 11 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/buildifier.yml diff --git a/.github/workflows/buildifier.yml b/.github/workflows/buildifier.yml new file mode 100644 index 00000000000..d5cea415d25 --- /dev/null +++ b/.github/workflows/buildifier.yml @@ -0,0 +1,26 @@ +name: Check bazel formatting + +on: + pull_request: + paths: + - "**.bazel" + - "**.bzl" + branches: + - main + - "rc/*" + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507 + with: + extra_args: buildifier --all-files --show-diff-on-failure + - if: failure() + run: | + echo "In order to format all files, please run:" + echo " bazel run //:buildifier" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 383bc110383..7fddd1929d4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,13 +20,15 @@ repos: - id: autopep8 files: ^misc/codegen/.*\.py - - repo: https://github.com/warchant/pre-commit-buildifier - rev: 0.0.2 - hooks: - - id: buildifier - - repo: local hooks: + - id: buildifier + name: Format bazel files + files: \.(bazel|bzl) + language: system + entry: bazel run //:buildifier + pass_filenames: false + - id: codeql-format name: Fix QL file formatting files: \.qll?$ diff --git a/BUILD.bazel b/BUILD.bazel index e69de29bb2d..3ccdcda5f12 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -0,0 +1,9 @@ +load("@buildifier_prebuilt//:rules.bzl", "buildifier") + +buildifier( + name = "buildifier", + exclude_patterns = [ + "./.git/*", + ], + lint_mode = "fix", +) diff --git a/MODULE.bazel b/MODULE.bazel index 4e1fe0d9f7c..c834acab7ae 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -22,6 +22,8 @@ bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "absl") bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json") bazel_dep(name = "fmt", version = "10.0.0") +bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True) + pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") pip.parse( hub_name = "codegen_deps", diff --git a/javascript/BUILD.bazel b/javascript/BUILD.bazel index eacfa554a8e..18315a09813 100644 --- a/javascript/BUILD.bazel +++ b/javascript/BUILD.bazel @@ -1,5 +1,5 @@ -load("@semmle_code//:dist.bzl", "dist") load("@rules_pkg//pkg:mappings.bzl", "pkg_files") +load("@semmle_code//:dist.bzl", "dist") load("@semmle_code//buildutils-internal:zipmerge.bzl", "zipmerge") package(default_visibility = ["//visibility:public"]) diff --git a/javascript/extractor/BUILD.bazel b/javascript/extractor/BUILD.bazel index 6793287c22f..360dc9370f9 100644 --- a/javascript/extractor/BUILD.bazel +++ b/javascript/extractor/BUILD.bazel @@ -1,5 +1,5 @@ -load("@semmle_code//:common.bzl", "codeql_fat_jar", "codeql_java_project") load("@rules_pkg//pkg:mappings.bzl", "pkg_files") +load("@semmle_code//:common.bzl", "codeql_fat_jar", "codeql_java_project") java_library( name = "deps", diff --git a/misc/codegen/BUILD.bazel b/misc/codegen/BUILD.bazel index 102e52fb10b..52a5c001134 100644 --- a/misc/codegen/BUILD.bazel +++ b/misc/codegen/BUILD.bazel @@ -1,5 +1,3 @@ -load("@codegen_deps//:requirements.bzl", "requirement") - py_binary( name = "codegen", srcs = ["codegen.py"], diff --git a/misc/codegen/generators/BUILD.bazel b/misc/codegen/generators/BUILD.bazel index df328a7df7f..f731c42ce23 100644 --- a/misc/codegen/generators/BUILD.bazel +++ b/misc/codegen/generators/BUILD.bazel @@ -1,5 +1,3 @@ -load("@codegen_deps//:requirements.bzl", "requirement") - py_library( name = "generators", srcs = glob(["*.py"]), diff --git a/python/extractor/tsg-python/tsp/BUILD.bazel b/python/extractor/tsg-python/tsp/BUILD.bazel index e3389fce1cc..1f7ec006c65 100644 --- a/python/extractor/tsg-python/tsp/BUILD.bazel +++ b/python/extractor/tsg-python/tsp/BUILD.bazel @@ -1,6 +1,6 @@ +load("@py_deps//:defs.bzl", "aliases", "all_crate_deps") load("@rules_rust//cargo:defs.bzl", "cargo_build_script") load("@rules_rust//rust:defs.bzl", "rust_library") -load("@py_deps//:defs.bzl", "aliases", "all_crate_deps") package(default_visibility = ["//visibility:public"]) diff --git a/swift/extractor/BUILD.bazel b/swift/extractor/BUILD.bazel index 42c1105053c..3acdbf014e3 100644 --- a/swift/extractor/BUILD.bazel +++ b/swift/extractor/BUILD.bazel @@ -1,5 +1,5 @@ -load("//swift:rules.bzl", "swift_cc_binary") load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles") +load("//swift:rules.bzl", "swift_cc_binary") swift_cc_binary( name = "extractor.real", @@ -7,6 +7,10 @@ swift_cc_binary( "*.h", "*.cpp", ]), + linkopts = select({ + "@platforms//os:macos": ["-headerpad_max_install_names"], + "//conditions:default": [], + }), visibility = ["//swift:__pkg__"], deps = [ "//swift/extractor/config", @@ -17,10 +21,6 @@ swift_cc_binary( "//swift/third_party/swift-llvm-support", "@absl//absl/strings", ], - linkopts = select({ - "@platforms//os:macos": ["-headerpad_max_install_names"], - "//conditions:default": [], - }), ) sh_binary( diff --git a/swift/logging/BUILD.bazel b/swift/logging/BUILD.bazel index cd2f3344166..1d6192b3c13 100644 --- a/swift/logging/BUILD.bazel +++ b/swift/logging/BUILD.bazel @@ -4,10 +4,10 @@ cc_library( hdrs = glob(["*.h"]), visibility = ["//visibility:public"], deps = [ + "//shared/cpp:extractor_shared", "@absl//absl/strings", "@binlog", "@fmt", "@json", - "//shared/cpp:extractor_shared", ], )