evilmartians / lefthook

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

Lefthook on windows can run powershell/pwsh as a runner #428

Open j-byrnes opened 1 year ago

j-byrnes commented 1 year ago

:wrench: Summary

When attempting to run Lefthook on Windows, it doesn't seem to identify/see the config file.

Lefthook version

1.2.9 0e6aa810510174063b6040b07e3cbdd9c78fba45

Steps to reproduce

Install

Prepare Repo

push-location c:\repo

lefthook_1.2.9_Windows_x86_64.exe install

lefthook_1.2.9_Windows_x86_64.exe add commit-msg

tree
C:\REPO
├───.lefthook
│   └───commit-msg
└───.lefthook-local
    └───commit-msg

Files

'pwsh.exe -NoProfile -ExecutionPolicy Bypass -File ".\convcoGitHook.ps1 -MessagePath '$1'"'

commit-msg: conventional-commits-check: scripts: "convcoGitHook.ps1": runner: pwsh.exe -NoProfile -ExecutionPolicy Bypass -File

- **repo\.lefthook\commit-msg\convcoGitHook.ps1**
```powershell

Write-Host "convcoGitHook.ps1 EXECUTED!!!!"

Expected results

> lefthook_1.2.9_Windows_x86_64.exe run commit-msg -v
Lefthook v1.2.9
SYNCING
SERVED HOOKS: commit-msg
RUNNING HOOK: commit-msg
convcoGitHook.ps1: (???Executing???)

convcoGitHook.ps1 EXECUTED!!!!

SUMMARY: (???NOT EMPTY???)

>

Actual results

> lefthook_1.2.9_Windows_x86_64.exe run commit-msg -v
Lefthook v1.2.9
SYNCING
SERVED HOOKS: commit-msg
RUNNING HOOK: commit-msg
convcoGitHook.ps1: (SKIP BY NOT EXIST IN CONFIG)

SUMMARY: (SKIP EMPTY)

>

Possible Solution

It is as if the config file isn't getting picked up/read/processed. The file lefthoook.yml is located in the root of the repo c:\repo which was the location it was created during install.

-v argument doesn't seem to include any additional output,

Per usage and configuration, I haven't been able to validate all the configuration file names/locations it would check.

While I'm not a Go programmer, I'm happy to do the heavy lifting on any/all testing for Windows since I know it's not the main focus.

Screenshots (if appropriate)

mrexox commented 1 year ago

Hey! Thank you for the issue. Could you show your lefthook.yml config content, please?

j-byrnes commented 1 year ago

Yeah, I included it above, repo\lefthook.yml

But here it is (in it's entirety;I did remove the default content)

# 'pwsh.exe -NoProfile -ExecutionPolicy Bypass -File ".\convcoGitHook.ps1 -MessagePath '$1'"'
commit-msg:
  conventional-commits-check:
    scripts:
      "convcoGitHook.ps1":
        runner: pwsh.exe -NoProfile -ExecutionPolicy Bypass -File

I get the same result if I use the above runner or one of the following:

        runner: pwsh.exe

#OR

        runner: pwsh
mrexox commented 1 year ago

Hey! I guess you've misconfigured lefthook.yml. Could you try this?


# 'pwsh.exe -NoProfile -ExecutionPolicy Bypass -File ".\convcoGitHook.ps1 -MessagePath '$1'"'
commit-msg:
  scripts:
    "convcoGitHook.ps1":
      runner: pwsh.exe -NoProfile -ExecutionPolicy Bypass -File

There is conventional-commits-check: key that breaks the config format (although no error is thrown :thinking:)

j-byrnes commented 1 year ago

Ok, Made the change to lefthook.yml

# 'pwsh.exe -NoProfile -ExecutionPolicy Bypass -File ".\convcoGitHook.ps1 -MessagePath '$1'"'
commit-msg:
  scripts:
    "convcoGitHook.ps1":
      runner: pwsh.exe -NoProfile -ExecutionPolicy Bypass -File

Results: No change

> lefthook_1.2.9_Windows_x86_64.exe run commit-msg -v
Lefthook v1.2.9
SYNCING
SERVED HOOKS: commit-msg
RUNNING HOOK: commit-msg
convcoGitHook.ps1: (SKIP BY NOT EXIST IN CONFIG)

SUMMARY: (SKIP EMPTY)
mrexox commented 1 year ago

Sorry for the delay. Could you share a github repository with the example configuration? I mean lefthook.yml, .lefthook/pre-commit/* scripts and other files that are related to lefthook configuration. It would help to investigate the issue because it's hard for me to identify the problem :thinking:

j-byrnes commented 1 year ago

Sorry for the delay. Could you share a github repository with the example configuration? I mean lefthook.yml, .lefthook/pre-commit/* scripts and other files that are related to lefthook configuration. It would help to investigate the issue because it's hard for me to identify the problem 🤔

Hey @mrexox, I apologize for the delayed response as work got in the way of responding... I've created a test repo to do further testing as requested:

https://github.com/j-byrnes/testLefthook

Since I see you've had several releases since my original post, I wasn't sure if the procedure for working on Windows had been updated.

Please let me know what the next steps are for troubleshooting.

mrexox commented 1 year ago

Hey @j-byrnes! Thank you for the test repo. I just noticed that the .lefthook/ contains the script testScript.ps1. But the correct path for scripts is - .lefthook/<hook name>/<script name>. See docs for details.

Could you please move the testScript.ps1 into .lefthook/commit-msg/ folder and check if it works?

j-byrnes commented 1 year ago

Thanks for the catch, it looks a little better... now the output is the following

> lefthook run commit-msg
Lefthook v1.3.9
RUNNING HOOK: commit-msg
testScript.ps1: (skip) not specified in config file

SUMMARY: (SKIP EMPTY)

I've commited the changes to the repo... not sure what it is referring to that the file isn't specified in the config file.

mrexox commented 1 year ago

Oh, I see. The problem is - script name is converted to the lowercase when YAML is parsed. That's the problem I guess :confused:

As a workaround I can suggest making script name in all lower case letters with - or _ to separate words. I'll check if it is possible to save the original case when parsing.

j-byrnes commented 1 year ago

That worked, I'm going to explore testing a little more, that may be it.

Also, one more question: Do you have any suggestions for the "rc" configuration, does it have to be shell and can it use relative path names?

j-byrnes commented 1 year ago

The need is to leverage rc to set an environment variable to add lefthook path during execution.

mrexox commented 1 year ago

You can specify this:

# lefthook.yml

rc: ./testrc.sh

# ...

But this is not tested enough, however it should work (note ./ is important, it doesn't work for me without it)