Skip to content

fix(extensions): keep git warnings from failing initialize-repo.ps1 on Windows PowerShell 5.1 - #5252

Merged
mnriem merged 1 commit into
github:mainfrom
Bortlesboat:fix/initialize-repo-ps51-stderr
Nov 5, 2026
Merged

mnriem merged 1 commit into
github:mainfrom
Bortlesboat:fix/initialize-repo-ps51-stderr

Conversation

@Bortlesboat

Copy link
Copy Markdown

Description

On Windows PowerShell 5.1, the git extension's initialize-repo.ps1 fails whenever git writes a warning. The script runs git init, git add . and git commit with 2>&1 under $ErrorActionPreference = 'Stop', and 5.1 turns each line a native command writes to redirected stderr into a terminating error, even when git exits 0. With Git for Windows' default core.autocrlf=true, git add . warns LF will be replaced by CRLF for the LF files Spec Kit writes, so the mandatory before_constitution hook stops with [specify] Error: warning: in the working copy of ... after git init but before the initial commit.

That leaves the project in a bad state. The interrupted git add leaves .git/index.lock behind, so later git add calls fail with Unable to create '.git/index.lock': File exists until the lock is deleted by hand. A rerun prints Git repository already initialized; skipping, so the initial commit never happens. #2680 shows Claude Code on Windows running this script under 5.1 from /speckit-constitution. PowerShell 7.2+ doesn't apply $ErrorActionPreference to native stderr, and the Bash and Python twins only check exit codes, so they are not affected.

This PR relaxes $ErrorActionPreference to Continue around those three git calls and restores it in a finally, as auto-commit.ps1 has done since #2258. Real failures are still caught by the existing $LASTEXITCODE checks.

Also in this PR:

  • tests/extensions/git/test_git_extension.py: _run_pwsh takes an exe argument, and a new TestInitializeRepoPowerShellStderr class runs under pwsh and Windows PowerShell 5.1, following TestResolveTasksPowerShellParity. The positive test adds an LF file with core.autocrlf=true and expects exit 0 and the initial commit. The negative test uses a pre-commit hook that rejects the commit and expects exit 1, the hook's message and no commit. The 5.1 cases skip off Windows; windows-latest has powershell.exe, so CI will run them there.
  • The git extension goes from 1.0.3 to 1.0.4 in extension.yml, catalog.json and the manifest test, for the version guard.

Testing

Windows 11, Windows PowerShell 5.1.26100, PowerShell 7.6.9, Git for Windows 2.45.1, Python 3.14.

  • pytest tests/extensions/git/test_git_extension.py::TestInitializeRepoPowerShellStderr with the script as on main: 1 failed, 3 passed. test_crlf_warnings_do_not_abort_initial_commit[windows-powershell-5.1] fails with [specify] Error: warning: in the working copy of '.specify/extensions/git/extension.yml', LF will be replaced by CRLF the next time Git touches it and assert 1 == 0.

  • This branch: 4 passed.

  • tests/extensions/git/ and tests/test_ps1_encoding.py: 85 passed, 119 skipped. The Bash tests skip here because bash resolves to the WSL launcher.

  • tests/extensions/git/ with a pwsh.exe copy of powershell.exe first on PATH, so every PowerShell test there runs on 5.1: 73 passed, 119 skipped.

  • tests/contract/: 167 passed. uvx [email protected] check src tests: clean. check_extension_version_bump.py: all invariants hold.

  • Tested locally with uv run specify --help

  • Ran existing tests with uv sync && uv run pytest

  • Tested with a sample project (if applicable)

Manual test results

Agent: Claude Code (Claude Opus 5.5), autonomous | OS/Shell: Windows 11 / Windows PowerShell 5.1, PowerShell 7.6.9

Projects were made from this branch with specify init <dir> --integration claude --script ps --extension git --ignore-agent-tools --non-interactive. For the main column, the installed initialize-repo.ps1 was replaced with the one on main. The script line that speckit.git.initialize tells the agent to run was run directly from the project root with powershell.exe -NoProfile -File or pwsh -NoProfile -File, not from an interactive agent session.

