Skip to content

fix(toolchains): write regular __pycache__ files when pyc caching is disabled - #4264

Merged
rickeylev merged 1 commit into
bazel-contrib:mainfrom
rickeylev:dev_null_pycache_file
Oct 15, 2026
Merged

rickeylev merged 1 commit into
bazel-contrib:mainfrom
rickeylev:dev_null_pycache_file

Conversation

@rickeylev

Copy link
Copy Markdown
Collaborator

When RULES_PYTHON_PYCACHE_DIR is /dev/null (NUL on Windows) or a
pycache directory cannot be created, python_repository previously
symlinked __pycache__ to /dev/null. Those character-device symlinks
can behave unexpectedly when read or archived.

Instead, return None from _get_pycache_root when /dev/null or
NUL is specified and create empty regular __pycache__ files when no
pycache root is available, preventing Python from writing local .pyc
files in the runtime repository.

Fixes #4198

…disabled

When `RULES_PYTHON_PYCACHE_DIR` is `/dev/null` (`NUL` on Windows) or a
pycache directory cannot be created, `python_repository` previously
symlinked `__pycache__` to `/dev/null`. Those character-device symlinks
can behave unexpectedly when read or archived.

Instead, return `None` from `_get_pycache_root` when `/dev/null` or
`NUL` is specified and create empty regular `__pycache__` files when no
pycache root is available, preventing Python from writing local `.pyc`
files in the runtime repository.

Fixes bazel-contrib#4198
@rickeylev
rickeylev requested a review from aignas as a code owner October 14, 2026 06:53
@rickeylev
rickeylev enabled auto-merge October 14, 2026 06:56

@aignas aignas 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.

That's a somewhat elegant fix. We create files that glob won't match but also that forces Python to not create any files.

@rickeylev
rickeylev added this pull request to the merge queue Oct 14, 2026
Merged via the queue into bazel-contrib:main with commit d03bc77 Oct 15, 2026
5 checks passed
@rickeylev
rickeylev deleted the dev_null_pycache_file branch October 15, 2026 00:04
@rickeylev

Copy link
Copy Markdown
Collaborator Author

Yeah, I'm relatively happy with it.

Long term, I'd love to get rid of this problem by preventing Python from findings its underlying repo location in external/. Which I think will require setting PYTHONHOME somewhere, somehow.

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