erlfmt icon indicating copy to clipboard operation
erlfmt copied to clipboard

Standalone `erlfmt` executable should use exit code 1 when formatting fails

Open frou opened this issue 2 years ago • 3 comments

Currently the standalone erlfmt executable exits with code 0 when formatting fails because the code being formatted has a syntax error in it. That is the same exit code which is used when formatting succeeds.

I think it should exit with code 1 (erlang:halt(1)) when formatting fails due to a syntax error.

frou avatar Oct 07 '21 13:10 frou

This is intentional. What erlfmt does in case like this is it reproduces the original source for the parts of the program it cannot handle correctly.

Because Erlang macros are extremely flexible, there is no way for us to cover all of them - in cases where erlfmt is not (yet) able to handle a particular construct, we just skip formatting that part of the code (the enclosing dot-delimited form). This seems like a more reasonable behaviour than crashing hard and refusing the format the entire file, since it's very likely the code is correct, just erlfmt is not good enough for the particular code pattern

michalmuskala avatar Oct 08 '21 14:10 michalmuskala

Thank you. Then it doesn't necessarily need to give up and refuse to format, it could take the same action that it does now, but just use a different exit code at the end when it's about to exit anyway. (code 3? AFAIK code 1 for failure, and code 2 is for being invoked incorrectly).

Without this, if say an editor is using the erlfmt executable programmatically, it needs to get into the business of parsing stderr to determine how effective the operation was.

frou avatar Oct 08 '21 14:10 frou

I now have an easy workaround for my purposes. So if the additional exit code thing sounds like more trouble than it's worth, feel free to just close this issue :)

frou avatar Oct 09 '21 10:10 frou