Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tartley opened this issue Nov 8, 2023 · 4 comments
Closed

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

tartley opened this issue Nov 8, 2023 · 4 comments
Labels
T: bug Something isn't working

Comments

@tartley
Copy link

tartley commented Nov 8, 2023

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
@tartley tartley added the T: bug Something isn't working label Nov 8, 2023
@JelleZijlstra
Copy link
Collaborator

Thanks for the report! cc @yilei

@yilei
Copy link
Contributor

yilei commented 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:

$ 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" )

@JelleZijlstra
Copy link
Collaborator

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

@tartley
Copy link
Author

tartley commented Nov 8, 2023

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
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants
@tartley @yilei @JelleZijlstra and others