Skip to content

Integrate black formatter with Bazel for local development#110

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/integrate-black-formatter-bazel
Draft

Integrate black formatter with Bazel for local development#110
Copilot wants to merge 3 commits intomasterfrom
copilot/integrate-black-formatter-bazel

Conversation

Copy link

Copilot AI commented Dec 6, 2025

Black is already used in CI but not exposed for local usage. This adds Bazel targets so developers can format and check Python code locally using the same version (24.8.0) as CI.

Changes

  • requirements.txt: Add black==24.8.0 dependency
  • MODULE.bazel: Configure pip.parse extension to pull pip dependencies via bzlmod
  • BUILD: Add :black, :black_fix, and :black_check targets
  • scripts/black_*.sh: Shell wrappers that locate the black binary in Bazel runfiles and run it against the workspace

Usage

# Format all Python files
bazel run //:black_fix -- .

# Check formatting (non-zero exit if files need formatting)
bazel run //:black_check

# Run black with custom args
bazel run //:black -- --check compiler/

The targets use BUILD_WORKSPACE_DIRECTORY to operate on source files rather than the Bazel sandbox.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.googleapis.com
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 version (dns block)
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 --version (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Integrate 'black' Formatter with Bazel for Code Formatting</issue_title> <issue_description>**This issue was copied from the upstream repository** google/emboss`.

Original issue: google#226
Original state: open, created at: 2025-07-08T18:02:14Z, updated at: 2025-07-12T22:14:47Z, by @EricRahm


Original 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:

  1. A bazel run Target for Fixing:

    • A target named black_fix will be created.
    • Running bazel run //:black_fix -- . will execute black on the entire workspace, reformatting any non-compliant Python files in-place.
  2. A bazel test Target for Checking (Optional):

    • A target named black_check will be created.
    • Running bazel test //:black_check (or as part of bazel test //...) will execute black --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.

Implementation Plan

  1. Add black as a Bazel Dependency:

    • Modify MODULE.bazel to use the pip.parse extension from rules_python.
    • Create a requirements.txt file to declare a dependency on black==24.8.0 and its necessary transitive dependencies.
  2. Create a py_binary for the Formatter:

    • In the root BUILD file, define a py_binary target named black_binary.
    • This target will use the entry_point attribute set to "black" to directly use the black command-line tool provided by the pip package.
  3. Implement the "Fix" Command:

    • Create a sh_binary named black_fix that simply calls the black_binary target with the provided arguments (e.g., . to format the whole project). This will be the user-facing command run with bazel run.
  4. Implement the "Check" Test:

    • Create a sh_test named black_check.
    • This test will execute the black_binary target with the --check and --diff flags. The test will succeed only if black returns a zero exit code (indicating no changes are needed).

Discussion (copied comments)

Comment by @EricRahm on 2025-07-08T18:05:21Z:

I did a cursory search and didn't find a black bzlmod, but I might not have searched hard enough.


</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 6, 2025 03:00
- Add requirements.txt with black==24.8.0
- Update MODULE.bazel to use pip.parse extension for Python dependencies
- Add black, black_fix, and black_check targets to root BUILD file
- Create shell scripts for black_fix and black_check commands

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
- Create black_common.sh with shared find_black function
- Update black_fix.sh and black_check.sh to use shared function
- Improve error messages to show all searched locations
- Update BUILD to include black_common.sh as data dependency

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Copilot AI changed the title [WIP] Integrate black formatter with Bazel for code formatting Integrate black formatter with Bazel for local development Dec 6, 2025
Copilot AI requested a review from AaronWebster December 6, 2025 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrate 'black' Formatter with Bazel for Code Formatting`

2 participants