chore: add husky pre-commit hook with lint-staged#759
chore: add husky pre-commit hook with lint-staged#759hydrosquall wants to merge 1 commit intovega:mainfrom
Conversation
Enable formatting issues to be automatically handled locally instead of only erroring in CI, reducing wasted CI minutes. Related to vega/vega#4237 Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
|
Hi @hydrosquall — am very supportive of pre-commit hooks. I wonder though if vega-datasets might need a different approach? The TypeScript surface in this repo is very small (4 files in src/, two of which are auto-generated). The primary development here consists of Python scripts and TOML config. In fact, looking at recent CI failures, the bottleneck is the "Format and lint (python)" step, not TypeScript: Run 21546775137 (ruff failure) Run 21695659624 (ruff failure) Also, just wanted to check about running tsc on specific staged files (which lint-staged does). This forces tsc to ignore tsconfig.json, losing the full project context. I'd suggest somehow targeting ruff instead. Maybe like this. "lint-staged": {
"*.py": ["ruff check --fix", "ruff format"]
}A note on Taplo: We might want to leave it out of the hook for now. The original maintainer stepped away from the project in December 2024. While v0.10.0 includes a Linux aarch64 binary, it was never published to PyPI. The latest PyPI version (0.9.3) lacks a Linux ARM wheel, which would break the pre-commit hook for developers on that platform. I'm hopeful v0.10.0 can get published to PyPI soon and if so that would hold us for a while. All this said, I'm very much in favor of finding a suitable way to add pre-commit hooks for this repo. |
|
@dsmedia thanks for the feedback and looking up the recent issues in CI that were specific to this repo. Even though we could opt to run both, I agree ruff is probably the more useful formatter here and it might be enough to just run one given there are only 2 non-auto-generated TS files in this repo! I've adjusted the PR based on your comments. |
Motivation
This PR adds pre-commit hooks to catch formatting issues locally before CI, reduce wasted CI minutes, and ensure consistent code style across the codebase (related to vega/vega#4237).
Changes
husky@^9.1.7for git hooks managementlint-staged@^16.2.7to run linters only on staged filesnpx lint-staged"prepare": "husky"script to auto-initialize hooks onnpm installuv run ruff check --fix- auto-fixes linting issuesuv run ruff format- formats code to project standardsuv runto ensure correct tool versions fromuv.lock(per project standards).gitignoreto exclude.claude/directoryNote: Taplo is excluded from pre-commit hooks due to maintainer stepping away and ARM Linux compatibility concerns, though it remains in CI.
Why Python/ruff instead of eslint format?
As noted in review feedback, this repository has a small TypeScript surface (4 files, 2 auto-generated) but significant Python development (11+ scripts). Recent CI failures have been in the "Format and lint (python)" step, not TypeScript.
Testing
Expand for local testing instructions
Verify husky initialization:
npm install # Should see husky install hooksTest the pre-commit hook with formatting issues: