evilmartians / lefthook

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

Environment variables from rc file are not available in command run and skip run #877

Open pschirch opened 5 days ago

pschirch commented 5 days ago

:wrench: Summary

We use an environment variable based tool stack. So it should be possible to skip specific lefthook commands by configure an environment variable, e.q. SOME_CHECK_ENABLED. We read about rc: in docs and found our use case:

Or if you want to use ENV variables that control the executables behaviour in lefthook.yml

Furthermore, based on the consumption that rc: exports all environment variables from an .env file, it should be possible to use skip from docs to skip a command like:

commands:
  some:check:
    skip:
      - run: test "${SOME_CHECK_ENABLED}" = "false"
    run: ...

I hope my explanations are sufficient. I am grateful for any advice.

Lefthook version

1.8.4

Steps to reproduce

.env

SOME_CHECK_ENABLED=false
SOME_OTHER_CHECK_ENABLED=false

lefthook.rc

. .env

We also test

. "$(cd "$(dirname "$0")" && pwd)/.env"

We had to use "$(cd "$(dirname "$0")" && pwd)/.env" in later mentioned command some-other:check instead of relative . .env to get this working.

and

export SOME_CHECK_ENABLED=false
export SOME_OTHER_CHECK_ENABLED=false

That always should work.

lefthook.yaml

skip_output:
  - meta
  - success
  - skips

min_version: 1.8.4

rc: ./lefthook.rc

some-framework:
  commands:
    some:check:
      skip:
        - run: test "${SOME_CHECK_ENABLED}" = "false"
      run: 'echo SOME_CHECK_ENABLED: enabled'

    some-other:check:
      skip:
        - run: |
            . "$(cd "$(dirname "$0")" && pwd)/.env"
            test "${SOME_OTHER_CHECK_ENABLED}" = "false"
      run: 'echo SOME_OTHER_CHECK_ENABLED: enabled'

We also tried rc: <some_absolute_path>/lefthook.rc

lefthook install --force && lefthook --verbose run some-framework

Expected results

The environment variable SOME_CHECK_ENABLED processed in rc file lefthook.rc should be available in command's some:check skip run to made the skip test.

Furthermore, the command's some:check run should also aware of these environment variables.

Change command some:check run to run: env for a test.

lefthook install --force && lefthook --verbose run some-framework | grep SOME_CHECK_ENABLED

Actual results

The environment variable SOME_CHECK_ENABLED is not available, the skip test fails and the command some:check will be executed.

Possible Solution

Less a solution, more a workaround.

Source . "$(cd "$(dirname "$0")" && pwd)/.env" in skip run every time.

    some-other:check:
      skip:
        - run: |
            . "$(cd "$(dirname "$0")" && pwd)/.env"
            test "${SOME_OTHER_CHECK_ENABLED}" = "false"
      run: 'echo SOME_OTHER_CHECK_ENABLED: enabled'

Logs / Screenshots

sync hooks: ✔️ (some-framework)
│ [lefthook] cmd:    [git rev-parse --path-format=absolute --show-toplevel]
│ [lefthook] stdout: /home/reghas/dev/lefthook-rc-test

│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-path hooks]
│ [lefthook] stdout: /home/reghas/dev/lefthook-rc-test/.git/hooks

│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-path info]
│ [lefthook] stdout: /home/reghas/dev/lefthook-rc-test/.git/info

│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-dir]
│ [lefthook] stdout: /home/reghas/dev/lefthook-rc-test/.git

│ [lefthook] cmd:    [git hash-object -t tree /dev/null]
│ [lefthook] stdout: 4b825dc642cb6eb9a060e54bf8d69288fbee4904

│ [lefthook] cmd:    [sh -c . "$(cd "$(dirname "$0")" && pwd)/.env"
test "${SOME_OTHER_CHECK_ENABLED}" = "false"                     
]                                                                
│ [lefthook] skip/only cmd: . "$(cd "$(dirname "$0")" && pwd)/.env"
test "${SOME_OTHER_CHECK_ENABLED}" = "false"                     
, result: true                                                   
│ [lefthook] cmd:    [sh -c test "${SOME_CHECK_ENABLED}" = "false"]
│ [lefthook] error:  exit status 1
│ [lefthook] skip/only cmd: test "${SOME_CHECK_ENABLED}" = "false", result: false
┃  some:check ❯ 

SOME_CHECK_ENABLED: enabled

  ────────────────────────────────────
summary: (done in 0.00 seconds)       
mrexox commented 1 day ago

Hey @pschirch ! Does it work when you're running Git hooks? Is it important for you to have this rc file working for non-git hooks? Originally rc option was supposed to be used when you are using git in a IDE or your shell doesn't provide some environment variables required for your hooks.