Skip to content

Commit

Permalink
Merge pull request #468 from harrymander/fix-index-error-with-env-she…
Browse files Browse the repository at this point in the history
…bang

Fix IndexError when shebang is just '#!/usr/bin/env'
  • Loading branch information
asottile authored Jul 7, 2024
2 parents 0853fe3 + 90c7a1e commit 2e9f390
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions identify/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ def parse_shebang(bytesio: IO[bytes]) -> tuple[str, ...]:
return ()

cmd = tuple(_shebang_split(first_line.strip()))
if cmd and cmd[0] == '/usr/bin/env':
if cmd[1] == '-S':
cmd = cmd[2:]
else:
cmd = cmd[1:]
if cmd[:2] == ('/usr/bin/env', '-S'):
cmd = cmd[2:]
elif cmd[:1] == ('/usr/bin/env',):
cmd = cmd[1:]

if cmd == ('nix-shell',):
return _parse_nix_shebang(bytesio, cmd)

if cmd == ('nix-shell',):
return _parse_nix_shebang(bytesio, cmd)
return cmd


Expand Down
2 changes: 2 additions & 0 deletions tests/identify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ def test_file_is_text_does_not_exist(tmpdir):
(b'#!\x00\x00\x00\x00', ()),
# shebang lines with multiple arguments
(b'#!/usr/bin/env -S python -u', ('python', '-u')),
(b'#!/usr/bin/env', ()),
(b'#!/usr/bin/env -S', ()),
),
)
def test_parse_shebang(s, expected):
Expand Down

0 comments on commit 2e9f390

Please sign in to comment.