Skip to content

Commit d43d716

Browse files
authored
Merge pull request #9811 from alokkumardalei-wq/feature/bazelisk-run-tidy
Bazel: Add unified hermetic:tidy target for workspace formatting
2 parents 2939447 + 8d652ae commit d43d716

9 files changed

Lines changed: 109 additions & 5 deletions

File tree

BUILD.bazel

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,52 @@ sh_binary(
504504
],
505505
)
506506

507+
# --- Bazel linting (buildifier -lint=warn) --------------------------------
508+
509+
# buildifier: disable=native-sh-test
510+
sh_test(
511+
name = "lint_bzl_test",
512+
srcs = ["//bazel:bzl_lint_test.sh"],
513+
args = ["$(rootpath @buildifier_prebuilt//:buildifier)"],
514+
data = [
515+
"@buildifier_prebuilt//:buildifier",
516+
],
517+
tags = ["local"],
518+
)
519+
520+
# --- Bazel formatting check (buildifier -mode=check -lint=off) ------------
521+
522+
# buildifier: disable=native-sh-test
523+
sh_test(
524+
name = "fmt_bzl_test",
525+
srcs = ["//bazel:bzl_fmt_test.sh"],
526+
args = ["$(rootpath @buildifier_prebuilt//:buildifier)"],
527+
data = [
528+
"@buildifier_prebuilt//:buildifier",
529+
],
530+
tags = ["local"],
531+
)
532+
533+
# --- Bazel auto-format only (buildifier -mode=fix) ------------------------
534+
535+
# buildifier: disable=native-sh-binary
536+
sh_binary(
537+
name = "tidy_bzl",
538+
srcs = ["//bazel:bzl_tidy.sh"],
539+
args = ["$(rootpath @buildifier_prebuilt//:buildifier)"],
540+
data = [
541+
"@buildifier_prebuilt//:buildifier",
542+
],
543+
)
544+
507545
# --- Umbrella targets ------------------------------------------------------
508546

509547
test_suite(
510548
name = "lint_test",
511549
tests = [
550+
":fmt_bzl_test",
512551
":fmt_tcl_test",
552+
":lint_bzl_test",
513553
":lint_tcl_test",
514554
],
515555
)
@@ -525,12 +565,19 @@ sh_binary(
525565
"$(rootpath //bazel:tclfmt)",
526566
"$(rootpath //bazel:tcl_lint_test.sh)",
527567
"$(rootpath //bazel:tclint)",
568+
"$(rootpath //bazel:bzl_tidy.sh)",
569+
"$(rootpath @buildifier_prebuilt//:buildifier)",
570+
"$(rootpath //bazel:bzl_lint_test.sh)",
571+
"$(rootpath @buildifier_prebuilt//:buildifier)",
528572
],
529573
data = [
530574
"tclint.toml",
575+
"//bazel:bzl_lint_test.sh",
576+
"//bazel:bzl_tidy.sh",
531577
"//bazel:tcl_lint_test.sh",
532578
"//bazel:tcl_tidy.sh",
533579
"//bazel:tclfmt",
534580
"//bazel:tclint",
581+
"@buildifier_prebuilt//:buildifier",
535582
],
536583
)

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ bazel_dep(name = "toolchains_llvm", version = "1.5.0", dev_dependency = True)
9494
# --- Dev dependencies (not propagated to downstream consumers) ---
9595

9696
bazel_dep(name = "bant", version = "0.2.4", dev_dependency = True)
97+
bazel_dep(name = "buildifier_prebuilt", version = "8.5.1.2", dev_dependency = True)
9798
bazel_dep(name = "googletest", version = "1.17.0.bcr.2", dev_dependency = True)
9899
bazel_dep(name = "rules_verilator", version = "0.1.0", dev_dependency = True)
99100
bazel_dep(name = "verilator", version = "5.036.bcr.3", dev_dependency = True)

MODULE.bazel.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ package(features = ["layering_check"])
1212

1313
exports_files(
1414
[
15+
"bzl_fmt_test.sh",
16+
"bzl_lint_test.sh",
17+
"bzl_tidy.sh",
1518
"fix_lint.sh",
1619
"install.sh",
17-
"tcl_lint_test.sh",
1820
"tcl_fmt_test.sh",
21+
"tcl_lint_test.sh",
1922
"tcl_tidy.sh",
2023
],
2124
visibility = ["//visibility:public"],

bazel/bzl_fmt_test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright (c) 2025-2026, The OpenROAD Authors
4+
#
5+
# Check that all Bazel files are properly formatted (no lint warnings).
6+
set -euo pipefail
7+
TOOL="$(readlink -f "$1")"
8+
WORKSPACE="$(dirname "$(readlink -f MODULE.bazel)")"
9+
cd "$WORKSPACE"
10+
# `git ls-files` skips submodule contents (src/sta, third-party/abc).
11+
# Explicit -mode=check -lint=off separates format errors from lint warnings,
12+
# and overrides the repo-root .buildifier.json default (mode: fix).
13+
git ls-files '*.bazel' '*.bzl' '**/BUILD' 'BUILD' '**/WORKSPACE' 'WORKSPACE' -z \
14+
| xargs -0 "$TOOL" -mode=check -lint=off

bazel/bzl_lint_test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright (c) 2025-2026, The OpenROAD Authors
4+
#
5+
# Lint all Bazel files using buildifier (check-only, with lint warnings).
6+
set -euo pipefail
7+
TOOL="$(readlink -f "$1")"
8+
WORKSPACE="$(dirname "$(readlink -f MODULE.bazel)")"
9+
cd "$WORKSPACE"
10+
# `git ls-files` skips submodule contents (src/sta, third-party/abc), so
11+
# we never try to reformat files owned by another repo.
12+
# Explicit -mode=check overrides the repo-root .buildifier.json default.
13+
git ls-files '*.bazel' '*.bzl' '**/BUILD' 'BUILD' '**/WORKSPACE' 'WORKSPACE' -z \
14+
| xargs -0 "$TOOL" -mode=check -lint=warn

bazel/bzl_tidy.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright (c) 2025-2026, The OpenROAD Authors
4+
#
5+
# Auto-format all Bazel files in-place using buildifier.
6+
set -euo pipefail
7+
TOOL="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
8+
cd "${BUILD_WORKSPACE_DIRECTORY:-$PWD}"
9+
# `git ls-files` skips submodule contents (src/sta, third-party/abc),
10+
# so we never rewrite files owned by another repo.
11+
git ls-files '*.bazel' '*.bzl' '**/BUILD' 'BUILD' '**/WORKSPACE' 'WORKSPACE' -z \
12+
| xargs -0 "$TOOL" -mode=fix -lint=fix

bazel/fix_lint.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env bash
22
# SPDX-License-Identifier: BSD-3-Clause
3-
# Copyright (c) 2025-2025, The OpenROAD Authors
3+
# Copyright (c) 2025-2026, The OpenROAD Authors
44
#
5-
# Auto-fix then lint. Delegates to tcl_tidy.sh and tcl_lint_test.sh
6-
# so that file-discovery logic is not duplicated (DRY).
5+
# Auto-fix then lint. Delegates to per-language tidy/lint scripts so
6+
# that file-discovery logic is not duplicated (DRY).
77
set -euo pipefail
88

99
export BUILD_WORKSPACE_DIRECTORY="${BUILD_WORKSPACE_DIRECTORY:-$PWD}"
@@ -12,6 +12,10 @@ export BUILD_WORKSPACE_DIRECTORY="${BUILD_WORKSPACE_DIRECTORY:-$PWD}"
1212
"$1" "$2"
1313
"$3" "$4" || rc=$?
1414

15+
# Bazel: auto-format then lint
16+
"$5" "$6"
17+
"$7" "$8" || rc=$?
18+
1519
git -C "$BUILD_WORKSPACE_DIRECTORY" status
1620

1721
if [ "${rc:-0}" -ne 0 ]; then

docs/contrib/LintTargets.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ the two umbrella targets.
6161
| `//:lint_tcl_test` | `sh_test` | Runs `tclint .` (lint rules) |
6262
| `//:fmt_tcl_test` | `sh_test` | Runs `tclfmt --check .` (formatting) |
6363
| `//:tidy_tcl` | `sh_binary` | Runs `tclfmt --in-place .` (auto-format) |
64+
| `//:lint_bzl_test` | `sh_test` | Runs `buildifier -mode=check -lint=warn` (lint rules) |
65+
| `//:fmt_bzl_test` | `sh_test` | Runs `buildifier -mode=check -lint=off` (formatting) |
66+
| `//:tidy_bzl` | `sh_binary` | Runs `buildifier -mode=fix -lint=fix` (auto-format) |
6467

6568
## Configuration
6669

6770
TCL linting and formatting are controlled by `tclint.toml` at the repository
6871
root. See the [tclint documentation](https://tclint.readthedocs.io/) for
6972
available options.
7073

74+
Bazel formatting and linting are controlled by `.buildifier.json` at the
75+
repository root. The test scripts (`bzl_lint_test.sh`, `bzl_fmt_test.sh`)
76+
explicitly pass `-mode=check` to override the default `mode: fix` in that
77+
config, ensuring they remain read-only checks.
78+
7179
## POLA
7280

7381
`//:fix_lint` follows the
@@ -98,7 +106,6 @@ The following linters and formatters are planned for `//:lint_test` and
98106
- **C++ clang-tidy** — static analysis for C++ sources
99107
- **C++ clang-format** — formatting check/fix for C++ and header files
100108
- **Python ruff** — lint + format for Python scripts in `etc/`, `docs/`, tests
101-
- **Buildifier** — lint + format for BUILD, .bzl, and MODULE.bazel files
102109
- **ShellCheck** — lint for bash scripts in `test/`, `bazel/`, `etc/`
103110
- **Duplicate message ID check** — replace Jenkins "Find Duplicated Message IDs" stage
104111
- **Doc consistency checks** — replace Jenkins "Documentation Checks" stage

0 commit comments

Comments
 (0)