Skip to content

black in-place reformat wipes or corrupts target when disk is full #2479

Description

@qlyoung

Describe the bug

When performing an in-place reformat, if the disk is full and black cannot write to disk, instead of leaving the file untouched it wipes the entire file, or corrupts it.

To Reproduce

  1. Fill up the disk
  2. Attempt to reformat a target python file
  3. Observe:
python3 -m black target.py
error: cannot format target.py: [Errno 28] No space left on device
Oh no! \U0001f4a5 \U0001f494 \U0001f4a5
1 file failed to reformat.
  1. The file is now corrupt or in some cases completely wiped.

Example:

qlyoung@host ~> md5sum target.py
3053e7e42131f8a2295d07ab3f70442d  target.py

qlyoung@host ~> df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda         20G   19G   78M 100% /

qlyoung@host ~> cat /dev/zero > bingus
cat: write error: No space left on device
qlyoung@host ~> df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda         20G   19G     0 100% /

qlyoung@host ~> python3 -m black target.py
error: cannot format target.py: [Errno 28] No space left on device
Oh no! \U0001f4a5 \U0001f494 \U0001f4a5
1 file failed to reformat.

qlyoung@host ~> md5sum target.py
9640bcda965d5b519ae09ac7d3e87a91  target.py

In this case the end of the file was truncated. In the case that caused me to notice this bug, the entire file was wiped.

Expected behavior

The file should be left with its original contents

Environment (please complete the following information):

~> lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

~> python --version
Python 3.8.11

~> python -m black --version
__main__.py, version 20.8b1

Does this bug also happen on main?

I'm too sad to find out

Additional context

😢

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions