Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"script" language without a shebang causes unexpected OSError #1350

Closed
chriselion opened this issue Mar 2, 2020 · 2 comments · Fixed by #1364
Closed

"script" language without a shebang causes unexpected OSError #1350

chriselion opened this issue Mar 2, 2020 · 2 comments · Fixed by #1364
Labels

Comments

@chriselion
Copy link

Repro steps:
Create a script hook.

-   repo: local
    hooks:
    -   id: foo
        name: foo
        language: script
        entry: foo.py
        types: [python]

foo.py can be empty.

If foo.py is not executable, pre-commit run foo --all-files produces a reasonable error message:

foo......................................................................Failed
- hook id: foo
- exit code: 1

Executable `/[redacted]/foo.py` is not executable

However, if it is executable but doesn't not contain a shebang, you get

$ pre-commit run foo --all-files
foo......................................................................An unexpected error has occurred: OSError: [Errno 8] Exec format error: '/[redacted]/foo.py'
Check the log at /[redacted]/.cache/pre-commit/pre-commit.log

Log contents:

version information

pre-commit version: 2.1.1
sys.version:
    3.7.6 (default, Dec 30 2019, 19:38:28) 
    [Clang 11.0.0 (clang-1100.0.33.16)]
sys.executable: /[redacted]/venv/bin/python3
os.name: posix
sys.platform: darwin

error information

An unexpected error has occurred: OSError: [Errno 8] Exec format error: '/[redacted]/foo.py'
Traceback (most recent call last):
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/error_handler.py", line 54, in error_handler
    yield
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/main.py", line 371, in main
    return run(args.config, store, args)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 339, in run
    return _run_hooks(config, hooks, args, environ)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 249, in _run_hooks
    verbose=args.verbose, use_color=args.color,
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 165, in _run_single_hook
    retcode, out = language.run_hook(hook, filenames, use_color)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/languages/script.py", line 19, in run_hook
    return helpers.run_xargs(hook, cmd, file_args, color=color)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/languages/helpers.py", line 109, in run_xargs
    return xargs(cmd, file_args, **kwargs)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/xargs.py", line 153, in xargs
    for proc_retcode, proc_out, _ in results:
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 598, in result_iterator
    yield fs.pop().result()
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
    return self.__get_result()
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/xargs.py", line 146, in run_cmd_partition
    *run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
  File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/util.py", line 208, in cmd_output_p
    proc = subprocess.Popen(cmd, **kwargs)
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/[redacted]/foo.py'

@asottile asottile added the bug label Mar 2, 2020
@chriselion
Copy link
Author

FWIW, I have identify==1.4.11 installed. here's what it returns for that file:

$ python -c "from identify.identify import parse_shebang_from_file; print(parse_shebang_from_file(\"foo.py\"))"
()

@asottile
Copy link
Member

this has landed in 2.2.0 -- thanks again for the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants