Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

[SUBLACK] Black did not run succesfully: Click was configured to use ASCII as encoding for the environment. #2

Open
niloch opened this issue May 10, 2018 · 19 comments
Labels
help wanted Extra attention is needed

Comments

@niloch
Copy link

niloch commented May 10, 2018

Operating system: macOS
Python version: 3.6
Black version: black, version 18.4a4
Sublime Text version: 3.0 build 3170

Sulbime Console output on save:

[SUBLACK] Black did not run succesfully: Traceback (most recent call last):
  File "/Users/my_user/.virtualenvs/my_project/bin/black", line 11, in <module>
    sys.exit(main())
  File "/Users/my_user/.virtualenvs/my_project/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/my_user/.virtualenvs/my_project/lib/python3.6/site-packages/click/core.py", line 676, in main
    _verify_python3_env()
  File "/Users/my_user/.virtualenvs/my_project/lib/python3.6/site-packages/click/_unicodefun.py", line 118, in _verify_python3_env
    'for mitigation steps.' + extra)
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment.  Consult http://click.pocoo.org/python3/for mitigation steps.

This system lists a couple of UTF-8 supporting locales that
you can pick from.  The following suitable locales where
discovered: af_ZA.UTF-8, am_ET.UTF-8, be_BY.UTF-8, bg_BG.UTF-8, ca_ES.UTF-8, cs_CZ.UTF-8, da_DK.UTF-8, de_AT.UTF-8, de_CH.UTF-8, de_DE.UTF-8, el_GR.UTF-8, en_AU.UTF-8, en_CA.UTF-8, en_GB.UTF-8, en_IE.UTF-8, en_NZ.UTF-8, en_US.UTF-8, es_ES.UTF-8, et_EE.UTF-8, eu_ES.UTF-8, fi_FI.UTF-8, fr_BE.UTF-8, fr_CA.UTF-8, fr_CH.UTF-8, fr_FR.UTF-8, he_IL.UTF-8, hr_HR.UTF-8, hu_HU.UTF-8, hy_AM.UTF-8, is_IS.UTF-8, it_CH.UTF-8, it_IT.UTF-8, ja_JP.UTF-8, kk_KZ.UTF-8, ko_KR.UTF-8, lt_LT.UTF-8, nl_BE.UTF-8, nl_NL.UTF-8, no_NO.UTF-8, pl_PL.UTF-8, pt_BR.UTF-8, pt_PT.UTF-8, ro_RO.UTF-8, ru_RU.UTF-8, sk_SK.UTF-8, sl_SI.UTF-8, sr_YU.UTF-8, sv_SE.UTF-8, tr_TR.UTF-8, uk_UA.UTF-8, zh_CN.UTF-8, zh_HK.UTF-8, zh_TW.UTF-8

Sublime Project settings:

{
    "settings": 
    {
        "sublack":
        {
            "black_command": "/Users/my_user/.virtualenvs/my_project/bin/black",

            // run black before saving document
            "on_save": true,

            // line length
            // uses black default, if not modified
            "line_length": null,
        }
    }
}
@nicokist
Copy link
Contributor

Does this possible fix work for you?

@jgirardet
Copy link
Owner

jgirardet commented May 11, 2018

@niloch did you test it in command line I'd like be sure it's not a black issue.

if it fixes the issue I'll be happy for a PR @nicokist

@niloch
Copy link
Author

niloch commented May 11, 2018

@jgirardet @nicokist Yep the fix_click_locale branch on your fork works correctly for me! Thanks a bunch! 👍

@jgirardet
Copy link
Owner

@nicokist @niloch
I merged the fix and it crashed by me because i'm not in the same locale as you.

I changed to : env["LC_ALL"] = ".".join(locale.getdefaultlocale())
It's ok for me

Is it still ok with you ?

@nicokist
Copy link
Contributor

I'm afraid not. Somehow on our systems the locale is None:

import locale
locale.getdefaultlocale()
(None, None)

Perhaps you could check for that and set LC_ALL only if locale.getdefaultlocale() == (None, None)?

@nicokist
Copy link
Contributor

nicokist commented May 11, 2018

I've checked, the following works for me:

if locale.getdefaultlocale() == (None, None):
    env["LC_ALL"] = "en_US.UTF-8"

@jgirardet
Copy link
Owner

If don't know if this fix will work if the user is not in "en_US.UTF-8".

We have to find a way to get the local under macos

which macos version do you use ? it seems it's an old python bug
https://bugs.python.org/issue18378

@jgirardet
Copy link
Owner

I did not find much about it.
I added a config option If a non US user expect the same bug but with a different locale:

lc = get_setting(self.view, "locale")
        if locale.getdefaultlocale() == (None, None):
            env["LC_ALL"] = lc or "en_US.UTF-8"

@nicokist
Copy link
Contributor

I'm on 10.13.4

If I use a non-english language I actually hit a crash earlier:

Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 1072, in run_
    return self.run(edit)
  File "/Users/nicokist/Library/Application Support/Sublime Text 3/Packages/sublack/sublack.py", line 115, in run
    Black(self.view)(edit)
  File "/Users/nicokist/Library/Application Support/Sublime Text 3/Packages/sublack/sublack.py", line 68, in __call__
    content = content.encode(encoding)
  File "./python3.3/encodings/undefined.py", line 19, in encode
UnicodeError: undefined encoding

probably because sublime is now getting confused about the encoding:

>>> view.encoding()
'Undefined'

but setting encoding = "UTF-8" rather than asking Sublime solves that. Does that work for you?

