Repository navigation
fix(cli): resolve the hooks dir git actually runs - #1511
Conversation
|
Thanks for this, and for the issue write-up that came with it. The I reproduced all three layouts before reading the diff. In a linked worktree One question on the husky remap. husky generates a shim per hook name in Two small notes. A user with a global |
|
Thanks — the Windows detail is useful, I only had the POSIX The husky shim questionChecked husky's source rather than reasoning about it, because your framing made me realise I had only ever observed a repo where the shim happened to exist. In husky 9.1.7 let l = [ 'pre-commit', 'pre-merge-commit', 'prepare-commit-msg', 'commit-msg',
'post-commit', 'applypatch-msg', ... ] // 14 names
...
l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\$(dirname "\$0")/h"`, { mode: 0o755 }))The list is fixed and So the end-to-end path is fixed: dispatch is immediate, not deferred to the next But you found a real holeThere is one case where nothing dispatches, and I had it wrong: a checkout where husky has never run. That is the same silent success this PR exists to remove, just relocated one directory over, so leaving it would have undercut the point. Added Writing to Your two notesGlobal
Verification
Adding the warning immediately caught one of my own earlier tests asserting a bare One thing to flag rather than bury: |
|
Thanks @haibeeb, this is a good fix and the diagnosis is better than the title suggests. Using You are right that the The husky This also covers #1609, which is the same One thing before I merge: CI has not run on this recently, only the code health check is reporting. Could you rebase onto main and push so the full matrix runs? Content-wise I have no changes to ask for. |
|
@haibeeb this is my mistake and I want to name it rather than let you find it. I merged #1808 an hour ago, which fixes the worktree crash the same way yours does, via For the record of what you had that the merged one did not: you identified the The husky So this is not superseded. What I would like:
Your branch is also too old to have a CI run against it, so the rebase is needed to get the matrix going regardless. Ping me when it is pushed and I will approve the run and merge it. #1512 stays open until this lands. Sorry for the wasted round trip. |
`install`, `uninstall` and `status` hardcoded `<root>/.git/hooks`, which is wrong in three layouts: - In a linked worktree (and a submodule) `.git` is a *file* holding a gitdir pointer, so `hooks_dir.mkdir()` raised NotADirectoryError and killed the command. - When `core.hooksPath` is set, git ignores `.git/hooks` entirely. husky and lefthook both set it, so most JS repos are affected: the hook installs "successfully", `status` reports it installed, and git never runs it. - A worktree's hooks live in the repository's common dir, not its own gitdir. Resolve via `git rev-parse --git-path hooks` instead, which is the same resolution git performs, then remap husky's generated `.husky/_` up to the committed `.husky/` user-hook dir -- `_` is gitignored and regenerated on every `npm install`, so a hook written there does not survive. Falls back to the previous path when no git binary is available.
Review follow-ups on the hooks-path resolver. husky writes a shim for a fixed list of all 14 git hook names on every install, regardless of which user hooks exist, and each shim execs `.husky/<name>` when that file is present. So a hook written to the user-hook dir is dispatched immediately rather than waiting for the next `npm install`; documented with the citation, and pinned by a test that installs with only `pre-commit` present. The exception is a checkout where husky has never run: `.husky/_` is absent, `core.hooksPath` points at a missing directory, and git runs no hooks at all. Returning a bare "installed" there would reproduce the silent success this resolver exists to remove, so `husky_pending_reason` reports it from both `install` and `status`. Also documents that a global `core.hooksPath` makes the hook shared across repos -- `status` reports installed everywhere and `uninstall` removes it everywhere -- and that the hook body stays repo-scoped through its `$ROOT/.repowise` guard, so a shared hook is inert rather than wrong. Corrects `detect_editor_setup_outcome`'s docstring, which claimed every probe was a local file read; the autosync probe now spawns `git rev-parse` once.
status() now returns installed (reason) in a husky checkout that has not run its package manager yet. The init offer, the completion panel and the workspace status table compared the exact string, so a hook that was installed and merely pending read as absent: the offer asked again, the panel said no auto-sync, and the table showed a cross.
fe6148b to
408db36
Compare
|
Thanks @haibeeb for keeping the husky remap guarded on the I said I would merge it once rebased, and the rebase is on me, so I did it. I pushed a rebase of your branch onto current main. Your two commits are intact with you as author; the conflict was only One commit of mine on top. Three places compared
|
RaghavChamadiya
left a comment
There was a problem hiding this comment.
Rebased onto main with the husky remap intact; merging once CI is green.
Summary
install/uninstall/statushardcoded<root>/.git/hooks. That path crashes on git worktrees and is silently ignored by git whenevercore.hooksPathis set, so the hook reports installed and never runs.git rev-parse --git-path hooks— the same resolution git itself performs — instead of assuming the layout..husky/_up to the committed.husky/user-hook dir, since_is gitignored and regenerated on everynpm install.The three broken layouts
.gitis a file, sohooks_dir.mkdir()raisedNotADirectoryErrorand aborted the commandcore.hooksPathset (husky, lefthook).git/hooks; hook written there is never executed, whilestatusreportsinstalledThe
core.hooksPathcase is the one worth emphasising: it fails silently. On a repo using husky (very common in JS/TS projects)repowise hook installhas been a no-op that reports success, so auto-sync never ran and there was no signal that anything was wrong.Reproduction of the crash:
Why remap husky's
_directoryhusky sets
core.hooksPath=.husky/_, but that directory is regenerated on every install and gitignored wholesale (.husky/_/.gitignoreis*), so a hook written into it disappears at the nextnpm install. husky's shim dispatches to.husky/<hook-name>one level up, which is the durable location. The remap is guarded so a directory merely named_is not affected.Behaviour is unchanged for a plain checkout, and there is a fallback to the previous path if no git binary is available.
Related Issues
Fixes #1512
Test Plan
New
tests/unit/cli/test_hooks_path_resolution.py(9 tests) covers: plain repo, relative and absolutecore.hooksPath, husky remap with and without generated helpers, a non-husky_directory, the worktree crash regression, install/status/uninstall agreeing on one location, and the no-git fallback.pytest) — 9 new;tests/unit/cli/green at 2108 passed, 1 skippedruff check .)npm run build) (no frontend changes)Note:
ruff formatalso wants to reformat pre-existing code inhooks.py(e.g.lines[end + 1 :]). I left that alone to keep this diff focused — CI runsruff check, which passes.Checklist