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

pex should error at build-time if entrypoint target doesn't exist #508

Closed
kwlzn opened this issue Jun 5, 2018 · 0 comments
Closed

pex should error at build-time if entrypoint target doesn't exist #508

kwlzn opened this issue Jun 5, 2018 · 0 comments
Assignees

Comments

@kwlzn
Copy link
Contributor

kwlzn commented Jun 5, 2018

current behavior is to let you build a pex that cannot execute:

[omerta ~]$ pex requests -e qqqqqqqqqqqq -o test.pex
[omerta ~]$ ./test.pex
Traceback (most recent call last):
  File ".bootstrap/_pex/pex.py", line 367, in execute
  File ".bootstrap/_pex/pex.py", line 293, in _wrap_coverage
  File ".bootstrap/_pex/pex.py", line 325, in _wrap_profiling
  File ".bootstrap/_pex/pex.py", line 410, in _execute
  File ".bootstrap/_pex/pex.py", line 468, in execute_entry
  File ".bootstrap/_pex/pex.py", line 473, in execute_module
  File "/Users/kwilson/Python/CPython-2.7.13/lib/python2.7/runpy.py", line 182, in run_module
    mod_name, loader, code, fname = _get_module_details(mod_name)
  File "/Users/kwilson/Python/CPython-2.7.13/lib/python2.7/runpy.py", line 107, in _get_module_details
    raise error(format(e))
ImportError: No module named qqqqqqqqqqqq

this is set in the PEXBuilder here: https://github.com/pantsbuild/pex/blob/master/pex/pex_builder.py#L227-L241

just above this, we do a similar check for "scripts" that exist in any distribution in the pex:
https://github.com/pantsbuild/pex/blob/master/pex/pex_builder.py#L216

using the finder utilities here:
https://github.com/pantsbuild/pex/blob/master/pex/finders.py#L269-L292

which use the pkg_resources.Distribution IResourceProvider interface here: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#iresourceprovider

to check for scripts inside of any distributions in the pex. it's possible we can use methods from this same interface to at a minimum validate the namespace of a given entrypoint in thirdparty deps. but validating that the entrypoint callable exists seems a little trickier - I think that may require importing, which could be tricky (in e.g. the case of a multi-platform pex.. and have side effects, etc).

one safer alternative there may be to load the source file as a resource and then ast.parse it - then walk the AST looking for matching FunctionDef (or other callable?) nodes: https://docs.python.org/3/library/ast.html#abstract-grammar (this may have limitations for multi-interpreter pexes tho).

we would also need to cover the "exists in raw sources in the pex" case (possibly by building up a synthetic Distribution around it and using the same approach)?

@kwlzn kwlzn removed the starter label Jun 21, 2018
@wisechengyi wisechengyi self-assigned this Jul 6, 2018
wisechengyi added a commit that referenced this issue Jul 23, 2018
Resolves #508

### Problem

Current behavior allows building a pex that cannot execute:

```
[omerta ~]$ pex requests -e qqqqqqqqqqqq -o test.pex
[omerta ~]$ ./test.pex
Traceback (most recent call last):
  File ".bootstrap/_pex/pex.py", line 367, in execute
  File ".bootstrap/_pex/pex.py", line 293, in _wrap_coverage
  File ".bootstrap/_pex/pex.py", line 325, in _wrap_profiling
  File ".bootstrap/_pex/pex.py", line 410, in _execute
  File ".bootstrap/_pex/pex.py", line 468, in execute_entry
  File ".bootstrap/_pex/pex.py", line 473, in execute_module
  File "/Users/kwilson/Python/CPython-2.7.13/lib/python2.7/runpy.py", line 182, in run_module
    mod_name, loader, code, fname = _get_module_details(mod_name)
  File "/Users/kwilson/Python/CPython-2.7.13/lib/python2.7/runpy.py", line 107, in _get_module_details
    raise error(format(e))
ImportError: No module named qqqqqqqqqqqq
```

### Solution

Verify entry point at build time. E.g. `a.b.c:m` means we will try to do `from a.b.c import m` in a separate process.

### Result

```
$ find hello
hello
hello/test.py
hello/tree.py

# Invalid module
$ python bin.py -D hello -e invalid.module  -o x.pex --validate-entry-point
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named invalid.module
Traceback (most recent call last):
  File "bin.py", line 758, in <module>
    main()
  File "bin.py", line 743, in main
    pex_builder.build(tmp_name, verify_entry_point=options.validate_ep)
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 529, in build
    self.freeze(bytecode_compile=bytecode_compile, verify_entry_point=verify_entry_point)
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 514, in freeze
    self._verify_entry_point()
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 263, in _verify_entry_point
    raise self.InvalidEntryPoint('Failed to:`{}`'.format(import_statement))
pex.pex_builder.InvalidEntryPoint: Failed to:`import invalid.module`

# invalid method
$ python bin.py -D hello -e test:invalid_method  -o x.pex --validate-entry-point
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name invalid_method
Traceback (most recent call last):
  File "bin.py", line 758, in <module>
    main()
  File "bin.py", line 743, in main
    pex_builder.build(tmp_name, verify_entry_point=options.validate_ep)
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 529, in build
    self.freeze(bytecode_compile=bytecode_compile, verify_entry_point=verify_entry_point)
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 514, in freeze
    self._verify_entry_point()
  File "/Users/yic/workspace/pex/pex/pex_builder.py", line 263, in _verify_entry_point
    raise self.InvalidEntryPoint('Failed to:`{}`'.format(import_statement))
pex.pex_builder.InvalidEntryPoint: Failed to:`from test import invalid_method`

# without the flag, invalid method still works
$ python bin.py -D hello -e test:invalid_method  -o x.pex 
```
@jsirois jsirois mentioned this issue Jul 27, 2018
jsirois added a commit to jsirois/pex that referenced this issue Aug 11, 2022
There was no proncipled reason to use it. Neither the originating commit
or its originiating issue refer to Pillow and the tests themselves don't
implicate anything being required except a 3rdparty lib.

See:
+ pex-tool#521
+ pex-tool#508
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants