zsh-workers
 help / color / mirror / code / Atom feed
From: Philippe Altherr <philippe.altherr@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh-workers@zsh.org
Subject: Re: Inconsistent behavior of ERR_EXIT with conditionals
Date: Fri, 11 Nov 2022 04:04:30 +0100	[thread overview]
Message-ID: <CAGdYchteCWPt_2skasZr3suB6HKxtnT4-ARAjivT1BkeGDdy6A@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7anFpEGnPiLrvYypgtJvY=DuCeP5Xm4cZnyHwP5KzTz0w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5254 bytes --]

>
> One last thought ... have you tried checking the behavior with both
> ERR_EXIT and ERR_RETURN ?  Functions behave differently for ERR_RETURN
> than ERR_EXIT.


Ha, I had the exact same thought yesterday while studying exec.c and loop.c
but no, ERR_RETURN doesn't help. ERR_EXIT gets disabled on the left of "&&"
and "||" and in the condition of if and loop statements. ERR_RETURN gets
re-enabled on function calls. For my behavior, I too would need re-enabling
but of ERR_EXIT and more importantly it would have to happen on the left of
";" (i.e., for every command that is followed by another one) rather than
on function calls.

I'm still studying the code to figure out whether my behavior is at all
possible and what it would require. At the moment, many parts of the code
still look like black-magic but I slowly start to understand what's going
on. In that regard, the description of word codes in parse.c
<https://sourceforge.net/p/zsh/code/ci/master/tree/Src/parse.c#l100> was
very helpful. I think that I now have a relatively good understanding of
how the parsed code is represented overall but I'm still missing many of
the details. Something that would be of tremendous help is the ability to
see the parsed word codes. Is it possible to print these somehow? And/or is
it possible to print the word codes as they get evaluated? I haven't seen
anything to that effect. Is there anything else you would recommend that I
read and/or use to better understand the codebase?

One example of a legitimate use of ERR_EXIT is to implement (shelling
> out from) "make -k" where each command is distinct and you want the
> build process to stop if one step is incomplete.


Although I must admit that I only very partially understand your example, I
assume that you see ERR_EXIT more like a feature for other tools like
"make" that need to run arbitrary shell commands than something for
standalone shell scripts. I could also imagine that ERR_EXIT was originally
designed for such cases. However, in my experience, ERR_EXIT is often used
as a safeguard for standalone scripts. Even though it's far from perfect,
it's definitely better than nothing. And, in my opinion, it's a legitimate
use of ERR_EXIT, even if that wasn't its original purpose.

I'm mainly a Java developer. In Java many functions may throw runtime
exceptions. You can catch and handle these but in 99% of the cases you just
ignore them because you know (or assume) that they won't be thrown. Doing
otherwise would be very impractical because your code would be littered
with try/catch statements. Every now and then one of these exceptions that
you thought could/should never be thrown is nevertheless thrown for some
reason. In that case the exception goes up the call stack and, if no catch
is encountered, it kills the program and prints a stack trace to the code
that threw the exception. This way it's obvious that something failed. It's
also easy to figure out where it happened. And, it ensures that no extra
harm is done (by keeping running the program).

In the world of shell scripts, there are no runtime exceptions but some
exit statuses play a similar role. Many UNIX commands and shell functions
can potentially return a non-zero exit status but when you use them you
often know (or assume) that they won't return a non-zero exit status. If
they nevertheless do, the default behavior of shell script is to simply
ignore the error. So if "cmd1" fails in "cmd1; cmd2", "cmd2" still gets
executed. It's already bad that "cmd1" failed. Running "cmd2" could
cause extra harm. It looks much safer to exit right after "cmd1". That's
the main reason I run all my scripts with ERR_EXIT enabled.

In addition to that, ERR_EXIT also makes it easier to notice errors and to
figure out where they happened, especially if ERR_EXIT is coupled with code
that prints a stack trace. Without ERR_EXIT you may not even notice that
something went wrong if no error message was printed (or it got swallowed).

My impression is that ERR_EXIT is commonly used for these reasons. In fact,
whenever I have seen it used, it seemed to be for these reasons. But maybe
I should confirm that. I'll ask some of my friends if they use it and with
what expectations.

In my opinion, the only downside of that usage of ERR_EXIT is that it's far
from foolproof. There are plenty of cases where just enabling ERR_EXIT
won't be enough to ensure that the script halts at the first unexpected
error. I would like to improve that. My zabort script already goes a long
way. It works around the non-propagation issues, which are by far the main
reason why just ERR_EXIT isn't enough. That's good but it would be even
better if the non-triggering issues could also be solved, even if these are
less common.

Philippe


On Thu, Nov 10, 2022 at 6:09 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Wed, Nov 9, 2022 at 5:00 PM Bart Schaefer <schaefer@brasslantern.com>
> wrote:
> >
> > I'm going to stop responding anyway, because I'm not interested in the
> > stated goal.  If others want to pursue this discussion, please go
> > ahead.
>
> One last thought ... have you tried checking the behavior with both
> ERR_EXIT and ERR_RETURN ?  Functions behave differently for ERR_RETURN
> than ERR_EXIT.
>

[-- Attachment #2: Type: text/html, Size: 6385 bytes --]

  reply	other threads:[~2022-11-11  3:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 16:37 Philippe Altherr
2022-11-06 20:45 ` Bart Schaefer
2022-11-07  3:50   ` Bart Schaefer
2022-11-07  5:35     ` [PATCH] " Bart Schaefer
2022-11-07  9:44       ` Peter Stephenson
2022-11-08  1:20         ` Bart Schaefer
2022-11-08  4:58     ` Philippe Altherr
2022-11-08  5:36       ` Bart Schaefer
2022-11-08  8:04         ` Lawrence Velázquez
2022-11-08 18:51           ` Philippe Altherr
2022-11-08 19:20             ` Lawrence Velázquez
2022-11-08 23:28             ` Bart Schaefer
2022-11-09  4:11               ` Philippe Altherr
2022-11-09  6:00                 ` Bart Schaefer
2022-11-09 14:22                   ` Philippe Altherr
2022-11-10  1:00                     ` Bart Schaefer
2022-11-10  5:09                       ` Bart Schaefer
2022-11-11  3:04                         ` Philippe Altherr [this message]
2022-11-11  4:06                           ` Lawrence Velázquez
2022-11-11  4:09                           ` Eric Cook
2022-11-08 23:11           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGdYchteCWPt_2skasZr3suB6HKxtnT4-ARAjivT1BkeGDdy6A@mail.gmail.com \
    --to=philippe.altherr@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).