Skip to content

fix(windows): prevent _update_cwd() from overwriting self.cwd with Git Bash POSIX path#16628

Closed
hypen-o wants to merge 1 commit into
NousResearch:mainfrom
hypen-o:fix/update-cwd-windows-posix-path
Closed

fix(windows): prevent _update_cwd() from overwriting self.cwd with Git Bash POSIX path#16628
hypen-o wants to merge 1 commit into
NousResearch:mainfrom
hypen-o:fix/update-cwd-windows-posix-path

Conversation

@hypen-o

@hypen-o hypen-o commented Apr 27, 2026

Copy link
Copy Markdown

Fixes the WinError 267 "The directory name is invalid" error on Windows/Git Bash terminal.backend: local.

Root Cause

LocalEnvironment._update_cwd() (local.py:390) reads the cwd from a temp file written by Git Bash's pwd -P command. Git Bash always outputs POSIX-style paths (e.g. /c/Users/user/project), which are invalid for subprocess.Popen(cwd=...) on Windows.

The failure sequence:

  1. TERMINAL_CWD or os.getcwd() sets self.cwd to a valid Windows path (e.g. D:\project)
  2. After the first successful command, _update_cwd() reads _cwd_file (which contains the POSIX path from Git Bash's pwd -P)
  3. It overwrites self.cwd with /c/Users/user/project (POSIX format)
  4. Next subprocess.Popen(cwd=self.cwd) -> WinError 267 (The directory name is invalid)
  5. All subsequent terminal commands fail silently

This affects all Windows users with terminal.backend: local using native Git Bash without WSL2. The initial cwd from TERMINAL_CWD is correct but gets immediately overwritten by the POSIX path from _update_cwd().

Fix

Guard _update_cwd() on Windows so the cwd file read (which always contains POSIX paths from Git Bash) cannot overwrite the already-correct self.cwd. The marker stripping (_extract_cwd_from_output()) still runs to keep output clean, but self.cwd is restored to its saved value afterward.

The _IS_WINDOWS variable is already defined at module level and used throughout the file in _find_bash(), _kill_process(), and _run_bash().

No non-Windows code paths are touched.

Relationship to existing work

  • Commit 4c136288 ("fix(local): respect configured cwd in init_session()") added cwd=self.cwd to subprocess.Popen(), which correctly fixes the init_session() startup directory. This PR complements it by also protecting against _update_cwd() overwriting self.cwd on subsequent commands after the first one succeeds.

  • PR fix(windows): native Git Bash terminal — exit 126 with empty output #14644 addresses the broader "exit 126 with empty output" issue on Windows (select.select, get_temp_dir, path conversion). This PR takes a simpler, complementary approach focused solely on the POSIX path overwrite in _update_cwd().

Tested

On Windows 11 + Git for Windows 2.42.0:

  • echo hello -> {output: "hello", exit_code: 0}
  • pwd -> {output: "/c/Users/user/project", exit_code: 0} (POSIX visible to user, but self.cwd stays valid for subsequent Popen)
  • cd /tmp && pwd -> cwd persists correctly
  • 10+ consecutive terminal commands all succeed, no WinError 267
  • TERMINAL_CWD=D:\project in .env preserved across all commands

Ref: #14644

…t Bash POSIX path

LocalEnvironment._update_cwd() reads the cwd from a temp file written by
Git Bash's pwd -P command. Git Bash always outputs POSIX-style paths
(e.g. /d/djh/hermes), which are invalid for subprocess.Popen(cwd=...) on
Windows, causing WinError 267 on all subsequent terminal commands.

Guard _update_cwd() on Windows so the cwd file read cannot overwrite the
already-correct self.cwd. The marker stripping (_extract_cwd_from_output)
still runs to keep output clean, but self.cwd is restored afterward.

The _IS_WINDOWS variable was already defined at module level and used
throughout the file in _find_bash(), _kill_process(), and _run_bash().

Ref: NousResearch#14644
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management backend/local Local shell execution labels Apr 27, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the contribution @hypen-o! The Windows / Git Bash POSIX-path issue is real and worth fixing, but the diff is 417/-408 across one file — that's CRLF line-ending noise from saving the file with Windows line endings. Could you resubmit with LF line endings? Set git config --global core.autocrlf false in your local clone, re-checkout the file, then reapply just the _update_cwd() change. Closing this for now; happy to merge a clean resubmit.

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

Labels

backend/local Local shell execution P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants