Skip to content

[BUG] build_scripts.py incorrectly handles shebang lines with DOS line endings #3129

@amalachow

Description

@amalachow

setuptools version

setuptools 60.9.3

Python version

Python 3.8.10

OS

NT-10.0-19042

Additional environment information

No response

Description

shebang using DOS line endings causes the shebang regular expression to fail resulting in redirection to the python interpreter used for the installed script to not behave as expected.

Expected behavior

The shebang is rewritten consistently for UNIX and DOS line endings.

How to Reproduce

#!/bin/bash -eu
printf "[[System Info]]\n"
uname -srvmo
python3 --version
python3 -m virtualenv --version
rm -rf .venv
python3 -m virtualenv --quiet -p python3 .venv
.venv/Scripts/python -m pip install --quiet --upgrade pip &> /dev/null
.venv/Scripts/python -m pip install --quiet --upgrade setuptools &> /dev/null
.venv/Scripts/python -m pip list

rm -rf example
mkdir example
cat > example/example.py << END
#!/usr/bin/env python3

def main():
    print("Example script")

if __name__=="__main__":
    main()
END

cat > example/setup.py << END
from setuptools import setup
setup(name="example", scripts=["example.py"])
END

printf "\n[[Expected behavior]]\n"
file example/example.py
.venv/Scripts/pip install --quiet ./example
head -1 .venv/Scripts/example.py

printf "\n[[Issue]]\n"
sed -ie 's/\r*$/\r/g' example/example.py
file example/example.py
.venv/Scripts/pip install --quiet -I ./example
head -1 .venv/Scripts/example.py

fix="--- a/build_scripts.py
+++ b/build_scripts.py
@@ -15 +15 @@
-first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
+first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?\r?$')"

printf "\n[[Proposed Fix]]\n"
echo "${fix}"
patch -u -p0 .venv/Lib/site-packages/setuptools/_distutils/command/build_scripts.py <<< ${fix}
file example/example.py
.venv/Scripts/pip install --quiet -I ./example
head -1 .venv/Scripts/example.py```

### Output

```console
[[System Info]]
MINGW64_NT-10.0-19042 3.1.7-340.x86_64 2021-03-26 22:17 UTC x86_64 Msys
Python 3.8.10
virtualenv 20.10.0 from C:\Python38\lib\site-packages\virtualenv\__init__.py
Package    Version
---------- -------
pip        22.0.3 
setuptools 60.9.3 
wheel      0.37.1 

[[Expected Behavior]]
example/example.py: Python script, ASCII text executable
#!C:\setuptools-issue\.venv\Scripts\python.exe

[[Issue]]
example/example.py: Python script, ASCII text executable, with CRLF line terminators
#!/usr/bin/env python3

[[Proposed Fix]]
--- a/build_scripts.py
+++ b/build_scripts.py
@@ -15 +15 @@
-first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')   
+first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?\r?$')
patching file .venv/Lib/site-packages/setuptools/_distutils/command/build_scripts.py
example/example.py: Python script, ASCII text executable, with CRLF line terminators
#!C:\setuptools-issue\.venv\Scripts\python.exe

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions