fixed #228: make exit code customizable to indicate whether files were changed#286
fixed #228: make exit code customizable to indicate whether files were changed#286bwendling merged 2 commits intogoogle:masterfrom reece:master
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
|
I signed it!
|
|
CLAs look good, thanks! |
|
Question, will your PR result in sensible behavior in case YAPF exits because of an unhandled exception? It appears to make no distinction between an error and any normal result. |
|
With respect to errors/exceptions, I believe that the code operates exactly as before. Note that no change was made to For example, with a non-existent file and my .bashrc: The PR is intended to act like this:
The PR is implemented with the default |
|
@gwelymernans, @vergult, @carlthome, @sanmai-NL : I'd appreciate comments on this PR re: yapf exit codes. This PR seems like a good compromise to me: folks who want exit code 0 for success irrespective of whether files were modified get that by default, and folks who to distinguish those cases by exit code get that functionality with a simple flag. Exit error codes are unchanged. |
|
Great! |
|
@reece: As I wrote in #228, if we are to provide detailed result information (files changed or not), which I think we are, then it's best to return that as regular, parseable standard output, in line with Unix conventions. It could be as simple as returning In YAPF 0.11.0 |
|
Apologies for my absence. @reece's and @sanmai-NL's comments are well taken. I'm not entirely sure how best to proceed, so I looked at an already established tool: Let me throw in my suggestion for good measure:
This retains the 0 is normal / 1 is error standard that most Unix commands adhere to. It also allows those who want to know which files were modified to get that information without unnecessarily spamming those who don't care about that information. My question then becomes whether this is sufficiently "standard" practice for other Unix tools so that people won't be too surprised by its behavior. Thoughts? |
|
AFAIK, that would be very conventional and solve the issue at hand. Nonetheless, such output would also be a bit suboptimal as part of Note that the ‘was modified’ text would be redundant, since we're talking about parseable stdout output. If you ultimately wish to list paths instead of the diff then I would propose to cut that out. Also, please make the list 0 byte separated. |
|
@gwelymernans: That WFM. Suggestion: merge this PR as-is since it moves in the right direction. Then, create a new issue to discuss and implement a new feature for listing changed files. That will enable the project to move forward with incremental progress. Thanks for yapf! |
|
@reece: |
|
I meant to keep |
|
@reece: I'm not sure we want to keep the @sanmai-NL: Excuse my ignorance, but I'm not 100% sure what you mean by 0-byte separated. Do you have an example? From the link, I gather that it's an encoding issue. As far as what to print out, we could allow the |
|
@gwelymernans: your example output contained line-ending characters to separate the file paths. If you want to list file paths, please separate them by zero bytes instead. Then Unix utilities (e.g., |
|
Status? |
|
@carlthome I'm okay with going ahead with the initial change where 0 indicates a non-error return (whether the file was changed or not) and 1 indicates some type of error. I want to hold off of adding a @reece could you modify your pull request to have just a 0 / 1 return? Also, please update the |
|
Thank you! |
|
I can see the rationale behind returning a 0 whenever there are no errors, but thinking about a fix for the Emacs plugin yapfify made me realise that the new solution also feels weird. When there are changes the whole corrected file is written to stdout, but nothing is written to stdout when the file contains nothing to be reformatted. If YAPF were to always write the completomething always has to break somewhere when you make a change like this :). After this changee output, you would only have to check for the exit code before using the output somewhere else. In the current situation you would have to check if anything was actually written to stdout, even if you know from the exit code that nothing went wrong. Would it be a good idea to always return the corrected file on stdout? |
|
I just found that YAPF does always return output when it operates on stdin. That means the fix for yapfify is easy, but it still seems strange that the behaviour is different between operating on a file or stdin in this way. |
Really? I agree that yapf should always return something on stdout when given input on stdin, even when unchanged (except in cases of legit errors). That behavior is most consistent with decades of Unix stdio conventions (sed, grep, perl, etc). I'm not seeing the behavior you cite. Some examples: Is that consistent with what you see? |
|
Exactly, luckily I found that YAPF does indeed always write to stdout when given input on stdout. My confusion stemmed from the fact that it does not return anything on stdout when given a file as input, like so: Is that also expected Unix behavior? I honoustly don't know, but it surprised me. |
|
Although there are formal specs (like POSIX), a lot of behavior is by convention. The following is entirely my opinion. I like the following basic templates for programs:
Conversely, mixing any of the above themes seems vaguely inconsistent. So, |
|
Re the output, @JorisE, you found the answer I was going to give you. :-) (Thank you for patching As @reece mentioned, the behavior I've seen has been mostly by convention as well. I do want to follow the "least surprise" axiom in this situation. For other formatting tools, like I like the idea of |
|
Thanks for the answers. |
|
@reece sorry for answering late. I'm totally satisfied with your final changes: small and simple. This will result in a less surprising behavior, thank you! Regarding the Thanks to all! |
As with others on #228, I was surprised to learn that yapf returns a non-success error code even when it completes successfully. This breaks long-standing convention (that, yes, is broken by other tools also).
The attached patch makes the return code in the case of changed files customizable. By default, 0 is returned when files are formatted according to the effective style, regardless of whether changes occurred or not. Callers may request that another exit code be used to indicate successful execution AND that files were modified. For example,
yapf -c 2recovers the previous behavior to exit with code 2.