Skip to content

--line-ranges bad behavior and internal errors #4033

Closed
@tartley

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.12

I 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.04

Additional context

I don't believe I have any active config.

$ ll ~/.config/black
ls: cannot access '/home/jhartley/.config/black': No such file or directory

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions