Repository navigation
Closed
Labels
T: bugSomething isn't workingSomething isn't working
Description
Describe the bug
I can't get the recently-merged --line-ranges command line option to work at all. It either produces incorrect results, or produces an internal error.
To Reproduce
Five lines of input causes an internal error
(long lines of output, the error message & .log content, wrapped by me for readability)
$ cat unformatted.py
print ( "format me" )
print ( "format me" )
print ( "format me" )
print ( "format me" )
print ( "format me" )
$ black --line-ranges=2-3 unformatted.py
error: cannot format unformatted.py: INTERNAL ERROR: Black produced
different code on the second pass of the formatter. Please report a bug
on https://github.com/psf/black/issues. This diff might be helpful:
/tmp/blk_fn8p09n6.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ echo $?
123
$ cat /tmp/blk_fn8p09n6.log
Mode(target_versions=set(),
line_length=88,
string_normalization=True,
is_pyi=False,
is_ipynb=False,
skip_source_first_line=False,
magic_trailing_comma=True,
experimental_string_processing=False,
python_cell_magics=set(),
preview=False)
--- source
+++ first pass
@@ -1,5 +1,5 @@
print ( "format me" )
+print("format me")
+print("format me")
print ( "format me" )
-print ( "format me" )
-print ( "format me" )
-print ( "format me" )
+print("format me")
--- first pass
+++ second pass
@@ -1,5 +1,5 @@
print ( "format me" )
print("format me")
print("format me")
-print ( "format me" )
print("format me")
+print("format me")
4 lines of input completes, but formats the wrong line range
$ cat unformatted.py
print ( "format me" )
print ( "format me" )
print ( "format me" )
print ( "format me" )
$ black --line-ranges=2-3 unformatted.py
reformatted unformatted.py
All done! ✨ 🍰 ✨
1 file reformatted.
$ cat unformatted.py
print ( "format me" )
print("format me")
print("format me")
print("format me")Expected behavior
I expect only lines 2 and 3 to be formatted, but instead 2-4 have been formatted.
Environment
I'm using a version of Black cloned from Github's latest main:
$ black --version
black, 23.11.1.dev1+g58f31a7 (compiled: no)
Python (CPython) 3.10.12I installed it using pipx install -e . on a cloned source tree.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Pop
Description: Pop!_OS 22.04 LTS # derived from Ubuntu 22.04Additional context
I don't believe I have any active config.
$ ll ~/.config/black
ls: cannot access '/home/jhartley/.config/black': No such file or directoryMetadata
Metadata
Assignees
Labels
T: bugSomething isn't workingSomething isn't working
Activity
JelleZijlstra commentedon Nov 8, 2023
Thanks for the report! cc @yilei
yilei commentedon Nov 8, 2023
I'll investigate. On the surface this is a very strange bug and only reproducible if every line is the same. If I slightly change the content of the string for each line then it works:
JelleZijlstra commentedon Nov 8, 2023
Probably an issue with the diffing algorithm in
adjusted_lines()?tartley commentedon Nov 8, 2023
Ah! Thank you for untangling my misdiagnosis.