fix: handle read-only .git files in deploy cleanup on Windows#4719
Open
OiPunk wants to merge 1 commit intogoogle:mainfrom
Open
fix: handle read-only .git files in deploy cleanup on Windows#4719OiPunk wants to merge 1 commit intogoogle:mainfrom
OiPunk wants to merge 1 commit intogoogle:mainfrom
Conversation
… cleanup Replace all bare shutil.rmtree() calls in cli_deploy.py with a _robust_rmtree() helper that clears the read-only bit before retrying deletion on Windows. This prevents PermissionError ([WinError 5]) when cleaning up .git/objects after deployment. The fix is compatible with Python 3.10+ (uses onerror for <3.12 and onexc for >=3.12).
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
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
Fixes #4635
On Windows, files inside
.git/objects/are marked read-only by default. Whenadk deploycleans up the temporary directory in itsfinallyblock,shutil.rmtree()raisesPermissionError: [WinError 5] Access deniedon these files, causing the CLI to crash even after a successful deployment.This PR introduces a
_robust_rmtree()helper that passes an error handler toshutil.rmtree. On Windows, the handler clears the read-only bit (os.chmod(path, stat.S_IWRITE)) and retries the deletion. On non-Windows platforms,shutil.rmtreeis called without any handler. The fix usesonexcfor Python >= 3.12 andonerrorfor Python < 3.12 to avoid deprecation warnings.All six
shutil.rmtree()call sites incli_deploy.py(to_cloud_run,to_agent_engine,to_gke) are updated.Test Plan
TestRobustRmtreewith three unit tests:test_removes_directory_tree-- verifies normal directory removaltest_removes_readonly_files-- verifies removal of trees containing read-only filestest_on_rm_error_clears_readonly_and_retries-- verifies the error handler chmod + retry logicpytest tests/unittests/cli/utils/test_cli_deploy.py