Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 won't install or work from a git worktree directory #580

Closed
zfeher opened this issue Sep 13, 2019 · 8 comments
Closed

v3 won't install or work from a git worktree directory #580

zfeher opened this issue Sep 13, 2019 · 8 comments

Comments

@zfeher
Copy link

zfeher commented Sep 13, 2019

Hi' Thank you for working on this project.

Today we wanted to upgrade from 0.14.x to the latest 3.0.5 version. But it didn't work. The hooks didn't run. Checking the hooks directory seemed just fine. Removing husky, and the hooks manually an reinstalling husky did not install the hooks.

However downgrading to 2.6.0 version worked flawlessly.

Then I became suspicious about git worktrees and it seems that is the problem. If we want to install the latest husky from a git worktree it won't install/remove/run the hooks.

As it seems versions ranging from 0.14-2.6.0 are able to handle this.

@typicode
Copy link
Owner

Hi @zfeher,

Thanks for the report.

Husky v3 uses git rev-parse ... to figure out where hooks should be installed. Before, it was some JS code that was doing this.

You can get some debug info during install using HUSKY_DEBUG=1 npm install husky --save-dev. Can you post the output?

Would you also have some commands I can run to reproduce the issue locally?

@zfeher
Copy link
Author

zfeher commented Sep 16, 2019

The requested debug output:

$ HUSKY_DEBUG=1 npm install -D husky

> husky@3.0.5 preuninstall C:\Users\zfeher\dev\projects\acme\acme-green\node_modules\husky
> node husky uninstall

husky:debug cwd: C:\Users\zfeher\dev\projects\acme\acme-green\node_modules\husky
husky:debug INIT_CWD: C:\Users\zfeher\dev\projects\acme\acme-green
husky > Uninstalling git hooks
husky:debug topLevel: C:/Users/zfeher/dev/projects/acme/acme-green
husky:debug gitDir: C:/Users/zfeher/dev/projects/acme/acme-main/.git/worktrees/acme-green
husky > Done

acme-green is a git worktree here whereas the acme-main is the main repo dir where the actual .git folder lives.

I noticed something new: If I installed husky in the main folder it worked. Then I uninstalled it, opened the worktree folder, installed husky there which didn't created hooks under acme-main/.git/hooks (like 2.6.0 did) instead it created hooks in the worktree under main/.git/worktrees/acme-green/hooks.
This sounds reasonable. But the hooks didn't work this way for some reason, maybe it is a git problem. But after installing husky in the main repo folder as well (installed hooks in acme-main/.git/hooks), the hooks started to work when I commited in the worktree.

You can repro this by making git worktrees in a cloned git repository. To do this:

1, clone or open a repository
2, cd to the repo folder
3, remove husky: npm r husky
4, create a worktree: git worktree add --detach ../repo-worktree
4, cd ../repo-worktree
5, npm i -D husky

Hope this helps.

@typicode
Copy link
Owner

typicode commented Oct 2, 2019

Hi @zfeher,

It helped a lot, thanks! I've just published a new version which should fix the issue (v3.0.8).

@forivall
Copy link
Contributor

forivall commented Oct 5, 2019

I'm having similar issues, and, with the new v3.0.8, whenever i switch from one worktree directory to the other, i have to run npm rebuild husky in order for hooks to work.

The .git directory is at ~owl2-env/packages/chat/.git (~owl2-env and ~owl are zsh named directories)

~owl2-env/packages/chat master
» HUSKY_DEBUG=1 git commit
husky:debug husky v3.0.8 (created at 10/4/2019, 8:03:20 PM)
husky:debug pre-commit hook started
husky:debug Current working directory is '/Users/emily/code/owl/owl2-env/packages/chat'
husky:debug Changed current working directory to '/Users/emily/code/owl/owl2-env/packages/chat'
husky > pre-commit (node v10.16.2)
🔍  Finding changed files since git revision 5b4ae5d.
🎯  Found 0 changed files.
✅  Everything is awesome!
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

~owl2-env/packages/chat master
» cd -
~owl/repos/chat

~owl/repos/chat chore/yarn-dedupe
» HUSKY_DEBUG=1 git commit
husky:debug husky v3.0.8 (created at 10/4/2019, 8:03:20 PM)
husky:debug pre-commit hook started
/Users/emily/code/owl/owl2-env/packages/chat/.git/hooks/pre-commit: line 48: ../../node_modules/run-node/run-node: No such file or directory

~owl/repos/chat chore/yarn-dedupe
» HUSKY_DEBUG=1 npm rebuild husky

> husky@3.0.8 install /Users/emily/code/owl/repos/chat/node_modules/husky
> node husky install

husky:debug Current working directory is '/Users/emily/code/owl/repos/chat/node_modules/husky'
husky:debug INIT_CWD environment variable is set to '/Users/emily/code/owl/repos/chat'
husky > Setting up git hooks
husky:debug Git rev-parse command returned:
husky:debug   --show-top-level: /Users/emily/code/owl/repos/chat
husky:debug   --git-common-dir: /Users/emily/code/owl/owl2-env/packages/chat/.git
husky:debug Installing hooks in '/Users/emily/code/owl/owl2-env/packages/chat/.git/hooks'
husky > Done

> husky@3.0.8 postinstall /Users/emily/code/owl/repos/chat/node_modules/husky
> opencollective-postinstall || exit 0

Thank you for using husky!
If you rely on this package, please consider supporting our open collective:
> https://opencollective.com/husky/donate

husky@3.0.8 /Users/emily/code/owl/repos/chat/node_modules/husky

~owl/repos/chat chore/yarn-dedupe
» HUSKY_DEBUG=1 git commit
husky:debug husky v3.0.8 (created at 10/4/2019, 8:05:55 PM)
husky:debug pre-commit hook started
husky:debug Current working directory is '/Users/emily/code/owl/repos/chat'
husky:debug GIT_DIR environment variable is set to '/Users/emily/code/owl/owl2-env/packages/chat/.git/worktrees/chat'
husky:debug If you're getting "fatal: not a git repository" errors, you may want to unset GIT_DIR
husky:debug Changed current working directory to '/Users/emily/code/owl/repos/chat'
husky > pre-commit (node v10.16.2)
🔍  Finding changed files since git revision 5b4ae5d.
🎯  Found 0 changed files.
✅  Everything is awesome!
On branch chore/yarn-dedupe
nothing to commit, working tree clean

~owl/repos/chat chore/yarn-dedupe
» cd -
~owl2-env/packages/chat

~owl2-env/packages/chat master
» HUSKY_DEBUG=1 git commit
husky:debug husky v3.0.8 (created at 10/4/2019, 8:05:55 PM)
husky:debug pre-commit hook started
.git/hooks/pre-commit: line 48: node_modules/run-node/run-node: No such file or directory

@typicode
Copy link
Owner

@forivall when you change directory it seems like the path to husky is different.

As the layout/bug is a bit complex, could you provide steps to recreate the issue locally?

I don't know if it applies, but you may want to check https://github.com/typicode/husky#monorepos, husky needs to be defined once in a project with multiple package.json.

@zfeher
Copy link
Author

zfeher commented Oct 14, 2019

@typicode thank you for the fix, tried it and works for us flawlessly

forivall added a commit to forivall/husky that referenced this issue Oct 17, 2019
@forivall
Copy link
Contributor

forivall commented Oct 17, 2019

@typicode yeah, it seems like it has to do with our multi-repo setup, where there is a root repo to setup various utilities to work on multiple repositories in sync, & a yarn workspace to share deps, similar to https://github.com/ckeditor/ckeditor5. (although, we're currently using pre-commit for most repositories, and I decided to start the switch to husky for a new project). In the case where the error happens, the repo is in the workspace, and the worktree is outside of it.

I have a (non reproducing) repro set up, and I'll add a parent repository to show the error in action.

@forivall
Copy link
Contributor

forivall commented Oct 17, 2019

Ah, I figured out the workaround / fix. After adding this to the workspaces section of the root repo's package.json (for the yarn workspace), and re-running yarn it works perfectly!

diff --git a/package.json b/package.json
index 7ae7807..e389194 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,8 @@
       "."
     ],
     "nohoist": [
-      "**/husky"
+      "**/husky",
+      "**/run-node"
     ]
   },
   "scripts": {

(I learned that husky needs to be in nohoist from https://github.com/ckeditor/ckeditor5/blob/e022775/package.json#L158 in the first place.)

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

No branches or pull requests

3 participants