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

lefthook is artificially slow when node_modules is not in the Git root #510

Closed
isaaclyman opened this issue Jun 26, 2023 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@isaaclyman
Copy link

🔧 Summary

On my team we have a project structured like so:

  • project/
    • .git/
    • front_end/
      • package.json
      • node_modules/
    • back_end/

So our package.json and node_modules are located in a subfolder of the project root. But our pre-commit hook, handled by lefthook, was taking 45+ seconds to run on every commit, which was strange because if I ran npx lefthook run pre-commit it took less than 5 seconds.

I finally looked into it, added some echo statements to the pre-commit script, and discovered the following:

  • The script looks for an installed version of lefthook in $dir/node_modules/lefthook, where $dir is the top-level directory of the Git working tree.
  • If not found, eventually the script falls back to npx @evilmartians/lefthook.
  • NPX first looks in the current directory for node_modules binaries and packages.
  • It then looks in PATH.
  • If not found, NPX downloads the named package from the NPM registry.
  • The download and install take far more time than the hooks themselves.

There's no message to indicate that lefthook is falling back to installing itself from the NPM registry, so it took me a while to figure out what has happening.

Lefthook version

1.4.3

Steps to reproduce

  • Create a lefthook.yml at the root of a Git repository with a pre-commit hook
  • In a subfolder, run npm install --save-dev lefthook
  • In the project root, run npx lefthook install
  • Run git commit

Expected results

I would see some message indicating that my lefthook installation was not found.

Actual results

The version of lefthook installed in subfolder/node_modules is not found, and @evilmartians/lefthook is downloaded from the NPM registry instead.

Possible Solutions

  • A config option in lefthook.yml could be provided to indicate where the node_modules folder is located.
  • The pre-commit script could search the project recursively up to a certain depth to find node_modules.
  • If all previous attempts to find lefthook fail, a warning message could indicate that npx may download it from NPM, introducing a large delay into the hook.
@isaaclyman isaaclyman added the bug Something isn't working label Jun 26, 2023
montchr added a commit to montchr/dotfield that referenced this issue Jul 5, 2023
@montchr
Copy link

montchr commented Jul 5, 2023

I haven't investigated the cause, but I've also been experiencing extremely long delays, oftentimes around 30 seconds per commit. I have not seen this issue when running git commands in an interactive shell, but it does occur when using Magit in Emacs. I don't know when this started exactly, but I would guess sometime in the past month or two – I had been using lefthook without issue before then.

@hyperupcall hyperupcall mentioned this issue Jul 11, 2023
2 tasks
@bard
Copy link

bard commented Sep 21, 2023

@montchr I don't know about OP's issue but yours is probably the same I encountered, and it's due to the termenv dependency (via lipgloss). I submitted a PR to fix this: muesli/termenv#152

As a workaround for Magit, setting TERM=tmux or TERM=screen might also work.

@mrexox
Copy link
Member

mrexox commented Mar 15, 2024

Please, check out new versions (1.6.x) This issue must be fixed there

@mrexox mrexox closed this as completed Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants