Skip to content

fix(toolchains): exclude pycache symlinks from runtime files - #4233

Merged
rickeylev merged 2 commits into
bazel-contrib:mainfrom
stevendee-recon:pycache-root
Oct 13, 2026
Merged

rickeylev merged 2 commits into
bazel-contrib:mainfrom
stevendee-recon:pycache-root

Conversation

@stevendee-recon

@stevendee-recon stevendee-recon commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

The runtime's lib/** glob matches the __pycache__ symlinks added in #3661, so they end up in runfiles.

With RULES_PYTHON_PYCACHE_DIR=/dev/null they are symlinks to a character device, and packaging a binary into a tar layer fails (#4198). The glob now excludes __pycache__ and everything under it.

The pyc cache is now keyed on the runtime archive's sha256 rather than a hash of the workspace root. The sources in an archive all share one mtime, so Python's mtime-and-size check can't tell a patch release's sources from the previous one's. The workspace hash didn't keep workspaces apart anyway: the repository contents cache shares one fetched runtime, symlinks included, across workspaces.

The default root stops trying locations that are likely to be shared and instead tries a more reliably user-owned fallback: if XDG_CACHE_HOME is unset, then on non-Windows, $HOME/.cache is used (per XDG). On Windows, %LOCALAPPDATA% is used. (NB. platformdirs would choose rules_python\Cache rather than rules_python_pycache; the current change opts for one directory name everywhere.)

Fixes #4198

The runtime's `lib/**` glob matches the `__pycache__` symlinks added in
bazel-contrib#3661, so they end up in runfiles.

With `RULES_PYTHON_PYCACHE_DIR=/dev/null` they are symlinks to a
character device, and packaging a binary into a tar layer fails (bazel-contrib#4198).
The glob now excludes `__pycache__` and everything under it.

The pyc cache is now keyed on the runtime archive's sha256 rather than a
hash of the workspace root. The sources in an archive all share one
mtime, so Python's mtime-and-size check can't tell a patch release's
sources from the previous one's. The workspace hash didn't keep
workspaces apart anyway: the repository contents cache shares one
fetched runtime, symlinks included, across workspaces.

The default root stops trying locations that are likely to be shared and
instead tries a more reliably user-owned fallback: if `XDG_CACHE_HOME`
is unset, then on non-Windows, `$HOME/.cache` is used (per [XDG][0]).
On Windows, `%LOCALAPPDATA%` is used. (NB. [`platformdirs`][1] would
choose `rules_python\Cache` rather than `rules_python_pycache`; the
current change opts for one directory name everywhere.)

[0]: https://specifications.freedesktop.org/basedir/latest/#variables
[1]: https://platformdirs.readthedocs.io/en/latest/
@stevendee-recon

Copy link
Copy Markdown
Contributor Author

@rickeylev @aignas I have signed the CLA - let me know if you need the email address on this commit to match or something.

@rickeylev rickeylev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR!

Comment thread python/private/python_repository.bzl Outdated
Comment thread python/private/python_repository.bzl Outdated
Comment thread python/private/python_repository.bzl Outdated
Comment thread python/private/python_repository.bzl
Comment thread python/private/python_repository.bzl Outdated
Comment thread python/private/python_repository.bzl
Comment thread python/private/python_repository.bzl Outdated
Comment thread python/private/python_repository.bzl
Key the pycache directory on a combined hash of the workspace root and
runtime identifier so different workspaces and runtime archives do not
collide, clarify comments in python_repository.bzl, and fix Sphinx MyST
directive nesting and issue link formatting in docs and news.
@rickeylev
rickeylev added this pull request to the merge queue Oct 13, 2026
Merged via the queue into bazel-contrib:main with commit 8c380e9 Oct 13, 2026
4 checks passed
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.

Ability to disable pycache symlinks

2 participants