Skip to content

Conversation

@RonnyPfannschmidt
Copy link
Member

Refactor _ensure_cache_dir_and_supporting_files to use a dedicated _make_cachedir function that atomically creates the cache directory with its supporting files (README.md, .gitignore, CACHEDIR.TAG).

  • Store all file contents as bytes in CACHEDIR_FILES dict
  • Use tempfile.mkdtemp + shutil.rmtree instead of TemporaryDirectory
  • Simplify cleanup logic for race conditions

Copilot AI review requested due to automatic review settings January 17, 2026 16:08
@RonnyPfannschmidt RonnyPfannschmidt added the skip news used on prs to opt out of the changelog requirement label Jan 17, 2026
Copy link

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 refactors the cache directory creation logic in cacheprovider.py by extracting it into a dedicated _make_cachedir function, simplifying the code structure while maintaining atomic creation and proper race condition handling.

Changes:

  • Consolidated file contents (README.md, .gitignore, CACHEDIR.TAG) into a single CACHEDIR_FILES dictionary with bytes values
  • Extracted cache directory creation logic into a new _make_cachedir function
  • Replaced tempfile.TemporaryDirectory context manager with tempfile.mkdtemp + shutil.rmtree for simpler cleanup

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

Refactor _ensure_cache_dir_and_supporting_files to use a dedicated
_make_cachedir function that atomically creates the cache directory
with its supporting files (README.md, .gitignore, CACHEDIR.TAG).

- Store all file contents as bytes in CACHEDIR_FILES dict
- Use tempfile.mkdtemp + shutil.rmtree instead of TemporaryDirectory
- Simplify cleanup logic for race conditions

Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
# gets "Directory not empty" from the rename. In this case,
# everything is handled so just continue after cleanup.
# On Windows, the error is a FileExistsError which translates to EEXIST.
shutil.rmtree(path, ignore_errors=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TemporaryDirectory does some extra stuff in its rmtree:

https://github.com/python/cpython/blob/63cc1257db468a368d64c0b968d203a0f4c7303a/Lib/tempfile.py#L917-L955

Perhaps it is prudent to keep using it?

Also I figure the TODO "pass ignore_cleanup_errors=True when we no longer support python < 3.10." is now applicable, maybe the TemporaryDirectory code is cleaner with that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i decided to go plain rmtree after assessing what we do and what TemporaryDirectory handles in addition and/or wrong
basically our goal is to make a directory , fill it with files, then push it in - so the extra errors it handles are not part of our flow, however we need to handle it breaking due to not supporting tunoff of delete in messy manners

just creating the directory, filling it in and renaming with rmtree on error seems way more simple/robust - no more working around TemporaryDirectory not fitting our use-case

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, LGTM.

The except BaseException case is not covered, would be nice to add a test for it but OK if it's too hard.

Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4.5 <claude@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants