Skip to content

python: Generate console scripts for every entry point form in extract.py - #1660

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-extract-entry-points
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-extract-entry-points

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

For every prebuilt_python_library, prelude/python/tools/extract.py writes one script per console_scripts entry of the wheel, and it assumes each value is module:function. Entry points that use a dotted object reference (module:object.method) or extras, such as mcp = mcp.cli:app [cli] in mcp 1.29.0 and watchmedo = watchdog.watchmedo:main [watchmedo] in watchdog 6.0.0, get a script that fails with SyntaxError when run. A value without a colon fails the whole extract action, and script names are lowercased (MyTool becomes mytool).

Affected version

Checked on main at 4b47e14 and on the release 2026-11-15 (5b3b13d); extract.py is identical in both.

Reproduction

From a buck2 checkout in $BUCK2, in an empty directory:

python3 - <<'EOF'
import zipfile
with zipfile.ZipFile("demo-1.0-py3-none-any.whl", "w") as zf:
    zf.writestr("demo/__init__.py",
        "class _P:\n    def run(self):\n        print('program.run')\nprogram = _P()\n"
        "def main():\n    print('main')\n")
    zf.writestr("demo-1.0.dist-info/METADATA", "Name: demo\nVersion: 1.0\n")
    zf.writestr("demo-1.0.dist-info/entry_points.txt",
        "[console_scripts]\ninv = demo:program.run\nx = demo:main [cli]\nMyTool = demo:main\n")
EOF
python3 "$BUCK2/prelude/python/tools/extract.py" --output out --entry-points bin --entry-points-manifest bin.json demo-1.0-py3-none-any.whl
ls bin
for s in bin/*; do PYTHONPATH=out python3 "$s" 2>&1 | tail -1; done

Expected

Scripts inv, MyTool and x that print program.run, main and main, as pip installs them. The entry points specification (packaging.python.org, "Entry points specification", data model) defines the value as importable.module:object.attr with optional [extras].

Actual

inv
mytool
x
SyntaxError: invalid syntax
main
SyntaxError: invalid syntax

The generated script inv contains from demo import program.run, and x contains from demo import main [cli].

Analysis

extract.py splits the value with entry_point.split(":") and formats both parts into from {mod} import {func} and {func}(), so any dot or extras marker after the colon lands in the import statement, and a value without a colon raises ValueError. configparser.ConfigParser lowercases option names by default, which changes the script name.

Patch

The value is parsed with the object-reference pattern used by importlib.metadata.EntryPoint. The script imports the first name of the qualified name and calls the whole of it, as pip's scripts do, and extras are ignored. optionxform keeps the case of script names. A value without a callable is rejected with a message naming the entry, as pip rejects it ("A callable suffix is required").

Test Plan: new prelude/python/tools/tests/extract_test.py (target extract_test) builds a wheel with plain, dotted, extras and mixed-case entries, runs each generated script, and checks the error for a value without a callable. Both cases fail on the parent commit and pass with the change. With the change, the reproduction above lists MyTool and prints program.run, main, main.

@meta-cla

meta-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @phr4nky!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

…t.py

Summary:
`extract.py` writes a script for each `console_scripts` entry of a
prebuilt wheel and assumed every value is `module:function`:

- a dotted object reference such as `demo:program.run` produced
  `from demo import program.run`, a SyntaxError when the script runs;
- extras such as `mcp.cli:app [cli]` (mcp) or
  `watchdog.watchmedo:main [watchmedo]` (watchdog) produced
  `from mcp.cli import app [cli]`, the same SyntaxError;
- a value without a colon failed the whole extract action with
  `ValueError: not enough values to unpack`;
- configparser lowercased script names, so `MyTool` became `mytool`.

The value is now parsed with the object-reference pattern that
`importlib.metadata.EntryPoint` uses: the script imports the first
name of the qualified name and calls the whole of it, as pip's
scripts do, and extras are ignored. Script names keep their case. A
reference without a callable is rejected with a message naming the
entry, as pip rejects it.

Test Plan:
New `tests/extract_test.py` (target `extract_test`) builds a wheel with
plain, dotted, extras and mixed-case entries and runs each generated
script, and checks the error for a reference without a callable. Both
cases fail on the parent commit and pass with this change.
@phr4nky
phr4nky force-pushed the fix-extract-entry-points branch from d293346 to 24ef541 Compare November 25, 2026 21:48
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134594510. (Because this pull request was imported automatically, there will not be any future comments.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@meta-codesync meta-codesync Bot closed this in 4d81f76 Nov 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 4d81f76.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
…t.py

Summary:
For every `prebuilt_python_library`, `prelude/python/tools/extract.py` writes one script per `console_scripts` entry of the wheel, and it assumes each value is `module:function`. Entry points that use a dotted object reference (`module:object.method`) or extras, such as `mcp = mcp.cli:app [cli]` in mcp 1.29.0 and `watchmedo = watchdog.watchmedo:main [watchmedo]` in watchdog 6.0.0, get a script that fails with `SyntaxError` when run. A value without a colon fails the whole extract action, and script names are lowercased (`MyTool` becomes `mytool`).

## Affected version

Checked on `main` at 4b47e1430e and on the release `2026-11-15` (5b3b13d3ea); `extract.py` is identical in both.

## Reproduction

From a buck2 checkout in `$BUCK2`, in an empty directory:

```
python3 - <<'EOF'
import zipfile
with zipfile.ZipFile("demo-1.0-py3-none-any.whl", "w") as zf:
    zf.writestr("demo/__init__.py",
        "class _P:\n    def run(self):\n        print('program.run')\nprogram = _P()\n"
        "def main():\n    print('main')\n")
    zf.writestr("demo-1.0.dist-info/METADATA", "Name: demo\nVersion: 1.0\n")
    zf.writestr("demo-1.0.dist-info/entry_points.txt",
        "[console_scripts]\ninv = demo:program.run\nx = demo:main [cli]\nMyTool = demo:main\n")
EOF
python3 "$BUCK2/prelude/python/tools/extract.py" --output out --entry-points bin --entry-points-manifest bin.json demo-1.0-py3-none-any.whl
ls bin
for s in bin/*; do PYTHONPATH=out python3 "$s" 2>&1 | tail -1; done
```

## Expected

Scripts `inv`, `MyTool` and `x` that print `program.run`, `main` and `main`, as pip installs them. The entry points specification (packaging.python.org, "Entry points specification", data model) defines the value as `importable.module:object.attr` with optional `[extras]`.

## Actual

```
inv
mytool
x
SyntaxError: invalid syntax
main
SyntaxError: invalid syntax
```

The generated script inv contains `from demo import program.run`, and x contains `from demo import main [cli]`.

## Analysis

`extract.py` splits the value with `entry_point.split(":")` and formats both parts into `from {mod} import {func}` and `{func}()`, so any dot or extras marker after the colon lands in the import statement, and a value without a colon raises `ValueError`. `configparser.ConfigParser` lowercases option names by default, which changes the script name.

## Patch

The value is parsed with the object-reference pattern used by `importlib.metadata.EntryPoint`. The script imports the first name of the qualified name and calls the whole of it, as pip's scripts do, and extras are ignored. `optionxform` keeps the case of script names. A value without a callable is rejected with a message naming the entry, as pip rejects it ("A callable suffix is required").

X-link: meta/buck2#1660

Reviewed By: dtolnay

Differential Revision: D134594510

fbshipit-source-id: 11bc18a57c0e08c459beff0f832aa78d9086077f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants