aspect-build / aspect-build/rules_py

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

Open
#475 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Starlark
Stars
145
Forks
97
Avg merge
1d 1h
Merged PRs (30d)
71

Description

### 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

```shell
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
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)!
```

Contributor guide

Open the contributing guide

Research direction

Start with the py_pex_binary reproduction using a py_binary whose srcs and main include __main__.py, then inspect the PEX entry point shown in py/tools/pex/main.py and the reported ChrootTaggingException. Done means the example builds successfully without the __main__.py fileset conflict.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.