From 6da044154c3ec60beeb8b87f43d514d8b54b63dd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Dec 2025 02:52:36 +0000 Subject: [PATCH 1/3] Initial plan From 2b0ed1083b9d1f3d491c4eb0fb7e3f3022917b73 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Dec 2025 03:00:21 +0000 Subject: [PATCH 2/3] Integrate black formatter with Bazel - 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> --- BUILD | 29 ++++++++++++++++++++++++ MODULE.bazel | 8 +++++++ requirements.txt | 1 + scripts/black_check.sh | 50 ++++++++++++++++++++++++++++++++++++++++++ scripts/black_fix.sh | 49 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 requirements.txt create mode 100755 scripts/black_check.sh create mode 100755 scripts/black_fix.sh diff --git a/BUILD b/BUILD index 4642551..8d5dfa6 100644 --- a/BUILD +++ b/BUILD @@ -12,7 +12,36 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@pip//:requirements.bzl", "entry_point") + exports_files([ "build_defs.bzl", "LICENSE", + "requirements.txt", ]) + +# Black formatter binary. Use with: +# bazel run //:black -- [args] +# Example to format all Python files: +# bazel run //:black -- . +alias( + name = "black", + actual = entry_point("black"), +) + +# Target to fix Python formatting issues. +# Usage: bazel run //:black_fix -- . +sh_binary( + name = "black_fix", + srcs = ["scripts/black_fix.sh"], + data = [":black"], +) + +# Target to check Python formatting. +# Usage: bazel run //:black_check +# This runs black --check --diff . on the workspace. +sh_binary( + name = "black_check", + srcs = ["scripts/black_check.sh"], + data = [":black"], +) diff --git a/MODULE.bazel b/MODULE.bazel index f5d06a2..129ac04 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -15,3 +15,11 @@ bazel_dep( repo_name = "com_google_googletest", ) bazel_dep(name = "rules_python", version = "0.31.0") + +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") +pip.parse( + hub_name = "pip", + python_version = "3.11", + requirements_lock = "//:requirements.txt", +) +use_repo(pip, "pip") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6876f3c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +black==24.8.0 diff --git a/scripts/black_check.sh b/scripts/black_check.sh new file mode 100755 index 0000000..ebe0228 --- /dev/null +++ b/scripts/black_check.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Script to check Python formatting with black. +# Usage: bazel run //:black_check +# +# This will pass if all files are correctly formatted and fail otherwise, +# printing a diff of the required changes. + +set -e + +# Find the black binary in the runfiles +SCRIPT_DIR="$(dirname "$0")" +BLACK_BIN="$SCRIPT_DIR/black/black" + +# Try to find black in different possible locations +if [[ ! -x "$BLACK_BIN" ]]; then + BLACK_BIN="$SCRIPT_DIR/../pip/bin/black/black" +fi + +if [[ ! -x "$BLACK_BIN" ]]; then + # Search for black in runfiles + BLACK_BIN="$(find "$RUNFILES_DIR" -name "black" -type f -executable 2>/dev/null | head -1)" +fi + +if [[ -z "$BLACK_BIN" ]] || [[ ! -x "$BLACK_BIN" ]]; then + echo "Error: Could not find black binary" >&2 + echo "Looked in: $SCRIPT_DIR/black/black" >&2 + exit 1 +fi + +# Change to the workspace directory (BUILD_WORKSPACE_DIRECTORY is set by bazel run) +if [[ -n "$BUILD_WORKSPACE_DIRECTORY" ]]; then + cd "$BUILD_WORKSPACE_DIRECTORY" +fi + +# Run black in check mode +exec "$BLACK_BIN" --check --diff . diff --git a/scripts/black_fix.sh b/scripts/black_fix.sh new file mode 100755 index 0000000..de574f0 --- /dev/null +++ b/scripts/black_fix.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Script to run black formatter to fix Python files. +# Usage: bazel run //:black_fix -- [args] +# +# Example: bazel run //:black_fix -- . + +set -e + +# Find the black binary in the runfiles +SCRIPT_DIR="$(dirname "$0")" +BLACK_BIN="$SCRIPT_DIR/black/black" + +# Try to find black in different possible locations +if [[ ! -x "$BLACK_BIN" ]]; then + BLACK_BIN="$SCRIPT_DIR/../pip/bin/black/black" +fi + +if [[ ! -x "$BLACK_BIN" ]]; then + # Search for black in runfiles + BLACK_BIN="$(find "$RUNFILES_DIR" -name "black" -type f -executable 2>/dev/null | head -1)" +fi + +if [[ -z "$BLACK_BIN" ]] || [[ ! -x "$BLACK_BIN" ]]; then + echo "Error: Could not find black binary" >&2 + echo "Looked in: $SCRIPT_DIR/black/black" >&2 + exit 1 +fi + +# Change to the workspace directory (BUILD_WORKSPACE_DIRECTORY is set by bazel run) +if [[ -n "$BUILD_WORKSPACE_DIRECTORY" ]]; then + cd "$BUILD_WORKSPACE_DIRECTORY" +fi + +# Run black with the provided arguments +exec "$BLACK_BIN" "$@" From 8691b633b83c4106e9fc075f80a432ade929fa9f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Dec 2025 03:02:25 +0000 Subject: [PATCH 3/3] Refactor black scripts to use shared function - 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> --- BUILD | 10 ++++++-- scripts/black_check.sh | 20 +++------------- scripts/black_common.sh | 51 +++++++++++++++++++++++++++++++++++++++++ scripts/black_fix.sh | 20 +++------------- 4 files changed, 65 insertions(+), 36 deletions(-) create mode 100755 scripts/black_common.sh diff --git a/BUILD b/BUILD index 8d5dfa6..d269ebd 100644 --- a/BUILD +++ b/BUILD @@ -34,7 +34,10 @@ alias( sh_binary( name = "black_fix", srcs = ["scripts/black_fix.sh"], - data = [":black"], + data = [ + ":black", + "scripts/black_common.sh", + ], ) # Target to check Python formatting. @@ -43,5 +46,8 @@ sh_binary( sh_binary( name = "black_check", srcs = ["scripts/black_check.sh"], - data = [":black"], + data = [ + ":black", + "scripts/black_common.sh", + ], ) diff --git a/scripts/black_check.sh b/scripts/black_check.sh index ebe0228..0341232 100755 --- a/scripts/black_check.sh +++ b/scripts/black_check.sh @@ -21,25 +21,11 @@ set -e -# Find the black binary in the runfiles SCRIPT_DIR="$(dirname "$0")" -BLACK_BIN="$SCRIPT_DIR/black/black" +source "$SCRIPT_DIR/black_common.sh" -# Try to find black in different possible locations -if [[ ! -x "$BLACK_BIN" ]]; then - BLACK_BIN="$SCRIPT_DIR/../pip/bin/black/black" -fi - -if [[ ! -x "$BLACK_BIN" ]]; then - # Search for black in runfiles - BLACK_BIN="$(find "$RUNFILES_DIR" -name "black" -type f -executable 2>/dev/null | head -1)" -fi - -if [[ -z "$BLACK_BIN" ]] || [[ ! -x "$BLACK_BIN" ]]; then - echo "Error: Could not find black binary" >&2 - echo "Looked in: $SCRIPT_DIR/black/black" >&2 - exit 1 -fi +# Find the black binary +find_black "$SCRIPT_DIR" || exit 1 # Change to the workspace directory (BUILD_WORKSPACE_DIRECTORY is set by bazel run) if [[ -n "$BUILD_WORKSPACE_DIRECTORY" ]]; then diff --git a/scripts/black_common.sh b/scripts/black_common.sh new file mode 100755 index 0000000..eb3fcbd --- /dev/null +++ b/scripts/black_common.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Shared functions for black formatter scripts. +# Source this file to use the find_black function. + +# Find the black binary in the Bazel runfiles. +# Sets BLACK_BIN to the path of the black binary. +# Returns 0 on success, 1 on failure. +find_black() { + local script_dir="$1" + + # Try to find black in different possible locations + BLACK_BIN="$script_dir/black/black" + if [[ -x "$BLACK_BIN" ]]; then + return 0 + fi + + BLACK_BIN="$script_dir/../pip/bin/black/black" + if [[ -x "$BLACK_BIN" ]]; then + return 0 + fi + + # Search for black in runfiles + if [[ -n "$RUNFILES_DIR" ]]; then + BLACK_BIN="$(find "$RUNFILES_DIR" -name "black" -type f -executable 2>/dev/null | head -1)" + if [[ -n "$BLACK_BIN" ]] && [[ -x "$BLACK_BIN" ]]; then + return 0 + fi + fi + + # Could not find black + echo "Error: Could not find black binary" >&2 + echo "Searched in:" >&2 + echo " - $script_dir/black/black" >&2 + echo " - $script_dir/../pip/bin/black/black" >&2 + echo " - RUNFILES_DIR=$RUNFILES_DIR" >&2 + return 1 +} diff --git a/scripts/black_fix.sh b/scripts/black_fix.sh index de574f0..483b419 100755 --- a/scripts/black_fix.sh +++ b/scripts/black_fix.sh @@ -20,25 +20,11 @@ set -e -# Find the black binary in the runfiles SCRIPT_DIR="$(dirname "$0")" -BLACK_BIN="$SCRIPT_DIR/black/black" +source "$SCRIPT_DIR/black_common.sh" -# Try to find black in different possible locations -if [[ ! -x "$BLACK_BIN" ]]; then - BLACK_BIN="$SCRIPT_DIR/../pip/bin/black/black" -fi - -if [[ ! -x "$BLACK_BIN" ]]; then - # Search for black in runfiles - BLACK_BIN="$(find "$RUNFILES_DIR" -name "black" -type f -executable 2>/dev/null | head -1)" -fi - -if [[ -z "$BLACK_BIN" ]] || [[ ! -x "$BLACK_BIN" ]]; then - echo "Error: Could not find black binary" >&2 - echo "Looked in: $SCRIPT_DIR/black/black" >&2 - exit 1 -fi +# Find the black binary +find_black "$SCRIPT_DIR" || exit 1 # Change to the workspace directory (BUILD_WORKSPACE_DIRECTORY is set by bazel run) if [[ -n "$BUILD_WORKSPACE_DIRECTORY" ]]; then