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

fix: Fix pwsh git hook exit codes. #1764

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

#### 🐞 Fixes

- Fixed an issue with PowerShell Git hooks not bubbling up exit codes of failed commands.

#### ⚙️ Internal

- Updated proto to v0.44.1 (from 0.43.1).
Expand Down
14 changes: 14 additions & 0 deletions crates/vcs-hooks/src/hooks_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ impl<'app> HooksGenerator<'app> {
"#!/usr/bin/env powershell"
},
"$ErrorActionPreference = 'Stop'",
// https://learn.microsoft.com/en-us/powershell/scripting/learn/experimental-features?view=powershell-7.4#psnativecommanderroractionpreference
"$PSNativeCommandErrorActionPreference = $true",
"",
]);
}
Expand All @@ -216,7 +218,19 @@ impl<'app> HooksGenerator<'app> {

for command in commands {
contents.push(command);

// https://github.com/moonrepo/moon/issues/1761
if !self.is_bash_format() {
contents.extend([
"",
"if ($LASTEXITCODE -ne 0) {",
" exit $LASTEXITCODE",
"}",
"",
]);
}
}

contents.push("\n");

self.create_file(file_path, contents.join("\n"))?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
source: crates/vcs-hooks/tests/hooks_generator_test.rs
expression: "fs::read_to_string(post_push).unwrap()"
expression: "clean_powershell(fs::read_to_string(post_push).unwrap())"
---
#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'
$PSNativeCommandErrorActionPreference = $true

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

moon check --all

if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
---
source: crates/vcs-hooks/tests/hooks_generator_test.rs
expression: "fs::read_to_string(pre_commit).unwrap()"
expression: "clean_powershell(fs::read_to_string(pre_commit).unwrap())"
---
#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'
$PSNativeCommandErrorActionPreference = $true

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

moon run :lint

if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

some-command

if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Loading