black icon indicating copy to clipboard operation
black copied to clipboard

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Open lukehodges opened this issue 3 years ago • 5 comments

Describe the bug

To Reproduce

Expected behavior

Environment (please complete the following information):

  • Version:
  • OS and Python version:

Does this bug also happen on main?

Additional context

simply running black run.py on my file brings up this error

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\resume\quantum-bot\venv\Scripts\black.exe\__main__.py", line 7, in <module>
  File "d:\resume\quantum-bot\venv\lib\site-packages\black\__init__.py", line 1283, in patched_main
    main()
  File "d:\resume\quantum-bot\venv\lib\site-packages\click\core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "d:\resume\quantum-bot\venv\lib\site-packages\click\core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "d:\resume\quantum-bot\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\resume\quantum-bot\venv\lib\site-packages\click\core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "d:\resume\quantum-bot\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "d:\resume\quantum-bot\venv\lib\site-packages\black\__init__.py", line 433, in main
    sources = get_sources(
  File "d:\resume\quantum-bot\venv\lib\site-packages\black\__init__.py", line 501, in get_sources
    gitignore = get_gitignore(root)
  File "d:\resume\quantum-bot\venv\lib\site-packages\black\files.py", line 125, in get_gitignore
    lines = gf.readlines()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)

lukehodges avatar Sep 17 '21 19:09 lukehodges

Looks like you have a gitignore file that contains invalid UTF-8.

JelleZijlstra avatar Sep 17 '21 19:09 JelleZijlstra

its just venv

lukehodges avatar Sep 17 '21 19:09 lukehodges

and the error still happens

lukehodges avatar Sep 17 '21 19:09 lukehodges

Anyone got some ideas or should i just close this

lukehodges avatar Dec 02 '21 09:12 lukehodges

run.py

@lukehodges what are the contents of run.py?

clavedeluna avatar Oct 22 '22 17:10 clavedeluna

no clue this was from ages ago

lukehodges avatar Oct 22 '22 20:10 lukehodges

@lukehodges I'd say this is worth closing then?

clavedeluna avatar Oct 23 '22 11:10 clavedeluna

Closing as there's no reproduction steps. If anyone runs into something similar, please open a new issue with full reproduction steps.

JelleZijlstra avatar Oct 23 '22 14:10 JelleZijlstra

@lukehodges I had the same error with using black on a directory with a .gitignore file that had */venv/* removing this resolved the issue.

ArriW avatar Dec 14 '22 23:12 ArriW

I had the same issue, my .gitignore file was edited by a windows app and was saved in "UTF-16 LE with BOM", converting it to UTF-8 worked like a charm.

The error is somewhat misleading however, as I (and I suspect most users) would look for the python file we tried to format, not the .gitignore file. Could we catch this error and report that we're trying to parse the .gitgnore file, rather than python source?

da1910 avatar May 10 '23 09:05 da1910