fix: catch NoSuchPathError in get_git_root()#4924
Open
VoidChecksum wants to merge 1 commit intoAider-AI:mainfrom
Open
fix: catch NoSuchPathError in get_git_root()#4924VoidChecksum wants to merge 1 commit intoAider-AI:mainfrom
VoidChecksum wants to merge 1 commit intoAider-AI:mainfrom
Conversation
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
There was a problem hiding this comment.
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.NoSuchPathErrorinaider.main.get_git_root()and returnNoneinstead of crashing. - Add unit tests ensuring
get_git_root()returnsNonefor bothNoSuchPathErrorandInvalidGitRepositoryError.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
git.NoSuchPathErrorto the exception handler inget_git_root()so paths containing special characters (like$$on Windows) don't cause an uncaught exception crashget_git_root()returnsNonefor bothNoSuchPathErrorandInvalidGitRepositoryErrorRoot Cause
When a directory path contains
$$characters on Windows, GitPython's internalos.path.expandvars()silently corrupts the path (e.g.,F:\$$data\python\detect_programsbecomesF:\$data\python\detect_programs). The corrupted path doesn't exist on disk, sogit.Repo()raisesgit.exc.NoSuchPathError. This exception was not in theexceptclause ofget_git_root(), causing an uncaught exception crash at startup.Fix
One-line change: add
git.NoSuchPathErrorto the existingexcepttuple inget_git_root(), matching the existing pattern forgit.InvalidGitRepositoryErrorandFileNotFoundError. This allows aider to gracefully fall back when the git repo path cannot be resolved.Test plan
test_get_git_root_no_such_path_error- mocksgit.Repoto raiseNoSuchPathError, verifiesget_git_root()returnsNonetest_get_git_root_invalid_repo- mocksgit.Repoto raiseInvalidGitRepositoryError, verifies existing behavior still worksFixes #2957