Command tested main this PR
/speckit.git.initialize, Windows PowerShell 5.1 Exit 1 on the CRLF warning, no commit, .git/index.lock left. A rerun skips and still has no commit. Exit 0, [OK] Git repository initialized, 1 commit, clean tree
/speckit.git.initialize, PowerShell 7.6.9 Exit 0, 1 commit Exit 0, 1 commit

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Claude Code (Claude Opus 5.5, max reasoning effort, autonomous) found the bug, wrote the fix, tests and this description, and ran the checks above on behalf of Bortlesboat; the commit has an Assisted-by trailer.

…n Windows PowerShell 5.1

initialize-repo.ps1 runs git init, git add and git commit with 2>&1
under $ErrorActionPreference = 'Stop' and checks $LASTEXITCODE.
Windows PowerShell 5.1 turns every line a native command writes to
redirected stderr into a terminating error under 'Stop', even when the
command exits 0. With Git for Windows' default core.autocrlf=true,
git add warns "LF will be replaced by CRLF" for the LF files Spec Kit
writes, so the mandatory before_constitution hook exits 1 after git
init but before the initial commit, leaving a stale .git/index.lock.
A rerun then skips because the repository already exists.

Relax $ErrorActionPreference to 'Continue' around those git calls and
restore it afterwards, as auto-commit.ps1 already does (github#2258).
Failures are still detected through $LASTEXITCODE. PowerShell 7 does
not apply $ErrorActionPreference to native stderr, so it was not
affected.

Bump the git extension to 1.0.4.

Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous)
@Bortlesboat
Bortlesboat requested a review from mnriem as a code owner November 5, 2026 11:41
Copilot AI dynamic review requested due to automatic review settings November 5, 2026 11:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues require changes before approval.

No open issues 🎉

What changed in this PR

Fixes Git extension initialization on Windows PowerShell 5.1 so harmless Git warnings no longer abort the initial commit, following the existing auto-commit.ps1 approach.

Changes:

  • Temporarily relaxes error handling around initialization commands, restores it afterward, and preserves exit-code checks for real failures.
  • Adds warning-success and commit-failure tests for PowerShell 7 and Windows PowerShell 5.1.
  • Bumps the Git extension to version 1.0.4.

The PR reports before-and-after regression results on Windows; these were not independently rerun.

File Description
tests/​extensions/​git/​test_git_extension.py Adds selectable PowerShell hosts and initialization regression tests; updates the expected version.
extensions/​git/​scripts/​powershell/​initialize-repo.ps1 Prevents warnings from terminating initialization and restores the error preference in finally.
extensions/​git/​extension.yml Bumps the extension version to 1.0.4.
extensions/​catalog.json Updates the catalog version to match the manifest.

Review effort: Balanced (auto)

@mnriem mnriem added the triage-must-have Verdict: high-value, important work for Spec Kit — do first label Nov 5, 2026
@mnriem
mnriem requested a dynamic review from Copilot November 5, 2026 13:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the PowerShell git-initialization fix.

No open issues 🎉

Review effort: Balanced (auto)

@mnriem
mnriem merged commit 04fca3d into github:main Nov 5, 2026
22 checks passed
@mnriem

mnriem commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

kevinnguyenhoang91 pushed a commit to kevinnguyenhoang91/spec-kit that referenced this pull request Nov 10, 2026
…n Windows PowerShell 5.1 (github#5252)

initialize-repo.ps1 runs git init, git add and git commit with 2>&1
under $ErrorActionPreference = 'Stop' and checks $LASTEXITCODE.
Windows PowerShell 5.1 turns every line a native command writes to
redirected stderr into a terminating error under 'Stop', even when the
command exits 0. With Git for Windows' default core.autocrlf=true,
git add warns "LF will be replaced by CRLF" for the LF files Spec Kit
writes, so the mandatory before_constitution hook exits 1 after git
init but before the initial commit, leaving a stale .git/index.lock.
A rerun then skips because the repository already exists.

Relax $ErrorActionPreference to 'Continue' around those git calls and
restore it afterwards, as auto-commit.ps1 already does (github#2258).
Failures are still detected through $LASTEXITCODE. PowerShell 7 does
not apply $ErrorActionPreference to native stderr, so it was not
affected.

Bump the git extension to 1.0.4.

Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-must-have Verdict: high-value, important work for Spec Kit — do first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants