Repository navigation
chore: add pre-commit hook using nx format + affected lint - #282
Conversation
| "jsdom": "~22.1.0", | ||
| "jsonc-eslint-parser": "^2.1.0", | ||
| "lerna": "^8.0.0", | ||
| "lint-staged": "^15.0.0", |
There was a problem hiding this comment.
Do we need a separate package for that? Couldn't we do nx affected --uncommited?
There was a problem hiding this comment.
Dobre pytanie. Sprawdziłem konkretnie, jak to wygląda w tym repo:
nx affected -t lintużywa targetulint, który jest zdefiniowany jakoeslint .zcwdustawionym na katalog projektu. Czyli działa na granularności całego projektu — przy zmianie jednego pliku przelintowałby cały affected package, a nie tylko zastagowane pliki.- Nie mamy w ogóle targetu
prettier/formatna projektach. Formatowanie przez nx jest dostępne tylko jako wbudowanenx format:write(i tak,--uncommittedistnieje). Warto zauważyć, że CI w ogóle nie odpala prettiera — leci tylkonx affected:lint. --uncommitteddziała na całym working tree, łącznie ze zmianami niezastagowanymi, i nie re-staguje auto-fixów. lint-staged działa dokładnie na zastagowanych plikach i dostawia poprawki z powrotem do indeksu.- Caveat z częściowym stagowaniem (
git add -p): lint-staged stashuje niezastagowaną resztę i operuje tylko na zastagowanym fragmencie;nx ... --uncommittedruszyłby też niezastagowaną resztę pliku, co przy częściowym commicie potrafi zaskoczyć.
Tradeoff w skrócie:
- lint-staged — precyzja na poziomie zastagowanych plików + auto-re-stage, kosztem jednej devDependency.
- nx (
format:write --uncommitted+affected -t lint --uncommitted) — bez nowej zależności i spójne z konwencją CI (nx affected), ale granularność na poziomie projektu, formatowanie tylko whole-file, brak re-stage i niezgrabne zachowanie przy częściowo zastagowanych plikach.
Moim zdaniem dla hooka pre-commit (gdzie chodzi właśnie o "popraw dokładnie to, co commituję") lint-staged lepiej pasuje do use case'u i ta jedna zależność się broni. Ale jest to realny wybór projektowy — jeśli wolicie zero nowych zależności i trzymanie się nx affected, chętnie przepiszę hooka na podejście oparte na nx format:write --uncommitted + nx affected -t lint --uncommitted. Którą wersję wolisz?
There was a problem hiding this comment.
A są jeszcze jakieś inne opcje w nxie?
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DXXPnGepYUGie3we1X9PTR
f24f2a6 to
041c9cf
Compare
|
Przerobiłem hook na wariant oparty o nx — bez dodawania nowej zależności ( Hook ( npx nx format:write --uncommitted
git add -u
npx nx affected -t lint --uncommittedRe-staging: @Komoszek — co do innych opcji w nx (v22), menu pod pre-commit wygląda tak:
Dlaczego Jedna uwaga praktyczna: target |
Requested by Sebastian Konowrocki · Slack thread
Before: Husky was installed and only enforced commit-message linting (
.husky/commit-msg→ commitlint). Nothing checked the staged code itself before a commit, so lint/format issues were only caught later in CI.After: Committing formats and lints what you are committing using the repo's existing nx tooling — no new dependency is added. Formatting fixes are re-staged so they land in the commit, and only the affected projects are linted, keeping it fast.
How:
.husky/pre-commit(same shebang-less Husky v9 style as.husky/commit-msg) runs:npx nx format:write --uncommitted— formats the changed files via nx's built-in Prettier.git add -u— re-stages those formatting fixes, becausenx format:writerewrites files but does notgit addthem, so without this the fixes would not be part of the commit.npx nx affected -t lint --uncommitted— runs thelinttarget for the projects affected by the changes.lint-stageddevDependency and itspackage.jsonconfig block;package-lock.jsonis regenerated accordingly.Verified locally:
nx format:write --uncommittedandnx affected -t lint --uncommittedrun cleanly, and a staged-edit dry run confirmed formatting fixes are re-staged and lint errors block the commit.Generated by Claude Code