Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: py_pex_binary fails if binary target includes __main__.py #475

Open
AlexLamaison-UnlikelyAI opened this issue Dec 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@AlexLamaison-UnlikelyAI
Copy link

AlexLamaison-UnlikelyAI commented Dec 11, 2024

What happened?

A py_pex_binary will fail to build if the binary it's given includes a file called main.py. Unfortunately, this is a common pattern because Gazelle uses it as a signal to create a binary target from the file.

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 7.1.1

Version of the Aspect rules, or other relevant rules from your
WORKSPACE or MODULE.bazel file: v1.0.0

Language(s) and/or frameworks involved: Python 3.9, Bazel, Gazelle

How to reproduce

This is an example of the kind of binary target generated by Gazelle and the pex target using it:

py_binary(
    name = "gazelle_generated_main",
    srcs = ["__main__.py"],
    imports = [".."],
    main = "__main__.py",
)

py_pex_binary(
    name = "my_pex",
    binary = ":gazelle_generated_main",
)

This generates an error like:

Traceback (most recent call last):
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/aspect_rules_py/py/tools/pex/main.py", line 190, in <module>
    pex_builder.freeze(bytecode_compile=False)
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/pex_builder.py", line 692, in freeze
    self._prepare_code()
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/pex_builder.py", line 607, in _prepare_code
    self._chroot.write(
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/common.py", line 623, in write
    self._tag(dst, label, compress)
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/common.py", line 541, in _tag
    self._check_tag(fn, label, compress)
  File "/home/alex/.cache/bazel/_bazel_alex/5413ef0fb20af199ccb77fe9356d6bd5/sandbox/processwrapper-sandbox/1552/execroot/_main/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/common.py", line 521, in _check_tag
    raise self.ChrootTaggingException(
pex.common.Chroot.ChrootTaggingException: Trying to add __main__.py to fileset(main) but already in fileset(executable)!
@AlexLamaison-UnlikelyAI AlexLamaison-UnlikelyAI added the bug Something isn't working label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant