-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
syntax: keep "exit guards" in place #564
Comments
BTW, there are other similar workarounds, but they also require two things on same line, e.g.: {
# code here
}; exit $?; |
The formatter doesn't try to interpret or understand your code in any way. So we can't treat However, you might be interested in #261, which is a more generic solution we could actually implement. If you configured the formatter to allow two consecutive commands on a single line, then I think your single-liner would work. |
Yeah, I think solution for #261 will works here too. |
Thanks! Closing in favor of that one then. |
This is an interesting feature of bash, and an interesting possible-bug because of it. Wouldn't putting function main {
# ... code ...
exit $?
}
main Bash has to read the entirety of the |
@theclapp I doubt this will work. AFAIK bash updates file seek position after executing each line - to support unpacking shell-archives located at same file. So, I suppose while executing main() it'll do seek() after each line, and thus will be affected by the issue. Two things should be at same line to avoid this - it won't seek() at the middle of the line (I suppose). |
No, it reads all of main at once, stores it, and then executes it when called. If this wasn't true, then the trick of writing In any case, fwiw, I tried it ( For anyone following along at home: If you want to try to reproduce this, be sure your editor writes files in place. E.g. in Vim |
I think it was wrong to close this issue as a duplicate of #261; re-reading both again, #261 was mostly about preference or semantics, while this is purely about defensive coding. Personally, I think @theclapp's solution is the sanest. It works with the current formatting rules - it doesn't require placing two commands in a single line. If we tried to go the opposite route, and somehow detect @powerman thoughts? |
Also, I'm open to ideas on how we could detect the "safe pattern" of having two commands in a single line, without allowing any |
I don't have personal preferences, any working solution is good for me.
|
What you say makes sense, but I still don't know how to reliably detect this without turning off line splitting entirely. |
I belive we don't have to implement this reliably - best effort (e.g. few regexp) should be good enough and much better than current behaviour anyway. |
So, quite literally, just matching |
Maybe even |
I would really appreciate if people gave real examples of these multi-command lines used for safety. So far, I've only been shown Are there others? |
Here's my snippet: if [ ! -f files/SHA256SUMS ]; then echo "files/SHA256SUMS file missing." 1>&2; exit 1; fi
if [ ! -f files/SHA256SUMS ]; then
echo "files/SHA256SUMS file missing." 1>&2
exit 1
fi My co-worker hates this formatting for certain validation rules. I don't give a shit. I'm looking to see if there is a way to annotate a file/line to be left alone. This would allow us to use the good parts of Trying to find a solution, I got bounced from #564 → #261 → #564, and I still can't tell if there's a solution to my issue or not. Is there a straight answer I've missed? |
There is no support for annotations or to turn off the formatting for parts of a script, no. There are no plans for that either. This issue will likely get fixed, because exit guards do serve a purpose. Then, your original line would likely format as:
|
This specific code can be rewritten as
This issue is about guarding against in-place edits while script is running. |
That's unfortunate. Guess I'll need to roll up my sleeves and get in a fight. Thanks. |
https://docs.fedoraproject.org/en-US/Fedora_Security_Team/1/html/Defensive_Coding/sect-Defensive_Coding-Shell-Edit_Guard.html recommends to have
main "$@" ; exit $?
as single line… Maybe it's worth to support this case and avoid auto-rewriting it as two lines?The text was updated successfully, but these errors were encountered: