Description
#3661 made python_repository write __pycache__ symlinks with targets that escape Bazel. Since the repository sets reproducible = True, these links get stored in the repository contents cache and shared. This can lead to surprises, as the symlink target validity varies by host system.
The recommended opt-out procedure is to set RULES_PYTHON_PYCACHE_DIR=/dev/null. But this winds up littering the repository cache with a bunch of symlinks to a character device, which, when processed by tar.bzl, become literal character devices and prevent any py_binary being packaged into a tar layer.
There should be a way to completely disable the #3661 functionality and get a repository without any __pycache__ symlinks at all.
Metadata
Metadata
Assignees
Labels
No labels
Activity
rickeylev commentedon Sep 29, 2026
Oh man, that'd be nice, but I haven't been able to find a nice way to tell the interpreter to stop creating files in its repo directory. Python loves reading through symlinks and finding its "true home" and putting pyc files there.
Are you using the (experimental) remote repo cache? I haven't seen issues with the local-disk repo cache.
I wonder if this can be fixed by writing a relative symlink. The repo directory has all the pycache files in it, they're just off in different directory to avoid the bazel bug mentioned in #3661. Or maybe a file named
__pycache__would short-circuit its logic and prevent it from writing.This sounds like a bug with tar.bzl
Stepping back a bit, that the interpreter's pycache files are even being processed is odd. The glob that matches files to include should be excluding them. Hm, maybe because, since
__pycache__, the ignore pyc glob pattern ends up matching the symlink itself?Do you have a repro?
stevendee-recon commentedon Sep 30, 2026
Repro here: https://github.com/stevendee-recon/python-repro
Re
PYTHONDONTWRITEBYTECODE- I think it'd be a good default to set when running in a Bazel sandbox, yeah. I have a local patch ofrules_pythonthat injects-B -Iinto the interpreter args before anything else; I have not thoroughly stress-tested it yet but have had no issues so far in a medium-sized (private, alas) monorepo.I'm not - this was a theoretical concern.
From my perspective at least, tar.bzl is behaving correctly here by refusing to put character devices in an OCI layer.
Yes, I think that's what's happening. However, I have other concerns with #3661 - sent privately.
stevendee-recon commentedon Oct 1, 2026
Fwiw another option is to set
PYTHONDONTWRITEBYTECODEin a.envrcfor a project that is using direnv / bazel_env. Then it should show up in the environment for any python, Bazel or otherwise, that gets started up, as long as the environment isn't reset at some point in the process. Not much forrules_pythonto do about that other than possibly mention it as a recommendation in documentation, I suppose.stevendee-recon commentedon Oct 12, 2026
@rickeylev I've submitted #4233, which fixes the
__pycache__ignore pattern you mentioned and also should mostly resolve the issues I mentioned by email (which alas I'm not sure ever made it through.)fix(toolchains): exclude pycache symlinks from runtime files (bazel-c…
rickeylev commentedon Oct 13, 2026
I think there's one remaining issue: if dev/null is specified, then create the pycache directories as regular files, which should prevent python from modifying anything in the repo
fix(toolchains): write regular __pycache__ files when pyc caching is …
build: Take the Python interpreter from the remote repo cache (#249)
chore(bazel): upgrade rules_python from 2.4.6 to 2.5.1 (dfinity#12158)