From 1acc4177a2010906f40e6a157a20d73a62bb03ba Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 27 Dec 2024 21:22:17 -0800 Subject: [PATCH] fix: Fix pwsh git hook exit codes. (#1764) * Add exit code. * Update changelog. * Fix snap. * Fix tests. --- CHANGELOG.md | 4 ++++ crates/vcs-hooks/src/hooks_generator.rs | 14 ++++++++++++++ ..._test__windows__creates_local_hook_files-2.snap | 7 ++++++- ...or_test__windows__creates_local_hook_files.snap | 12 +++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ac7dc8ac9..0cad2f01eca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/crates/vcs-hooks/src/hooks_generator.rs b/crates/vcs-hooks/src/hooks_generator.rs index 4d11b98e372..fa5b9d7995f 100644 --- a/crates/vcs-hooks/src/hooks_generator.rs +++ b/crates/vcs-hooks/src/hooks_generator.rs @@ -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", "", ]); } @@ -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"))?; diff --git a/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files-2.snap b/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files-2.snap index 608189c137a..cb5ec785271 100644 --- a/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files-2.snap +++ b/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files-2.snap @@ -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 +} diff --git a/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files.snap b/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files.snap index 98b4671400e..125633002ea 100644 --- a/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files.snap +++ b/crates/vcs-hooks/tests/snapshots/hooks_generator_test__windows__creates_local_hook_files.snap @@ -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 +}