Skip to content

fix: suppress CRLF warnings in auto-commit.ps1#2258

Merged
mnriem merged 4 commits intogithub:mainfrom
SARAMALI15792:fix/auto-commit-crlf-warnings
Apr 17, 2026
Merged

fix: suppress CRLF warnings in auto-commit.ps1#2258
mnriem merged 4 commits intogithub:mainfrom
SARAMALI15792:fix/auto-commit-crlf-warnings

Conversation

@SARAMALI15792
Copy link
Copy Markdown
Contributor

@SARAMALI15792 SARAMALI15792 commented Apr 17, 2026

Description

Fixes #2253

The auto-commit.ps1 script fails on Windows when git emits CRLF line-ending warnings to stderr. With $ErrorActionPreference = 'Stop', PowerShell converts any stderr output from native commands into terminating ErrorRecord exceptions — crashing the script before the 2>$null redirection can suppress them.

Root Cause

PowerShell raises a NativeCommandError when it sees any stderr output from a native command under $ErrorActionPreference = 'Stop', terminating the script before stderr redirection takes effect. Lines using 2>$null are ineffective because the error is raised before the redirection is processed.

Solution

Temporarily set $ErrorActionPreference = 'Continue' around all native git calls that may emit stderr warnings (repo detection, change detection, and staging/commit). This prevents stderr from becoming a terminating error while still allowing 2>&1 capture for diagnostics on real failures. Each block uses try/finally to guarantee the preference is restored.

Changes

  • extensions/git/scripts/powershell/auto-commit.ps1 — Three git call sites wrapped with $ErrorActionPreference = 'Continue' + try/finally:
    1. git rev-parse (repo detection)
    2. git diff / git ls-files (change detection — the original crash site)
    3. git add / git commit (staging and commit)
  • tests/extensions/git/test_git_extension.py — New TestAutoCommitPowerShellCRLF class with 5 tests using core.autocrlf=true + LF-ending tracked files. On Windows runners the CRLF warnings fire and validate the fix end-to-end.

Why This Is Safe

  • Only affects $ErrorActionPreference scope; restored via finally blocks
  • Uses 'Continue' (not 'SilentlyContinue') so error output is still captured in $out for diagnostics
  • Exit code semantics unchanged ($LASTEXITCODE is unaffected by ErrorActionPreference)
  • Bash version is unaffected (stderr redirection works correctly there)

Testing

  • All 15 PowerShell tests pass locally (macOS with pwsh)
  • New CRLF tests pass trivially on non-Windows (no warnings emitted); on the Windows runner core.autocrlf=true triggers actual CRLF warnings to exercise the fix
  • Existing TestAutoCommitPowerShell tests confirm no regressions

Replace 2> with 2>&1 redirection and  assignment to properly
suppress stderr output including CRLF warnings on Windows. Exit code
logic preserved for change detection.

Fixes github#2253
@SARAMALI15792 SARAMALI15792 requested a review from mnriem as a code owner April 17, 2026 11:32
The 2>&1 approach still raises terminating errors under
$ErrorActionPreference='Stop'.  Instead, temporarily set
SilentlyContinue around all native git calls that may emit
CRLF warnings to stderr (rev-parse, diff, ls-files, add, commit).

Adds 5 pytest tests (TestAutoCommitPowerShellCRLF) that set
core.autocrlf=true with LF-ending files.  On Windows runners
this triggers actual CRLF warnings; on other platforms the tests
pass trivially.

Fixes github#2253
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Windows failures in the git extension’s PowerShell auto-commit hook when git emits CRLF-related stderr output, and adds regression tests around CRLF warning scenarios.

Changes:

  • Update auto-commit.ps1 to temporarily relax PowerShell error handling around git invocations that can emit CRLF warnings.
  • Add a new PowerShell-focused CRLF test suite to validate auto-commit behavior with core.autocrlf=true.
Show a summary per file
File Description
extensions/git/scripts/powershell/auto-commit.ps1 Prevent CRLF stderr output from becoming terminating errors during repo detection, change detection, and commit steps.
tests/extensions/git/test_git_extension.py Add regression tests intended to cover CRLF-warning behavior for auto-commit.ps1.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 4

Comment thread extensions/git/scripts/powershell/auto-commit.ps1 Outdated
Comment thread extensions/git/scripts/powershell/auto-commit.ps1 Outdated
Comment thread extensions/git/scripts/powershell/auto-commit.ps1 Outdated
Comment thread tests/extensions/git/test_git_extension.py Outdated
- Use 'Continue' instead of 'SilentlyContinue' so error output is
  still captured in $out for diagnostics on real git failures.
- Wrap all three EAP save/restore blocks in try/finally to guarantee
  restoration even on unexpected exceptions.
- Fix CRLF test to commit a tracked LF file first, then modify it,
  so git diff --quiet HEAD actually inspects the tracked change and
  triggers the CRLF warning on Windows.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment thread tests/extensions/git/test_git_extension.py
On Windows, probe git diff stderr before running the script to verify
the test setup actually produces the expected CRLF warning.  This
makes the regression test deterministic on the Windows runner.  On
non-Windows the probe is skipped (warnings don't fire there).
@mnriem mnriem requested a review from Copilot April 17, 2026 13:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new

@mnriem mnriem merged commit ba9a8b8 into github:main Apr 17, 2026
15 checks passed
@mnriem
Copy link
Copy Markdown
Collaborator

mnriem commented Apr 17, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The auto-commit.ps1 script fails when git emits CRLF warnings to stderr

3 participants