feat: Add optional hermetic Python toolchain support - #268
Open
jlojosnegros wants to merge 1 commit into
Open
Conversation
Add use_hermetic_python_workaround parameter to refresh_compile_commands() to enable projects using hermetic Python toolchains to work around rules_python placeholder expansion issues. Background: When projects use hermetic Python toolchains (e.g., via @python3_12_host or similar), rules_python's py_binary template placeholders (%interpreter_args%, %stage2_bootstrap%) may fail to expand correctly, causing errors like: python3: can't open file '%interpreter_args%' Solution: This commit adds an optional workaround that uses sh_binary with a bash wrapper template instead of py_binary, bypassing rules_python's templating entirely. The traditional py_binary approach remains the default for backward compatibility. Implementation: - Add use_hermetic_python_workaround boolean parameter (defaults to False) - Create refresh_wrapper.sh.template for sh_binary wrapper - Implement _expand_wrapper_template rule to expand the template - Conditionally use sh_binary when workaround is enabled - Add if __name__ == "__main__": main() to refresh.template.py - Document the new parameter and related issues in README Testing: Tested with Envoy proxy (1200+ C++ files) using @python3_12_host//:python. Successfully generated 16MB compile_commands.json with 1233 entries. check: https://github.com/jlojosnegros/envoy:test/hedron-hermetic-python-integration for more info Related issues: hedronvision#165, hedronvision#245, hedronvision#168 Signed-off-by: Jose Luis Ojosnegros Manchón <jl.ojosnegros.manchon@gmail.com>
|
is this repo still supported? this looks like a fairly obvious win |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!