jason-kane/PyYapf

Error with last PR #44

svartkanin opened this issue · 6 comments

I updated the package to the changes committed and receive the following error when using the command pipenv run yapf:

Traceback (most recent call last):
  File "/opt/sublime_text/sublime_plugin.py", line 1088, in run_
    return self.run(edit)
  File "~/.config/sublime-text-3/Packages/PyYapf Python Formatter/PyYapf.py", line 433, in run
    yapf.format(edit)
  File "~/.config/sublime-text-3/Packages/PyYapf Python Formatter/PyYapf.py", line 256, in format
    raise err
  File "~/.config/sublime-text-3/Packages/PyYapf Python Formatter/PyYapf.py", line 253, in format
    startupinfo=self.popen_startupinfo
  File "./python3.3/subprocess.py", line 819, in __init__
  File "./python3.3/subprocess.py", line 1448, in _execute_child
FileNotFoundError: [Errno 2] No such file or directory: 'pipenv'

I'm not entirely sure what the problem with subprocess.popen is, I've tried applying the shell=True flag as well which eliminates the error but doesn't perform the formatting.

Another question, is there a reason why this is using subprocess and not use yapf as a module instead https://github.com/google/yapf#example-as-a-module?
One could look for the style file in the cwd or parent and then apply it.
I have some time next week and could implement that if that's okay by the maintainers

I have tried the full path for pipenv now, the error is gone but the formatting doesn't work. The call
encoded_stdout, encoded_stderr = popen.communicate(encoded_text)
doesn't seem to return anything.

I'll take a look a the module option meanwhile, maybe I can get a decent version running

The main issue as I remember it is that in general you cannot format python 3 code when running yapf under python 2. This was a major problem since Sublime Text 2 used Python 2. Even if we decide to drop support for Sublime Text 2, there would still be the problem that if a bundled version of yapf would quickly get outdated, leading to further problems down the line (namely that your running pipenv run yapf on the command line will produce differently formatted source files than when formatting using PyYapf).

For those reasons, I feel we should first of all try to figure out why pipenv run yapf doesn't work for you. Is there anything we can do to help you debug the problem?

This got me curious, so I tried it (osx):

pip3 install pipenv
mkdir ~/test
cd ~/test
cp ~/holy_hell_I_found_it.py . # random python script for testing against
pipenv --python 3.7
pipenv install yapf

Uninstall and reinstall pyyapf (because osx is kind of funky about sublime package installs) to make sure i have the current version. note to self: include the version # in stdout when debug = true

Edit my user settings to set yapf_command to "/usr/local/bin/pipenv run yapf" and debug = true. Toss a sample python file in /test, open it in sublime 3, trigger yapf.

PyYapf: Encoding is not specified, falling back to default 'UTF-8'
PyYapf: Found yapf: /usr/local/bin/pipenv run yapf
PyYapf: Formatting selection (0, 1729)
PyYapf: Detected indent ''
PyYapf: Running ['/usr/local/bin/pipenv', 'run', 'yapf'] in /Users/jason/test
PyYapf: Exit code 0

Code is reformatted. Mess with the formatting and trigger it again. Perfect. Maybe it isn't really using my pipenv installed yapf? pip uninstall yapf to clear my non-pipenv yapf and try sublime again.. no problem.

I'm hopeful comparing my verification process to what you are doing will help? I hoped I would see some funny business with stdin/out from going through pipenv but it just worked.

Thanks for the info.

I actually managed to fix it now with a very simple change. The pipenv uses the click package which had issues with some environment variables

Traceback (most recent call last):
  File "~/.pyenv/versions/3.6.8/bin/pipenv", line 10, in <module>
    sys.exit(cli())
  File "~/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "~/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 696, in main
    _verify_python3_env()
  File "v/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pipenv/vendor/click/_unicodefun.py", line 124, in _verify_python3_env
    ' mitigation steps.' + extra
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/en/7.x/python3/ for mitigation steps.

Setting the following variables did the trick:

export LC_ALL=C.UTF-8
export LANG=C.UTF-8

Brilliant!