Skip to content

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

Closed

Description

@tartley

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

  1. JelleZijlstra commented on Nov 8, 2023

    @JelleZijlstra
    Collaborator

    Thanks for the report! cc @yilei

  2. yilei commented on Nov 8, 2023

    @yilei
    Contributor

    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:

    $ cat ranges.py
    print ( "format m1" )
    print ( "format m2" )
    print ( "format m3" )
    print ( "format m4" )
    print ( "format m5" )
    $ black --line-ranges=2-3 ranges.py
    reformatted ranges.py
    
    All done! ✨ 🍰 ✨
    1 file reformatted.
    $ cat ranges.py
    print ( "format m1" )
    print("format m2")
    print("format m3")
    print ( "format m4" )
    print ( "format m5" )
    
  3. JelleZijlstra commented on Nov 8, 2023

    @JelleZijlstra
    Collaborator

    Probably an issue with the diffing algorithm in adjusted_lines()?

  4. tartley commented on Nov 8, 2023

    @tartley
    Author

    Ah! Thank you for untangling my misdiagnosis.

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

Metadata

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