Skip to content

Commit

Permalink
Fix IndexError when shebang is just '#!/usr/bin/env'
Browse files Browse the repository at this point in the history
  • Loading branch information
harrymander committed Jul 1, 2024
1 parent 437ef92 commit 90c7a1e
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 90c7a1e

Please sign in to comment.