black
black copied to clipboard
Have a warning when the user tries to reformat root.
Is your feature request related to a problem? Please describe.
The .
and /
characters are very close together, and this can lead to accidentally reformatting from the wrong directory.
Describe the solution you'd like
@ichard26 suggested to have a warning like with rm -rf /
, and I agree with this suggestion.
Describe alternatives you've considered
Additional context
Can I work on this? I think that one way to check if one of the sources (paths) passed to Black is root is checking the absolute path and compare it with its parent (aiming for a portable solution):
if src.resolve() == src.resolve().parent:
... # src is root
Then, adding a message and a proper flag (--format-root
? --accept-root
?) could complete this feature. Not sure if its necessary to compare src
to home and display a similar warning:
if src.resolve() == Path.home():
... # src is home
Also, I'm guessing that this should return an exit code 1.
I'd prefer warning on root than erroring out so we wouldn't have to add yet another flag to disable the safeguard. I'm not against erroring out though. Checking if home was passed wouldn't hurt assuming it isn't complicated.
I'd just be careful that the resolve()
calls don't accidentally crash Black. We're careful to make sure a resolve()
call erroring only causing the offending source entry to be skipped. https://github.com/psf/black/blob/d97b7898b34b67eb3c6839998920e17ac8c77908/src/black/files.py#L156-L182