evilmartians / lefthook

Fast and powerful Git hooks manager for any type of projects.
MIT License
4.66k stars 211 forks source link

postinstall does not get executed with pnpm #760

Closed thomasfr closed 1 month ago

thomasfr commented 1 month ago

:wrench: Summary

If i run pnpm install in a freshly cloned git repo the postinstall script of lefthook gets not executed locally.

➜ pnpm --version
9.4.0
➜ node --version
v20.15.0
➜ cat package.json
{
    "name": "something",
    "version": "1.0.0",
    "type": "module",
    "private": true,
    "sideEffects": false,
    "scripts": {
        "clean": "git clean -fdX",
        "check": "biome check .",
        "check:fix": "biome check --fix .",
        "check:unused": "knip"
    },
    "devDependencies": {
        "@biomejs/biome": "1.8.2",
        "knip": "^5.22.2",
        "lefthook": "^1.6.18",
        "@playwright/test": "^1.45.0",
        "turbo": "2.0.4"
    },
    "engines": {
        "node": ">=v20.13.1",
        "pnpm": ">=9.1.2"
    },
    "packageManager": "pnpm@9.1.2"
}
➜ cat lefthook.yml
pre-commit:
  parallel: true
  commands:
    check:
      run: pnpm turbo run check -- --staged --fix --no-errors-on-unmatched
      stage_fixed: true
    typecheck:
      run: pnpm turbo run typecheck
    test:
      run: pnpm turbo run test
    unused-code:
      run: pnpm turbo run check:unused

Lefthook version

1.6.18 4ea200b04d374ba1bb9f66a940430309a2838d27

Steps to reproduce

  1. git clone <repo> target-dir
  2. cd target-dir
  3. pnpm install

Expected results

Configured git hooks in lefthook.yaml should be correctly installed in .git/hooks, resulting in git hooks getting triggered correctly.

➜ find .git/hooks/* -not -iname '*.sample'
.git/hooks/pre-commit
.git/hooks/prepare-commit-msg

Actual results

➜ find .git/hooks/* -not -iname '*.sample'

Should list all "installed" git hooks by lefthook (all files not matching *.sample

Possible Solution

I tried on the same Repo, same commit history, with npm and it works as expected, but not with pnpm. Even if i put enable-pre-post-scripts = true to the .npmrc within the repo, it does not get executed.

What did fix it for me was to add side-effects-cache = false to the .npmrc located in the git repository.

So i am not sure if its actually a bug with lefthook, but maybe it should get noted somewhere in your docs that the cache could prevent the installation of lefthook.

mrexox commented 1 month ago

Yes, looks like it is enabled by default in new pnpm versions: https://pnpm.io/blog/2022/12/30/yearly-update#side-effects-cache-since-v700. Thank you for this finding. I will update the docs.