evilmartians / lefthook

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

npm: Not working with bun without node #688

Closed cethien closed 3 weeks ago

cethien commented 5 months ago

:wrench: Summary

seems like the tool doesn't work without node. i have only bun for js on my system. after lefthook install, every hooked git actions results in /usr/bin/env: ‘node’: No such file or directory

Lefthook version

1.6.7 599459eb87d1dab4fd56707fd1ad3a4209c133c8

Steps to reproduce

curl -fsSL https://bun.sh/install | bash
cd <repo>
bun add -D lefthook
<define any lefthook action, eg. commitlint>
lefthook install
git add . && git commit -m "hello"

Expected results

commitlint error

Actual results

/usr/bin/env: ‘node’: No such file or directory

Logs / Screenshots

+ '[' '' = 0 ']'
+ call_lefthook run prepare-commit-msg .git/COMMIT_EDITMSG message
++ git rev-parse --show-toplevel
+ dir=<userprofile>/source/posh-nv
++ uname
++ tr '[:upper:]' '[:lower:]'
+ osArch=mingw64_nt-10.0-22631
++ uname -m
++ sed s/aarch64/arm64/
+ cpuArch=x86_64
+ test -n ''
+ lefthook.exe -h
+ lefthook.bat -h
+ test -f <userprofile>/source/posh-nv/node_modules/lefthook/bin/index.js
+ <userprofile>source/posh-nv/node_modules/lefthook/bin/index.js run prepare-commit-msg .git/COMMIT_EDITMSG message     
/usr/bin/env: ‘node’: No such file or directory
mrexox commented 5 months ago

Hey! Thank you for creating an issue. Do you have an idea of how an executable script could be run with either nodejs or bun? I can think of a statically built executable (probably with bun) but I have concerns. It would be great to know if there's a common solution for such cases. I will investigate

cethien commented 5 months ago

bun is intended as a node replacement, so just calling a script.js file should work fine.

It would be great to know if there's a common solution for such cases. I will investigate

i will check out some stuff and give a comment

dalisoft commented 4 months ago

@mrexox @cethien #705 maybe fixes this case as removes require of Node.js and #!/usr/bin/env node

Blaquewithaq commented 4 months ago

For those of you, that want to utilize this package with bun now, Ive figured out a quick patch solution until bun support has been officially added.

Add these to your package.json file and you'll be good to go.

"prepare:lefthook": "lefthook install && bun -e \"const fs=require('node:fs'); fs.writeFileSync('node_modules/lefthook/bin/index.js', fs.readFileSync('node_modules/lefthook/bin/index.js', 'utf8').replace(/^#!\\/usr\\/bin\\/env\\s+node/gm, '#!/usr/bin/env bun'))\"",
"postinstall": "bun prepare:lefthook"
dy0gu commented 2 months ago

@Blaquewithaq, thanks for the temporary fix!

Any news on if @dalisoft's PR removes the node dependency to allow runtime independence? Using the above solution is a whole lot of verbose that would be amazing to clear up.

Like @cethien said, Bun is a node runtime replacement, we could simply run any script.js using either Node or Bun depending on the first that passes, since the result will be runtime independent once converted to git hooks.

cethien commented 2 months ago

since I opened the issue, I was experimenting around a bit and noticed something:

when installing other tools like esbuild, there is no js script that executes the binary, only a script to install for the right OS. the root cause for my problem seems to be the js script that runs the binary - is it possible to ditch the index.js alltogether?

mrexox commented 2 months ago

I hope so, I have @dalisoft's PR on my radar, I just need to test it to make sure it solves the issue with bun.

dy0gu commented 1 month ago

I hope so, I have @dalisoft's PR on my radar, I just need to test it to make sure it solves the issue with bun.

Hey @mrexox, it seems you have merged the PR you referred to here, do you have any updates on this issue? Can it be closed, or are there any extra steps that need to be taken?

mrexox commented 1 month ago

Hey! Unfortunately there was a backward compatibility issue with the PR, I am going to try a slightly different approach and will notify ASAP.

mrexox commented 1 month ago

@cethien could you please check if the newest lefthook version works? I've changed the order of checks in the hook template and it must not fail with /usr/bin/env: ‘node’: No such file or directory

cethien commented 1 month ago

@cethien could you please check if the newest lefthook version works? I've changed the order of checks in the hook template and it must not fail with /usr/bin/env: ‘node’: No such file or directory

hey, only managed to test now

I removed node from my system, and tested this one:

.lefthook.yml

# yaml-language-server: $schema=https://json.schemastore.org/lefthook.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

commit-msg:
  parallel: true
  commands:
    commitlint:
      run: bun commitlint --edit $1

it seemed to work, my commit was some gibberish and i got a commitlint error.

cethien commented 3 weeks ago

seems to be resolved