macisamuele / language-formatters-pre-commit-hooks

Collection of custom pre-commit hooks.
Apache License 2.0
115 stars 58 forks source link

Error when using pretty-format-golang: urllib3 v2.0, OpenSSL 1.1.1+, LibreSSL 2.8.3 #159

Closed raulmeli closed 1 year ago

raulmeli commented 1 year ago

The problem

Hello, when trying to use this hook I'm getting the following error:

Go Formatter.............................................................Failed
- hook id: pretty-format-golang
- exit code: 1
Traceback (most recent call last):
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/bin/pretty-format-golang", line 5, in <module>
    from language_formatters_pre_commit_hooks.pretty_format_golang import pretty_format_golang
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/lib/python3.9/site-packages/language_formatters_pre_commit_hooks/pretty_format_golang.py", line 6, in <module>
    from language_formatters_pre_commit_hooks.pre_conditions import golang_required
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/lib/python3.9/site-packages/language_formatters_pre_commit_hooks/pre_conditions.py", line 9, in <module>
    from language_formatters_pre_commit_hooks.utils import run_command
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/lib/python3.9/site-packages/language_formatters_pre_commit_hooks/utils.py", line 10, in <module>
    import requests
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/lib/python3.9/site-packages/urllib3/__init__.py", line 38, in <module>
    raise ImportError(
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with LibreSSL 2.8.3. See: https://github.com/urllib3/urllib3/issues/2168
golangci-lint............................................................Passed

The error seems to be thrown by the virtual env of the repo, and not my local Python version, but I could be misunderstanding that.

Tried but didn't work

So far I tried:

My setup/environment

.pre-commit-config.yaml

repos:
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: v2.3.0
    hooks:
      - id: pretty-format-golang
        args:
          - --autofix
  - repo: https://github.com/golangci/golangci-lint
    rev: v1.44.2
    hooks:
      - id: golangci-lint
        entry: golangci-lint run
        args:
          - --max-issues-per-linter=0
          - --max-same-issues=0
          - --config=.code_quality/.golangci.yml
          - --new-from-rev=HEAD~1 # comment this arg if you want to run pre-commit run --all-files

I also tried with the version 2.8.0 of this repo (and deleted the cache of pre-commit) and nothing changed, same error.

raulmeli commented 1 year ago

Maybe not ideal but I fixed it by doing these steps:

  1. Activate the virtual env of the repo in the pre-commit cache: source /Users/myuser/.cache/pre-commit/repofaj4nkze/py_env-python3/bin/activate
  2. Install the latest 1.x version of Urllib (v2.0 drops support for OpenSSL<1.1.1): pip install urllib3==1.26.15
  3. Done. Deactivate the virtual env: deactivate
macisamuele commented 1 year ago

I'm surprised about the reported issue, haven't looked yet on ways to reproduce.

The suggested workarkund is good but too manual, I would suggest to check https://pre-commit.com/#config-additional_dependencies as additional_dependencies would allow to add the restriction of urllib3<2