@nicokist
Copy link
Contributor

nicokist commented May 11, 2018

Your latest commit works for me.

I think the undefined encoding was due to me trying switching languages while testing using the same file, it didn't recur when I created new files.

@jgirardet
Copy link
Owner

after some reading :
click is aware of that : http://click.pocoo.org/5/python3/

there a pep about cross platform locale setting : https://www.python.org/dev/peps/pep-0538/ (python3.7)

The pep explains the best way to achieve this on osx is to set LC_CTYPE=UTF-8 in environ.
could you try it on your system (on sublime : preference->browse Package->sublack->sublack.py and then set line 74 :
env["LC_ALL"] = lc or "en_US.UTF-8" to env["LC_CTYPE"] = "UTF-8"

and tell me if it's good. thanks for help

@nicokist
Copy link
Contributor

        if locale.getdefaultlocale() == (None, None):
            env["LC_CTYPE"] = "UTF-8"

Still works.

@jgirardet
Copy link
Owner

ok to keep it simple I removed,the locale settings. If someone has same problem on other platform, I will deal with it in the code.
So for now it's OSX only patch :

if locale.getdefaultlocale() == (None, None):
            if platform.system() == "Darwin":
                env["LC_CTYPE"] = "UTF-8"

thank for help guys

@jgirardet
Copy link
Owner

It should be fixed now but I let It opened for now if other users encounter same problem.

@sylwekb
Copy link

sylwekb commented Jun 14, 2019

To anybody having this issue in 2019.
The sublack package is no longer just folder, it's a zip file with .sublime-package extension. To fix:

  1. change the extension
  2. unzip it
  3. edit main.py with the solution in comments above
  4. zip it back
  5. change the extension back to .sublime-package
  6. restart sublime.

@jgirardet jgirardet reopened this Jun 17, 2019
@jgirardet
Copy link
Owner

hi,
could you explain me more about it please

@sylwekb
Copy link

sylwekb commented Jun 17, 2019

hey

well I had the same problem with this error on max os

ValueError: unknown locale: UTF-8

To fix it I need to do the following in sublime console:

import os

os.environ['LC_ALL'] = 'en_US.UTF-8'
os.environ['LANG'] = 'en_US.UTF-8'

To make it permament, I needed to edit sublack code and I gave an instuction how to do that above.

@jgirardet
Copy link
Owner

Hi. thank for the details.
You aren't facing exactly the same bug.
Initially, this thread was about the case where NO local was set at all. this was fixed adding in utils.py

def get_env():
    # modifying the locale is necessary to keep the click library happy on OSX
    env = os.environ.copy()
    if locale.getdefaultlocale() == (None, None):
        if sublime.platform() == "osx":
            env["LC_CTYPE"] = "UTF-8"
return env

You have a different kind of trouble since your system seems to not be aware of UTF-8 but en_US.UTF-8.

I have 2 questions about it.

  • does setting only LC_CTYPE to en_US.UTF-8` would work on you system ?
  • could give me the full backtrace with logging set to debug so I could catch the exception at the good place and propose a fix ?

@jgirardet jgirardet added the help wanted Extra attention is needed label Feb 6, 2020
@StabbarN
Copy link

I had this problem and

import os
os.environ['LC_ALL'] = 'en_US.UTF-8'
os.environ['LANG'] = 'en_US.UTF-8'

solved it.

To solve it permanently I ran

set -xU LC_ALL en_US.UTF-8
set -xU LANG en_US.UTF-8

once in my fish shell. LC_ALL and LANG weren't set before.

Answers to your questions @jgirardet below

does setting only LC_CTYPE to en_US.UTF-8` would work on you system ?

No, it doesn't. I had $LC_CTYPE set to UTF-8 and changing it to en_US.UTF-8 doesn't help.

could give me the full backtrace with logging set to debug so I could catch the exception at the good place and propose a fix ?

[sublack:main.py](INFO) Loglevel set to DEBUG
[sublack:commands.py](DEBUG) running black_file
[sublack:blacker.py](DEBUG) config: {'black_on_save': True, 'black_fast': False, 'black_py36': None, 'black_log': 'debug', 'black_confirm_formatall': True, 'black_use_blackd': False, 'black_skip_string_normalization': False, 'black_target_version': ['py36'], 'black_command': 'black', 'black_blackd_autostart': False, 'black_line_length': None, 'black_default_encoding': 'utf-8', 'black_use_precommit': False, 'black_blackd_port': '45484', 'black_blackd_host': 'localhost'}
[sublack:blacker.py](DEBUG) encoding: UTF-8
[sublack:blacker.py](DEBUG) working dir: /Users/x/code/some-proj/utils
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 1088, in run_
    return self.run(edit)
  File "/Users/x/Library/Application Support/Sublime Text 3/Installed Packages/sublack.sublime-package/sublack/utils.py", line 53, in to_time
  File "/Users/x/Library/Application Support/Sublime Text 3/Installed Packages/sublack.sublime-package/sublack/commands.py", line 41, in run
  File "/Users/x/Library/Application Support/Sublime Text 3/Installed Packages/sublack.sublime-package/sublack/blacker.py", line 388, in __call__
  File "/Users/x/Library/Application Support/Sublime Text 3/Installed Packages/sublack.sublime-package/sublack/utils.py", line 199, in get_env
  File "./python3.3/locale.py", line 537, in getdefaultlocale
  File "./python3.3/locale.py", line 465, in _parse_localename
ValueError: unknown locale: UTF-8

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants