Skip to content

fix: catch NoSuchPathError in get_git_root()#4924

Open
VoidChecksum wants to merge 1 commit intoAider-AI:mainfrom
VoidChecksum:fix/catch-nosuchpatherror-in-get-git-root
Open

fix: catch NoSuchPathError in get_git_root()#4924
VoidChecksum wants to merge 1 commit intoAider-AI:mainfrom
VoidChecksum:fix/catch-nosuchpatherror-in-get-git-root

Conversation

@VoidChecksum
Copy link
Copy Markdown

Summary

  • Add git.NoSuchPathError to the exception handler in get_git_root() so paths containing special characters (like $$ on Windows) don't cause an uncaught exception crash
  • Add unit tests verifying get_git_root() returns None for both NoSuchPathError and InvalidGitRepositoryError

Root Cause

When a directory path contains $$ characters on Windows, GitPython's internal os.path.expandvars() silently corrupts the path (e.g., F:\$$data\python\detect_programs becomes F:\$data\python\detect_programs). The corrupted path doesn't exist on disk, so git.Repo() raises git.exc.NoSuchPathError. This exception was not in the except clause of get_git_root(), causing an uncaught exception crash at startup.

Fix

One-line change: add git.NoSuchPathError to the existing except tuple in get_git_root(), matching the existing pattern for git.InvalidGitRepositoryError and FileNotFoundError. This allows aider to gracefully fall back when the git repo path cannot be resolved.

Test plan

  • Added test_get_git_root_no_such_path_error - mocks git.Repo to raise NoSuchPathError, verifies get_git_root() returns None
  • Added test_get_git_root_invalid_repo - mocks git.Repo to raise InvalidGitRepositoryError, verifies existing behavior still works
  • Both tests pass locally

Fixes #2957

When a directory path contains special characters like `$$` on Windows,
GitPython's `os.path.expandvars()` silently corrupts the path (e.g.,
`F:\$$data` becomes `F:\$data`), causing `NoSuchPathError` to be raised
from `git.Repo()`. This exception was not caught, resulting in an
uncaught exception crash.

Add `git.NoSuchPathError` to the existing exception handler in
`get_git_root()` so it gracefully returns `None` like other git
errors, allowing aider to fall back to `--no-git` mode.

Fixes Aider-AI#2957
Copilot AI review requested due to automatic review settings March 15, 2026 19:55
Copy link
Copy Markdown

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

This PR hardens get_git_root() against git.exc.NoSuchPathError (notably triggered on Windows when GitPython corrupts paths containing $$) so aider can gracefully start up and proceed without a resolved git root.

Changes:

  • Catch git.NoSuchPathError in aider.main.get_git_root() and return None instead of crashing.
  • Add unit tests ensuring get_git_root() returns None for both NoSuchPathError and InvalidGitRepositoryError.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
aider/main.py Expands get_git_root() exception handling to include NoSuchPathError.
tests/basic/test_main.py Adds tests covering NoSuchPathError and InvalidGitRepositoryError behaviors for get_git_root().

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

You can also share your feedback on Copilot code review. Take the survey.

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.

Uncaught NoSuchPathError in base.py line 236

2 participants