Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] pre-commit hooks don't propagate exit code in powershell #1761

Closed
chadrik opened this issue Dec 26, 2024 · 0 comments · Fixed by #1764
Closed

[bug] pre-commit hooks don't propagate exit code in powershell #1761

chadrik opened this issue Dec 26, 2024 · 0 comments · Fixed by #1764
Assignees
Labels
bug Something isn't working

Comments

@chadrik
Copy link
Contributor

chadrik commented Dec 26, 2024

Describe the bug

The autogenerated pre-commit.ps1 script does not return non-zero when the configured commands return non-zero, and as a result they will not halt a commit from proceeding on error.

Steps to reproduce

Using Powershell on Windows, do the following:

  1. Set the pre-commit script to an executable (not another powershell script) that returns non-zero such as node -e "process.exit(1)" or python -c "import sys;sys.exit(1)":
vcs:
  manager: 'git'
  hooks:
    pre-commit:
      - 'python -c "import sys;sys.exit(1)"'
  1. Use moon sync hooks to create the pre-commit.ps1 script. The result looks like this:
#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'

# Automatically generated by moon. DO NOT MODIFY!
# https://moonrepo.dev/docs/guides/vcs-hooks

python -c "import sys;sys.exit(1)"
  1. Open a powershell console and execute the script:
pwsh .moon\hooks\pre-commit.ps1
  1. Print the exit code from the powershell console:
Write-Host "exit code $($LASTEXITCODE)"

The expectation is that $ErrorActionPreference = 'Stop' will cause the script to abort and return non-zero as with set -e in Linux, but $ErrorActionPreference = 'Stop' only applies to powershell cmlets, not "native" commands (i.e. subproceses), as explained here.

Expected behavior

pre-commit.ps1 should return non-zero when one of its commands fails, so that git commit will be halted.

In Powershell 7.4 there is an option called $PSNativeCommandUseErrorActionPreference which, when set to $true, will cause the $ErrorActionPreference = 'Stop' to apply to native commands, and should produce the expected behavior. However, 7.4 is still relatively new, so it would be best if moon supported older versions of powershell. I tested $PSNativeCommandUseErrorActionPreference in powershell 7.2 and it has no effect.

A solution that works for older versions of powershell requires an explicit check of the exit code after each command:

#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'

# Automatically generated by moon. DO NOT MODIFY!
# https://moonrepo.dev/docs/guides/vcs-hooks

first-command 

if ($LASTEXITCODE -ne 0) {
    throw "Command failed. See above errors for details"
}

second-command 

if ($LASTEXITCODE -ne 0) {
    throw "Command failed. See above errors for details"
}

Screenshots

Environment


@chadrik chadrik added the bug Something isn't working label Dec 26, 2024
@chadrik chadrik changed the title [bug] pre-commit hooks don't propagate exit code [bug] pre-commit hooks don't propagate exit code in powershell Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants