quaint-telephone-89068
12/09/2022, 6:09 PM./pants export currently follow Pex's default of generating "hermetic" console scripts, by rewriting all the shebangs of scripts under bin/ to pass -sE to python. This prevents the use of exported scripts with a custom PYTHONPATH, which breaks use-cases like running pylint with in-repo plugins and a custom source root.
Describe the solution you'd like
Pex 2.1.118 added a --non-hermetic-scripts option to the venv tool, which disables the addition of -sE to script shebangs. Pants could pass that flag when running the export logic.
This should definitely be safe to do when exporting a non-symlinked venv. I'm not sure if it would be safe to do in the symlink case, since the virtualenv might also be used for other processes within Pants. Maybe this should be an option on the export goal-subsystem, with the caveat that if you enable it you will get worse caching performance out of the symlink mode.
Describe alternatives you've considered
I thought about writing a wrapper script around ./pants export that re-re-writes the shebangs to remove -sE. I worry about the safety of doing this when using symlinked exports, since I'd be munging files within the Pants cache.
To avoid munging data in the pants cache, the wrapper script would need to do something like:
1. Run ./pants export with symlinking
2. Delete the top-level symlink under dist/export
3. Recreate the directory structure of a virtualenv under dist/export in place of the deleted link
4. Within the virtualenv skeleton, link back to everything in the Pants-cache venv except for bin/
5. Copy all the files from Pants-cache bin/ to the virtualenv under dist/export, rewriting shebangs along the way
I'm confident I could write such a script, but don't really want to if it turns out it's safe for Pants to always run with --non-hermetic-scripts 😅
Additional context
The pylint example might seem unrealistic since we'd typically want people to run ./pants lint, but it is important for IDE integrations. We have other custom tools that do wacky things with importlib that are broken in the same way.
pantsbuild/pants