pypa / pyproject-hooks

A low-level library for calling build-backends in `pyproject.toml`-based project
https://pyproject-hooks.readthedocs.io/
MIT License
122 stars 49 forks source link

Handle hooks calling sys.exit() on success #176

Closed takluyver closed 1 year ago

takluyver commented 1 year ago

E.g. setuptools' hooks call setup.py in process, and the code in there may call sys.exit() even when there's no error. Maybe setuptools should catch this, but the spec doesn't say, and catching it here can protect against any similar cases from other backends.

Code calling os._exit(0), which bypasses any Python handling, will still fail, but if anyone's doing that, they can deal with the consequences themselves.

@pfmoore suggested catching all errors in the hooks and putting the traceback in the JSON. That's not unthinkable, but we already have a way to handle such cases - the frontend gets a CalledProcessError (or a similar exception from its custom subprocess runner) and should ensure that at least stderr from the backend is available somewhere for debugging. This is valuable because it also covers errors that aren't Python exceptions (e.g. segfaults), and may have additional context besides the traceback, like logged messages.

takluyver commented 1 year ago

I realised this doesn't work after all, because all the hooks are specified as returning something if defined, and they can't do that with an uncaught exception bubbling up. It will have to be caught in setuptools.