python-poetry / python-poetry/poetry

shebang for tool.poetry.scripts gets too long for scripts created for venv

Open
#6,837 3 comments 1 reaction 1 assignee View on GitHub

@Secrus is already working on this.

Since Oct 6, 2024.

area/scripts kind/bug status/triage
Dominant language
Python
Stars
34.3k
Forks
2.5k
Avg merge
2d 19h
Merged PRs (30d)
30

Description

  • Poetry version: 1.2.2
  • Python version: 3.8.10
  • OS version and name: Ubuntu 18.04
  • I am on the latest stable Poetry version, installed using a recommended method.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have consulted the FAQ and blog for any relevant entries or release notes.

Issue

Script files created for entries [tool.poetry.scripts] might contain shebang lines which are longer than ~128 bytes which will fail when executed.

Following will try to explain the situation:
In folder /var/jenkins_home/workspace/taf_dev/v4/ci/atc-dryrun/profiles_and_permanence venv is created and the full path to venv bin folder becomes:
/var/jenkins_home/workspace/taf_dev/v4/ci/atc-dryrun/profiles_and_permanence/.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/

Now the problematic script content is:

#!/var/jenkins_home/workspace/taf_dev/v4/ci/atc-dryrun/profiles_and_permanence/.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/python
import sys
from tasystem.wsb.wsbcta import main

if __name__ == '__main__':
    sys.exit(main())

Running the file will fail:

.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/system-wsb: line 2: import: command not found
.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/system-wsb: line 3: from: command not found
.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/system-wsb: line 6: syntax error near unexpected token `main'
.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/system-wsb: line 6: `    sys.exit(main())'

System tries to run that python content with bash (default in the system).

It seem that running above in Ubuntu 22.04 does not produce the same problem (not fully tested though).

But it seems that Poetry does have some kind of "protection" for this because all the other scripts created from dependencies are created differently. Bellow is an example of script created for one of the dependencies.

#!/bin/sh
'''exec' /var/jenkins_home/workspace/taf_dev/v4/ci/atc-dryrun/profiles_and_permanence/.poetry/venvs/ta-system-JbxFE9SW-py3.8/bin/python "$0" "$@"
' '''
# -*- coding: utf-8 -*-
import re
import sys
from cta.app.wsb.wsb import cli
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(cli())

Running that has no problems.

One more observation. If the shebang line will get under the limit (what ever that is) all created script files will have the form of

#!/var/jenkins_home/workspace/taf_dev/v4/ci/atc-dryrun/p_n_p/.poetry/venvs/ta-system-g0t6XxPn-py3.8/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from cta.app.wsb.wsb import cli
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(cli())

i.e. they will shebang pointing for the venv python rather than default shell.

So the problem is that scripts created for "local" project (in local pyproject.toml file) are not fixed for long shebang line the same way as scripts created from project dependencies.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.