Skip to content

Commit

Permalink
fix: Fix pwsh git hook exit codes. (#1764)
Browse files Browse the repository at this point in the history
* Add exit code.

* Update changelog.

* Fix snap.

* Fix tests.
  • Loading branch information
milesj authored Dec 28, 2024
1 parent fed3a5f commit 1acc417
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
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
}

0 comments on commit 1acc417

Please sign in to comment.