Skip to content

sh syntax error when running on windows #1167

@NavyD

Description

@NavyD

Description

sh syntax error when running on windows: -c: line 3: syntax error: unexpected end of file .

lefthook.yml

colors: false
test:
  jobs:
    - name: test
      run: |
        if [ -z "$CI" ]; then
            echo "NO CI"
        else
            echo "CI"
        fi

Commands to reproduce

$ $env:LEFTHOOK_VERBOSE='true'

$ lefthook run test
│  [lefthook] git: git version
│             out: git version 2.51.0.windows.2
│  [lefthook] git: git rev-parse --path-format=absolute --show-toplevel --git-path hooks --git-path info --git-dir
│             out: C:/Users/xxxx/workspaces/projects/xxx
│                  C:/Users/xxxx/workspaces/projects/xxx/.git/hooks
│                  C:/Users/xxxx/workspaces/projects/xxx/.git/info
│                  C:/Users/xxxx/workspaces/projects/xxx/.git
│  loading config: C:\Users\xxxx\workspaces\projects\xxx\lefthook.yaml
╭──────────────────────────────╮
│ lefthook v1.13.6  hook: test │
╰──────────────────────────────╯
│  [lefthook] run: "C:\Program Files\Git\bin\sh.exe" -c "if [ -z "$CI" ]; then
│      echo "NO CI"
│  else
│      echo "CI"
│  fi
"
⠙ waiting: testCI
else
    echo CI
fi
: -c: line 3: syntax error: unexpected end of file
│  [lefthook] git: git diff --name-only --cached --diff-filter=ACMR
│  [lefthook] git: git diff --name-only --cached --diff-filter=ACMRD
│  [lefthook] git: git status --short --porcelain
│             out: M lefthook-nodejs.yaml
│                  M lefthook.yaml
│                  ?? CHANGELOG.md
┃  test ❯



  ────────────────────────────────────
summary: (done in 0.19 seconds)
✗ test (0.19 seconds)

Lefthook version

1.13.6 539f66c92f10e20ed369d769afee1cd6e93d5735

Possible solution

Thank you for creating this excellent software!

I found the problem occurs here:

cmdLine := "\"" + sh + "\"" + " -c " + "\"" + cmdstr + "\""

I'm not familiar with the golang and don't understand why it can't be passed as a parameter like unix:

command := exec.CommandContext(ctx, "sh", "-c", cmdstr)

Or we can also use a temp file or stdin for sh to read to avoid this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions