Skip to content

chore: make pre-commit ruff hooks package-aware for monorepo - #18285

Open
chalmerlowe wants to merge 2 commits into
mainfrom
chore/monorepo-precommit-ruff
Open

chore: make pre-commit ruff hooks package-aware for monorepo#18285
chalmerlowe wants to merge 2 commits into
mainfrom
chore/monorepo-precommit-ruff

Conversation

@chalmerlowe

Copy link
Copy Markdown
Contributor

chore: make pre-commit ruff hooks package-aware for monorepo

The Problem

The repository contains over 280 packages that share the google namespace (such as google.cloud.secretmanager, google.api_core, and google.cloud.storage).

When developers run pre-commit hooks locally, Ruff executes from the repository root. Because there is no single top-level google source folder at the root, Ruff's import sorter (isort rule I001) cannot determine that package-local code is first-party. It treats all imports under google.* as third-party dependencies and combines them into one alphabetical list.

However, continuous integration lint sessions (nox -s lint) execute from within each individual package folder (such as packages/google-cloud-secret-manager). In that environment, Ruff recognizes the package's local code as first-party and requires third-party dependencies (like google.protobuf or google.auth) to appear before local imports, separated by a blank line.

This mismatch caused commits formatted by local pre-commit hooks to fail continuous integration lint tests.

The Solution

  • Added ci/run_ruff.py: A lightweight runner script that groups staged Python files by their enclosing package directory (under packages/ or preview-packages/) and executes ruff check and ruff format within each package directory. For repository-level files outside of packages, it executes from the repository root.
  • Updated .pre-commit-config.yaml: Replaced the generic root Ruff hooks with local hooks that call ci/run_ruff.py.

Notes to Reviewers

  • This change requires no manual configuration or extra tools for maintainers or external contributors. Running pre-commit run works automatically out of the box.
  • The script preserves the existing lint standards across the repository, using the same Python 3.10 target version, 88-character line length, and I rule selection used in the package Noxfiles.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the remote Ruff pre-commit hook with a custom, package-aware local runner script (ci/run_ruff.py) to handle linting and formatting within individual package directories in the monorepo. The review feedback highlights critical improvements for the pre-commit configuration, including adding additional_dependencies to pin the Ruff version in the isolated virtualenv and using python instead of python3 for Windows compatibility. Additionally, the feedback suggests handling potential FileNotFoundError exceptions when executing Ruff and expanding the file suffix checks to support .pyi and .ipynb files.

Comment thread .pre-commit-config.yaml
Comment thread ci/run_ruff.py Outdated
Comment thread ci/run_ruff.py Outdated
@chalmerlowe chalmerlowe self-assigned this Sep 4, 2026
@chalmerlowe
chalmerlowe marked this pull request as ready for review September 4, 2026 12:39
@chalmerlowe
chalmerlowe requested a review from a team as a code owner September 4, 2026 12:39
@chalmerlowe
chalmerlowe requested a review from ohmayr September 4, 2026 12:39
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.

1 participant