fix(py_pex_binary): exclude hermetic python toolchain files to prevent AmbiguousDistributionError#930
fix(py_pex_binary): exclude hermetic python toolchain files to prevent AmbiguousDistributionError#930xangcastle wants to merge 4 commits intomainfrom
Conversation
| # these will match in bzlmod setup with --incompatible_use_plus_in_repo_names flag flipped. | ||
| "rules_python++python+", | ||
| "aspect_rules_py+/py/tools/", | ||
| "python_interpreters+", |
There was a problem hiding this comment.
Don't we need the ~ version as well?
There was a problem hiding this comment.
Also I think if CI passed then either this line is not needed or it is not being tested...
| if dest_path.find(exclude) != -1: | ||
| return [] | ||
|
|
||
| if "_distutils_hack" in f.path: |
| load("//py:defs.bzl", "py_binary", "py_pex_binary") | ||
|
|
||
| # Test that both single-file modules (six) and multi-file modules (cowsay) work with py_pex_binary. | ||
| # Also tests that py_pex_binary works with hermetic Python toolchains (python_version = 3.11). |
There was a problem hiding this comment.
Can that be a separate test? This is replacing an old test, not "also" testing something...
|


Problem
When building a
py_pex_binaryfrom apy_binarythat uses a hermetic Python toolchain (e.g., from rules_python), the build fails with:This happens because the hermetic Python interpreter includes pip and setuptools in its
site-packages, andpy_pex_binarywas incorrectly including these as dependencies. WhenDistribution.load()is called on thesite-packagesdirectory, it finds multiple.dist-infodirectories and raises an error.Solution
Exclude hermetic Python toolchain files from being processed as PEX dependencies:
Added exclusion pattern for the hermetic interpreter repository:
python_interpreters+(matches repos likeaspect_rules_py++python_interpreters+python_3_11_aarch64_apple_darwin)Added specific file exclusions for internal toolchain files:
_distutils_hack- Part of the interpreter's distutils shimdistutils-precedence.pth- Interpreter configuration fileChanges
py/private/py_pex_binary.bzl: Added exclusion patterns and specific file filterspy/tests/py-pex-binary/BUILD.bazel: Updated test to use hermetic Python 3.11 toolchain to validate the fixTesting
bazel test //py/tests/py-pex-binary:test__print_modules_pex
//py/tests/py-pex-binary:test__print_modules_pex PASSED