bazel: add Python format lint target#10368
Conversation
There was a problem hiding this comment.
Code Review
This pull request integrates Python formatting and linting into the Bazel build system using the black tool. It introduces new targets for checking and fixing Python formatting, along with the necessary shell scripts and dependency updates. Review feedback focuses on improving the robustness of the scripts by supporting 'main' as a default branch and ensuring uncommitted changes are included in the formatting process to improve the local development experience.
| cd "$WORKSPACE" | ||
|
|
||
| base_ref="" | ||
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/master master; do |
There was a problem hiding this comment.
Many repositories have transitioned from master to main as the default branch name. Adding origin/main and main to the candidate list ensures the linting logic works correctly in environments using the newer naming convention, preventing silent skips of the formatting checks.
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/master master; do | |
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/main main origin/master master; do |
| changed_files+=("$file") | ||
| done < <( | ||
| if [ -n "$base_ref" ]; then | ||
| git diff --name-only --diff-filter=d -z "$base_ref" HEAD -- "*.py" |
There was a problem hiding this comment.
Explicitly specifying HEAD as the second argument to git diff limits the check to committed changes only. Removing it allows the script to also identify and check uncommitted changes in the working tree and index, providing faster feedback during local development. Since $base_ref is determined using git merge-base, this will correctly identify all changes introduced by the current branch plus any pending work.
| git diff --name-only --diff-filter=d -z "$base_ref" HEAD -- "*.py" | |
| git diff --name-only --diff-filter=d -z "$base_ref" -- "*.py" |
| cd "${BUILD_WORKSPACE_DIRECTORY:-$PWD}" | ||
|
|
||
| base_ref="" | ||
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/master master; do |
There was a problem hiding this comment.
Adding origin/main and main to the candidate list ensures the auto-formatting logic works correctly in repositories that use main as the default branch.
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/master master; do | |
| for candidate in "${OPENROAD_LINT_BASE_REF:-}" origin/main main origin/master master; do |
| changed_files+=("$file") | ||
| done < <( | ||
| if [ -n "$base_ref" ]; then | ||
| git diff --name-only --diff-filter=d -z "$base_ref" HEAD -- "*.py" |
There was a problem hiding this comment.
Removing the explicit HEAD argument allows black to format uncommitted changes in the working tree. This is typically the desired behavior when running a 'tidy' script locally before committing code.
| git diff --name-only --diff-filter=d -z "$base_ref" HEAD -- "*.py" | |
| git diff --name-only --diff-filter=d -z "$base_ref" -- "*.py" |
Signed-off-by: naveenvenk17 <naveenvenkat1711@gmail.com>
d385144 to
73ccf19
Compare
|
Follow-up on Gemini feedback:
|
Fixes #9859.
Summary:
//:fmt_py_testand//:tidy_pyusing Bazel's Black console script//:lint_testand//:fix_lintNotes:
.github/workflows/black.yamlworkflow in place, matching the concern in the issue discussion that PRs still need a formatting guardrailOPENROAD_LINT_BASE_REF,origin/master, ormaster, and skips tracked symlinks so Windows worktrees do not feed symlink placeholder files to BlackTests:
wsl bash -lc 'cd /mnt/d/startup/openroad-project/OpenROAD && npx --yes @bazel/bazelisk@latest run --workspace_status_command= //bazel:requirements.update'wsl bash -lc 'cd /mnt/d/startup/openroad-project/OpenROAD && npx --yes @bazel/bazelisk@latest test --workspace_status_command= --test_output=errors //:fmt_py_test'wsl bash -lc 'cd /mnt/d/startup/openroad-project/OpenROAD && npx --yes @bazel/bazelisk@latest run --workspace_status_command= //bazel:black -- --version'BUILD.bazelandbazel/BUILDbash -nforbazel/py_fmt_test.sh,bazel/py_tidy.sh, andbazel/fix_lint.shgit diff --check//:lint_testin the Windows-backed WSL checkout;//:fmt_py_testpassed, but existing full-tree Bazel/Tcl formatting checks reported CRLF/worktree formatting noise unrelated to this PR