feat: Add optional hermetic Python toolchain support #268
+152
−14
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.
Description
This PR adds an optional workaround for projects using hermetic Python toolchains that encounter
rules_pythonplaceholder expansion errors.Problem
Projects using hermetic Python toolchains (e.g., via
@python3_12_host//:pythonor similar custom hermetic interpreters) encounter errors when runningbazel run :refresh_compile_commands:Root Cause: The default
py_binaryimplementation relies onrules_python's template placeholder expansion mechanism. When projects specify custom hermetic Python interpreters, placeholders like%interpreter_args%and%stage2_bootstrap%may fail to expand correctly, preventing the tool from running.Solution
This PR introduces an optional
use_hermetic_python_workaroundparameter that enables an alternative implementation usingsh_binarywith a bash wrapper template instead ofpy_binary. This bypassesrules_python's templating entirely while maintaining full functionality.The traditional
py_binaryapproach remains the default for 100% backward compatibility.Usage
Projects experiencing hermetic Python issues can opt-in to the workaround:
Projects without hermetic Python issues continue working without any changes.
Implementation Details
New files:
refresh_wrapper.sh.template: Bash wrapper that locates and executes the Python script from Bazel's runfiles directory, bypassingrules_pythontemplatingModified files:
BUILD: Export the new template fileREADME.md: Document the new parameter, problem description, and solutionrefresh.template.py: Addif __name__ == "__main__": main()for direct executionrefresh_compile_commands.bzl:use_hermetic_python_workaroundboolean parameter (defaultFalse)_expand_wrapper_templaterule for template expansionsh_binarywhen workaround is enabledHow the workaround works:
py_binary, creates ansh_binarytargetrefresh_wrapper.sh.templatewith the Python script namerules_pythoninvolvementTesting
Successfully tested with Envoy proxy (~1200 C++ files, hermetic Python toolchain
@python3_12_host//:python):compile_commands.jsonsuccessfully (16MB, 1233 entries)Test branch: jlojosnegros/envoy:test/hedron-hermetic-python-integration
This branch demonstrates:
use_hermetic_python_workaround = TrueBackward Compatibility
100% backward compatible
use_hermetic_python_workaround = False)py_binarywithout modificationsTrade-offs
Pros:
Cons:
Related Issues
This PR addresses the root cause of:
Future Considerations
If
rules_pythonimproves hermetic toolchain support in the future, this workaround could be deprecated. However, it provides immediate value for projects blocked by the current limitation.Checklist
Additional Notes
This PR takes a conservative approach by:
I'm happy to make any adjustments based on maintainer feedback. Thank you for considering this contribution!