Skip to content

chore: restore the pre-commit hook with lefthook - #5222

Open
Frank3K wants to merge 2 commits into
google:masterfrom
Frank3K:chore/lefthook-pre-commit
Open

Frank3K wants to merge 2 commits into
google:masterfrom
Frank3K:chore/lefthook-pre-commit

Conversation

@Frank3K

@Frank3K Frank3K commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Summary

The repo's pre-commit hook hasn't run since mid-2021. package.json still has a husky v4 "husky": {"hooks": …} block. husky 7 and later ignore that block, and there was no .husky/ directory or prepare script. The script it pointed to (scripts/pre-commit.sh) also had a bug: it ran git stash -k for unstaged changes but never popped the stash.

This PR restores the hook with lefthook instead of husky, which hasn't had a release since 9.1.7. It also fixes clang-format on Apple Silicon.

Changes

Pre-commit hook (lefthook.yml)

  • Runs on staged files only, in order:
    1. clang-format -i on packages/*/src/**/*.ts, excluding *.d.ts. This is the same scope as npm run format.
    2. eslint --fix on staged files. eslint.config.mjs decides which files are actually linted.
  • Fixed files are re-staged. In a partially staged file, unstaged changes are left as they were.
  • Hooks are installed by lefthook's postinstall script on npm install, so no prepare script is needed. On CI (CI=true) they are not installed.
  • Run LEFTHOOK=0 git commit … to skip the hook once.

clang-format

  • Replaces the clang-format npm package with clang-format-node, pinned to 4.0.5, which ships
    clang-format 23.1.5. The old package is at 1.8.0, last released in 2023. It bundles clang-format 15 and has only an x86_64 macOS binary, so it fails with spawn Unknown system error -86 on Apple Silicon Macs without Rosetta. clang-format-node is built from the official LLVM sources and ships native binaries for each
    platform.
  • The version is pinned exactly, because a different clang-format version formats code differently.
  • Adds npm run format:check (clang-format --dry-run --Werror) and runs it in the unit-tests workflow after lint.

Reformat

  • A separate style: commit reformats 13 files with clang-format 23.1.5. It only changes whitespace and line breaks (git diff -w shows only re-wrapped lines). Most of it is a re-indent of decorators.ts.

Removed

  • scripts/pre-commit.sh, the husky v4 config block, and the husky dev dependency.

Testing

Tested on macOS (Apple Silicon), Node 24:

  • npm install installs .git/hooks/pre-commit.
  • Staged a badly formatted change and left another edit unstaged in the same file, then committed. Only the staged change was formatted and committed. The unstaged
    edit stayed in the working tree, and no stash was left behind.
  • A file in a nested folder (src/features/…) is formatted, and .d.ts files are skipped.
  • npm run format:check, npm run lint (0 errors) and npm run build pass.

Notes for contributors

  • After pulling this, run npm install once to install the hook.
  • If you previously set up husky locally and git config core.hooksPath is set, lefthook won't install its hooks. Run git config --unset core.hooksPath, then run npm install again.

@Frank3K
Frank3K force-pushed the chore/lefthook-pre-commit branch from 60189a3 to 8798e1b Compare November 14, 2026 00:12
@Frank3K
Frank3K force-pushed the chore/lefthook-pre-commit branch from 7b891af to 066ddac Compare November 21, 2026 20:06
The hook config in package.json used the husky v4 format, which husky 7+
ignores, so the hook has not run since mid-2021. scripts/pre-commit.sh
also never popped the stash it created for unstaged changes.

- Replace husky with lefthook. Its postinstall script installs the hooks
  on `npm install`. lefthook.yml formats staged TypeScript sources with
  clang-format and then runs `eslint --fix`, re-staging the results.
  Unstaged changes in partially staged files are left untouched.
- Replace the clang-format npm package (clang-format 15, x86_64-only on
  macOS) with clang-format-node, pinned to 4.0.5 (clang-format 23.1.5),
  which ships native binaries for Apple Silicon.
- Add `npm run format:check` and run it in the unit-tests workflow.
- Remove scripts/pre-commit.sh and the husky v4 config block.
Whitespace-only changes from running `npm run format` with the
clang-format version now pinned via clang-format-node.
@Frank3K
Frank3K force-pushed the chore/lefthook-pre-commit branch from 066ddac to 219c167 Compare November 21, 2026 20:11
@Frank3K
Frank3K marked this pull request as ready for review November 21, 2026 20:16

This branch has not been deployed

No deployments
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