Skip to content

Zero length stdin input causes black to fail (regression) #2337

Description

@andyphelps

Describe the bug

Using latest version 21.6b0 piping an empty file into black as below causes "string index out of range". This is a regression from 21.5b2.

NOTE: referencing an empty file works as expected this is only when piping into - as example below.
i.e. this works:

touch __init__.py
python -m black __init__.py
All done! ✨ 🍰 ✨
1 file left unchanged.

To Reproduce

touch __init__.py
cat __init__.py | python -m black -
error: cannot format -: string index out of range
Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

Using stdin with empty file works as previous version without error.

Environment (please complete the following information):

  • Version:
  • OS and Python version:

Does this bug also happen on main?

Yes

Additional context

A bit of a tails case but we're using this approach within a CI script.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: configurationCLI and configurationF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.R: not a bugThis is deliberate behavior of Black.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions