fix: use _rmtree for Windows-safe directory cleanup in downloader#299
Merged
danielmeppiel merged 1 commit intomainfrom Mar 14, 2026
Merged
fix: use _rmtree for Windows-safe directory cleanup in downloader#299danielmeppiel merged 1 commit intomainfrom
danielmeppiel merged 1 commit intomainfrom
Conversation
Replace bare shutil.rmtree() calls with the existing _rmtree() helper that handles read-only git pack files and Windows file locks. The helper was already defined but not used in two code paths (download_package and download_subdirectory_package). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the GitHub dependency downloader to use the existing Windows-safe _rmtree() helper instead of shutil.rmtree() when clearing non-empty target directories prior to downloading packages, addressing Windows PermissionError issues on read-only/locked .git pack files.
Changes:
- Replace
shutil.rmtree(target_path)with_rmtree(target_path)indownload_subdirectory_package(). - Replace
shutil.rmtree(target_path)with_rmtree(target_path)indownload_package().
Comments suppressed due to low confidence (1)
src/apm_cli/deps/github_downloader.py:1413
- _rmtree() can swallow PermissionError and may leave the directory partially intact (Windows fallback uses ignore_errors=True). Proceeding to mkdir() and then cloning into target_path risks mixing old and new contents without surfacing an error. Suggest making the cleanup strict here (post-check that target_path is removed/empty, or have _rmtree raise on failure after retries) so installs don't silently succeed with stale files.
if target_path.exists() and any(target_path.iterdir()):
_rmtree(target_path)
target_path.mkdir(parents=True, exist_ok=True)
| # If target exists and has content, remove it | ||
| if target_path.exists() and any(target_path.iterdir()): | ||
| shutil.rmtree(target_path) | ||
| _rmtree(target_path) |
| # If directory already exists and has content, remove it | ||
| if target_path.exists() and any(target_path.iterdir()): | ||
| shutil.rmtree(target_path) | ||
| _rmtree(target_path) |
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.
Replace bare
shutil.rmtree()with existing_rmtree()helper that handles read-only git pack files and Windows file locks. Two code paths indownload_packageanddownload_subdirectory_packagewere using the raw call instead of the helper that was already defined for this exact purpose.Fixes
PermissionError: [WinError 5] Access is deniedon.git/objects/pack/*.idxfiles during dependency update on Windows.