-
Notifications
You must be signed in to change notification settings - Fork 27
Open
Labels
Description
Motivation
To ensure a consistent Python code style across the project and automate a manual, error-prone process, we should integrate the black code formatter directly into our Bazel build system. We already use black in our CI pipeline, but don't expose it for local usage.
Below is a proposed approach, but we're open to do thing this other ways.
Proposed Solution
We will implement two separate Bazel targets to achieve this:
-
A
bazel runTarget for Fixing:- A target named
black_fixwill be created. - Running
bazel run //:black_fix -- .will executeblackon the entire workspace, reformatting any non-compliant Python files in-place.
- A target named
-
A
bazel testTarget for Checking (Optional):- A target named
black_checkwill be created. - Running
bazel test //:black_check(or as part ofbazel test //...) will executeblack --check --diff. - This test will pass if all files are correctly formatted and fail otherwise, printing a diff of the required changes. This is ideal for CI integration.
- A target named
Implementation Plan
-
Add
blackas a Bazel Dependency:- Modify
MODULE.bazelto use thepip.parseextension fromrules_python. - Create a
requirements.txtfile to declare a dependency onblack==24.8.0and its necessary transitive dependencies.
- Modify
-
Create a
py_binaryfor the Formatter:- In the root
BUILDfile, define apy_binarytarget namedblack_binary. - This target will use the
entry_pointattribute set to"black"to directly use theblackcommand-line tool provided by the pip package.
- In the root
-
Implement the "Fix" Command:
- Create a
sh_binarynamedblack_fixthat simply calls theblack_binarytarget with the provided arguments (e.g.,.to format the whole project). This will be the user-facing command run withbazel run.
- Create a
-
Implement the "Check" Test:
- Create a
sh_testnamedblack_check. - This test will execute the
black_binarytarget with the--checkand--diffflags. The test will succeed only ifblackreturns a zero exit code (indicating no changes are needed).
- Create a
Reactions are currently unavailable