chore: improve pre commit performance (#5406)

* chore: improve pre-commit hook trigger and make sure only applies to
staged/unstaged changes.

* docs: update code-quality doc

* docs: improve doc
This commit is contained in:
alamin-br authored and GitHub committed 2026-05-21 15:05:32 +00:00
1 parent 4373a68e35
commit 40b8371c68
5 files changed
+215 -25

No files matched your search

+7 -5
View File
@@ -26,13 +26,15 @@ repos:
name: backend lint & format (just b fix)
entry: just b fix
language: system
pass_filenames: false
files: \.py$
pass_filenames: true
# Mirror the dirs ruff lints in `just b fix` (see backend/justfile)
files: ^(backend/(src|tests)|(premium|enterprise)/backend/(src|tests))/.*\.py$
- id: frontend-fix
name: frontend lint & format (just f fix)
entry: just f fix
language: system
pass_filenames: false
files: \.(js|jsx|ts|tsx|vue|mjs|css|scss|json|md|html)$
exclude: ^(web-frontend/\.nuxt/|web-frontend/dist/|web-frontend/\.output/)
pass_filenames: true
# Mirror the dirs eslint/stylelint/prettier run on in `just f fix`
files: ^(web-frontend|(premium|enterprise)/web-frontend)/.*\.(js|jsx|ts|tsx|vue|mjs|css|scss|json|md|html)$
exclude: ^web-frontend/(\.nuxt|dist|\.output)/
+61 -8
View File
@@ -201,24 +201,77 @@ uv *ARGS:
# Code Quality & Linting
# =============================================================================
# Run all lint checks
# Run all lint checks. Pass file paths to lint only those files (repo-root-relative,
# e.g. backend/src/foo.py). Paths outside the backend trees are silently skipped.
[group('4 - code-quality')]
check: _check-dev
[positional-arguments]
check *ARGS: _check-dev
#!/usr/bin/env bash
set -euo pipefail
{{ _load_env }}
{{ uv_run }} ruff check --config pyproject.toml {{ backend_source_dirs }} {{ backend_tests_dirs }}
{{ uv_run }} ruff format --config pyproject.toml --check {{ backend_source_dirs }} {{ backend_tests_dirs }}
if [ $# -eq 0 ]; then
{{ uv_run }} ruff check --config pyproject.toml {{ backend_source_dirs }} {{ backend_tests_dirs }}
{{ uv_run }} ruff format --config pyproject.toml --check {{ backend_source_dirs }} {{ backend_tests_dirs }}
exit 0
fi
PY_FILES=()
for f in "$@"; do
case "$f" in
*.py) ;;
*) continue ;;
esac
# Rewrite repo-root-relative paths to backend-relative so ruff runs from
# backend/ — matching the no-args invocation. This is required so ruff's
# src auto-detection and per-file-ignores resolve consistently.
case "$f" in
backend/*) PY_FILES+=("${f#backend/}") ;;
premium/backend/*|enterprise/backend/*) PY_FILES+=("../$f") ;;
*) continue ;;
esac
done
if [ ${#PY_FILES[@]} -eq 0 ]; then
exit 0
fi
{{ uv_run }} ruff check --config pyproject.toml "${PY_FILES[@]}"
{{ uv_run }} ruff format --config pyproject.toml --check "${PY_FILES[@]}"
alias lint := check
alias l := check
# Fix code style (sort imports + format)
# Fix code style (sort imports + format). Pass file paths to fix only those files
# (repo-root-relative, e.g. backend/src/foo.py). Paths outside the backend trees
# are silently skipped.
[group('4 - code-quality')]
fix: _check-dev
{{ uv_run }} ruff check --config pyproject.toml --fix {{ backend_source_dirs }} {{ backend_tests_dirs }}
{{ uv_run }} ruff format --config pyproject.toml {{ backend_source_dirs }} {{ backend_tests_dirs }}
[positional-arguments]
fix *ARGS: _check-dev
#!/usr/bin/env bash
set -euo pipefail
if [ $# -eq 0 ]; then
{{ uv_run }} ruff check --config pyproject.toml --fix {{ backend_source_dirs }} {{ backend_tests_dirs }}
{{ uv_run }} ruff format --config pyproject.toml {{ backend_source_dirs }} {{ backend_tests_dirs }}
exit 0
fi
PY_FILES=()
for f in "$@"; do
case "$f" in
*.py) ;;
*) continue ;;
esac
case "$f" in
backend/*) PY_FILES+=("${f#backend/}") ;;
premium/backend/*|enterprise/backend/*) PY_FILES+=("../$f") ;;
*) continue ;;
esac
done
if [ ${#PY_FILES[@]} -eq 0 ]; then
exit 0
fi
{{ uv_run }} ruff check --config pyproject.toml --fix "${PY_FILES[@]}"
{{ uv_run }} ruff format --config pyproject.toml "${PY_FILES[@]}"
alias format := fix
alias f := fix
+3
View File
@@ -173,6 +173,9 @@ packages = ["src/baserow"]
target-version = "py311"
line-length = 88
exclude = ["**/migrations/**", "**/generated/**"]
# Apply `exclude` even when files are passed explicitly on the command line,
# so per-file invocations (e.g. via pre-commit) skip migrations/generated code.
force-exclude = true
[tool.ruff.lint]
select = [
+55 -6
View File
@@ -11,14 +11,49 @@ you can easily run the linters using [just](./justfile.md) commands.
**Backend (from project root or `backend/` directory):**
- `just b format`: auto-format all Python code using Ruff formatter.
- `just b fix`: run Ruff checks with automatic fixes and format Python code.
- `just b lint`: check Python code with Ruff.
Aliases: `just b format`, `just b f`.
- `just b lint`: check Python code with Ruff. Alias: `just b check`, `just b l`.
**Frontend (from project root or `web-frontend/` directory):**
- `just f lint`: check JavaScript and SCSS files with ESLint and Stylelint.
- `just f fix`: auto-fix code style issues.
- `just f lint`: check the frontend trees with ESLint (JS/TS/Vue), Stylelint
(SCSS), and Prettier (also CSS/JSON/Markdown/HTML/YAML).
- `just f fix`: auto-fix issues with the same tools.
All of the above accept an optional list of file paths and restrict the run to
just those files:
```bash
just b fix backend/src/baserow/core/utils.py
just f fix web-frontend/modules/core/jobTypes.js
```
A few things to know when passing paths:
- **Paths must be repo-root-relative** (for example `backend/src/baserow/...`,
not `src/baserow/...`), even when you invoke the recipe from inside
`backend/` or `web-frontend/`. This keeps the behavior identical regardless
of where you run it from and matches what `pre-commit` passes in.
- Paths outside the linted trees (`backend/{src,tests}`,
`{premium,enterprise}/backend/{src,tests}`, `web-frontend/`,
`{premium,enterprise}/web-frontend/`) are silently skipped, as are files with
extensions the tools don't handle. This lets you pipe in a mixed file list
without filtering it yourself.
- **With no arguments each recipe lints its full component tree** (`just b`
walks all backend trees, `just f` walks all frontend trees; running both
covers the monorepo). This matters when you scope to a diff with `$(...)`
— if the diff is empty, the expansion produces no arguments and you'll
accidentally trigger a full-tree run. Guard the call when scripting:
```bash
# Scope to your branch's changes, skipping the run if nothing changed:
files=$(git diff --name-only origin/develop...HEAD)
if [ -n "$files" ]; then
just b fix $files
just f fix $files
fi
```
## Running tests
@@ -26,7 +61,7 @@ There are also commands to easily run the tests.
- `just b test` (backend): run all backend Python tests with pytest.
- `just b test -n=auto` (backend): run tests in parallel for faster execution.
- `just f test` (frontend): run all frontend tests with Jest.
- `just f test` (frontend): run all frontend tests with Vitest.
## Git pre-commit hooks
@@ -73,7 +108,7 @@ just b run pre-commit run --files path/to/file1.py path/to/file2.js
A normal `git commit` runs the hooks on **staged files only**, and `pre-commit run`
with no arguments does the same. To lint every file you have changed relative to
`HEAD` (staged *and* unstaged), pass the diff explicitly:
`HEAD` (staged _and_ unstaged), pass the diff explicitly:
```bash
just b run pre-commit run --files $(git diff --name-only HEAD)
@@ -88,6 +123,20 @@ lint everything your branch changes:
just b run pre-commit run --files $(git diff --name-only origin/develop...HEAD)
```
If you only want to run the Ruff or ESLint/Stylelint/Prettier steps (and skip the
ancillary pre-commit hooks like `check-yaml`), call the `just` recipes directly
with the same file list — they accept a list of paths and route each file to the
appropriate tool. Remember to guard against an empty diff, otherwise an empty
expansion will lint the whole monorepo:
```bash
files=$(git diff --name-only origin/develop...HEAD)
if [ -n "$files" ]; then
just b fix $files
just f fix $files
fi
```
See the [pre-commit documentation](https://pre-commit.com/#usage) for more advanced
usage (skipping hooks for a commit, running a single hook, updating hook versions,
etc.).
+89 -6
View File
@@ -78,15 +78,98 @@ install:
# Code Quality & Linting
# =============================================================================
# Run all linters (eslint + stylelint + prettier)
# Run all linters (eslint + stylelint + prettier). Pass file paths to lint only those files
# (repo-root-relative, e.g. web-frontend/modules/foo.js). Paths outside the three
# frontend trees are silently skipped.
#
# Each tool is invoked in the same cwd as the matching yarn script:
# - eslint runs from the repo root (where eslint.config.mjs lives and where
# "files: [premium/web-frontend/**/*]" path patterns can match correctly)
# - stylelint and prettier run from web-frontend/ so their .stylelintignore and
# .prettierignore files (which live there) get auto-discovered
# Per-file results therefore match the no-args path, with one intentional
# difference: eslint is invoked with `--no-warn-ignored` so that when
# pre-commit (or a manual diff-driven invocation) passes a file that lives in
# an eslint-ignored location, we don't emit a noisy warning per file. The
# no-args path doesn't need this flag because it walks directories.
[group('4 - code-quality')]
lint:
yarn run lint
[positional-arguments]
lint *ARGS:
#!/usr/bin/env bash
set -euo pipefail
# Fix code style issues
if [ $# -eq 0 ]; then
yarn run lint
exit 0
fi
JS_FILES=() # repo-root-relative, for eslint (run from repo root)
SCSS_FILES=() # web-frontend-relative, for stylelint (run from web-frontend/)
PRETTIER_FILES=() # web-frontend-relative, for prettier (run from web-frontend/)
for f in "$@"; do
case "$f" in
web-frontend/*) rel="${f#web-frontend/}" ;;
premium/web-frontend/*|enterprise/web-frontend/*) rel="../$f" ;;
*) continue ;;
esac
case "$f" in
*.js|*.jsx|*.ts|*.tsx|*.vue|*.mjs|*.cjs) JS_FILES+=("$f"); PRETTIER_FILES+=("$rel") ;;
*.scss) SCSS_FILES+=("$rel"); PRETTIER_FILES+=("$rel") ;;
*.css|*.json|*.md|*.html|*.yml|*.yaml) PRETTIER_FILES+=("$rel") ;;
esac
done
if [ ${#JS_FILES[@]} -gt 0 ]; then
[ -f .nuxt/eslint.config.mjs ] || yarn postinstall
(cd .. && web-frontend/node_modules/.bin/eslint --no-warn-ignored --cache --cache-location=web-frontend/.cache/eslint "${JS_FILES[@]}")
fi
if [ ${#SCSS_FILES[@]} -gt 0 ]; then
./node_modules/.bin/stylelint --cache --cache-location=.cache/stylelint "${SCSS_FILES[@]}"
fi
if [ ${#PRETTIER_FILES[@]} -gt 0 ]; then
./node_modules/.bin/prettier --check --log-level=warn --cache --cache-location=.cache/prettier "${PRETTIER_FILES[@]}"
fi
# Fix code style issues. Pass file paths to fix only those files (repo-root-relative,
# e.g. web-frontend/modules/foo.js). Paths outside the three frontend trees are
# silently skipped. See `lint` above for the per-tool cwd rationale.
[group('4 - code-quality')]
fix:
yarn run fix
[positional-arguments]
fix *ARGS:
#!/usr/bin/env bash
set -euo pipefail
if [ $# -eq 0 ]; then
yarn run fix
exit 0
fi
JS_FILES=()
SCSS_FILES=()
PRETTIER_FILES=()
for f in "$@"; do
case "$f" in
web-frontend/*) rel="${f#web-frontend/}" ;;
premium/web-frontend/*|enterprise/web-frontend/*) rel="../$f" ;;
*) continue ;;
esac
case "$f" in
*.js|*.jsx|*.ts|*.tsx|*.vue|*.mjs|*.cjs) JS_FILES+=("$f"); PRETTIER_FILES+=("$rel") ;;
*.scss) SCSS_FILES+=("$rel"); PRETTIER_FILES+=("$rel") ;;
*.css|*.json|*.md|*.html|*.yml|*.yaml) PRETTIER_FILES+=("$rel") ;;
esac
done
if [ ${#JS_FILES[@]} -gt 0 ]; then
[ -f .nuxt/eslint.config.mjs ] || yarn postinstall
(cd .. && web-frontend/node_modules/.bin/eslint --no-warn-ignored --cache --cache-location=web-frontend/.cache/eslint --fix "${JS_FILES[@]}")
fi
if [ ${#SCSS_FILES[@]} -gt 0 ]; then
./node_modules/.bin/stylelint --cache --cache-location=.cache/stylelint --fix "${SCSS_FILES[@]}"
fi
if [ ${#PRETTIER_FILES[@]} -gt 0 ]; then
./node_modules/.bin/prettier --write --log-level=warn --cache --cache-location=.cache/prettier "${PRETTIER_FILES[@]}"
fi
# =============================================================================
# Testing