Skip to content

Instantly share code, notes, and snippets.

@ekfeet
Last active September 12, 2024 08:07
Show Gist options
  • Save ekfeet/a1d79264214734df7a0182787b48100f to your computer and use it in GitHub Desktop.
Save ekfeet/a1d79264214734df7a0182787b48100f to your computer and use it in GitHub Desktop.
Bug Report - py_pex_binary
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_pex_binary")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
compile_pip_requirements(
name = "requirements",
src = "requirements.in",
requirements_txt = "requirements_lock.txt",
)
py_binary(
name = "main",
srcs = ["main.py"],
deps = ["@pypi//isodate"]
)
py_pex_binary(
name = "main_pex",
binary = ":main"
)
import isodate
def main():
print(isodate)
if __name__ == "__main__":
main()
# https://github.com/bazelbuild/rules_python/releases/
bazel_dep(name = "rules_python", version = "0.35.0")
# https://github.com/aspect-build/rules_py/releases
bazel_dep(name = "aspect_rules_py", version = "0.8.0")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
configure_coverage_tool = True,
python_version = "3.11",
)
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pypi",
python_version = "3.11",
requirements_lock = "//:requirements_lock.txt",
)
use_repo(pip, "pypi")
# run `bazel run //:requirements.update`
diff --git a/py/private/py_pex_binary.bzl b/py/private/py_pex_binary.bzl
index faf8081..ec144ab 100644
--- a/py/private/py_pex_binary.bzl
+++ b/py/private/py_pex_binary.bzl
@@ -47,14 +47,17 @@ def _map_srcs(f, workspace):
#
# No Match: `external/rules_python~~pip~pypi_39_rtoml/site-packages/rtoml-0.11.0/src/mod/parse.py`
# Reason: It has three `/` after first `site-packages` substring.
- if site_packages_i != -1 and f.path.count("/", site_packages_i) == 2:
- if f.path.find("dist-info", site_packages_i) != -1:
- return ["--distinfo={}".format(f.dirname)]
- return ["--dep={}".format(f.dirname)]
+ if site_packages_i != -1:
+ if f.path.count("/", site_packages_i) == 1 and f.basename != "__init__.py":
+ return ["--dep={}".format(f.dirname)]
+ if f.path.count("/", site_packages_i) == 2:
+ if f.path.find("dist-info", site_packages_i) != -1:
+ return ["--distinfo={}".format(f.dirname)]
+ return ["--dep={}".format(f.dirname)]
# If the path does not have a `site-packages` in it, then put it into
# the standard runfiles tree.
- elif site_packages_i == -1:
+ else:
return ["--source={}={}".format(f.path, dest_path)]
return []
@@ -156,4 +159,4 @@ py_pex_binary = rule(
PY_TOOLCHAIN
],
executable = True,
-)
\ No newline at end of file
+)
diff --git a/py/tools/pex/main.py b/py/tools/pex/main.py
index 4598109..b12770b 100644
--- a/py/tools/pex/main.py
+++ b/py/tools/pex/main.py
@@ -159,7 +159,9 @@ pex_info.interpreter_constraints = [
]
for dep in options.dependencies:
- dist = Distribution.load(dep + "/../")
+ if dep.split("/")[-1] != "site-packages":
+ dep += "/../"
+ dist = Distribution.load(dep)
# TODO: explain which level of inferno is this!
key = "%s-%s" % (dist.key, dist.version)
@ekfeet
Copy link
Author

ekfeet commented Sep 12, 2024

the fix should probably also check f.path.endswith(".py") in the case where the "/" count is 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment