Skip to content
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

rustfmt --check: handle writing to a broken pipe gracefully #6012

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bertschingert
Copy link

this addresses a panic when rustfmt --check makes a write() call that fails. There have been a few issues that already discussed this:

#5114
#2926

On one of the issues it was noted that it would be difficult to hit this outside of contrived situations like piping to "false", but I think it really is pretty easy to hit -- I got this panic by accidentally making a typo in "less", and piping to "les":

rustfmt --check prog.rs | les

With this patch I opted to leave the panic behavior in place for any write errors other than EPIPE, so that the tool's behavior is unchanged except in this specific situation. However, it could be preferable to take the opportunity to remove the panics entirely and handle all write errors gracefully. I can update the patch to not panic at all on write errors, if others agree.

The rustfmt tool panics when any writes to the output file descriptor
fail, whether sending color codes to the terminal, regular writes, or
flushing stdout.

It's fairly easy to hit this by accidentally writing to a broken pipe.
For example, I hit this when I tried to pipe into `less` but made a
typo:

$ rustfmt --check src/main.rs | les
panic

This patch updates rustfmt to handle BrokenPipe / EPIPE failures more
gracefully. Any failures lead to the program immediately exiting, since
there is no sense in continuing to write to a broken pipe, except for
the final flush() which doesn't need to exit early since it's already at
the end of program execution.

Any write errors aside from BrokenPipe still result in panic!(), since
any other errors are unexpected. This leaves the program behaving the
same as before for any other write errors.
@ytmimi
Copy link
Contributor

ytmimi commented Jan 25, 2024

Probably better not to panic!. Also, I think the tests are failing because you need to run rustfmt on itself 😅. The self_test makes sure that rustfmt is formatted properly.

Screen Shot 2024-01-24 at 9 53 01 PM

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

Successfully merging this pull request may close these issues.

3 participants